Bug 496593 - Image cache entry comparison is wrong. r=vladimir, sr=bzbarsky
authorJoe Drew <joe@drew.ca>
Mon, 15 Jun 2009 15:33:48 -0700
changeset 29251 de5bca83e6822fb19f8127dc9ca841b71ee6e22b
parent 29250 7b79aea438216a2cec50f4814edad4520e8d690b
child 29252 8c22f0edf182ef44f65d19dd0da141e77985c688
push idunknown
push userunknown
push dateunknown
reviewersvladimir, bzbarsky
bugs496593
milestone1.9.2a1pre
Bug 496593 - Image cache entry comparison is wrong. r=vladimir, sr=bzbarsky
modules/libpr0n/src/imgLoader.h
--- a/modules/libpr0n/src/imgLoader.h
+++ b/modules/libpr0n/src/imgLoader.h
@@ -239,30 +239,38 @@ public:
 
   static nsresult InitCache();
 
   static PRBool RemoveFromCache(nsIURI *aKey);
   static PRBool RemoveFromCache(imgCacheEntry *entry);
 
   static PRBool PutIntoCache(nsIURI *key, imgCacheEntry *entry);
 
-  // Returns true if |one| is less than |two|
+  // Returns true if we should prefer evicting cache entry |two| over cache
+  // entry |one|.
   // This mixes units in the worst way, but provides reasonable results.
   inline static bool CompareCacheEntries(const nsRefPtr<imgCacheEntry> &one,
-                                  const nsRefPtr<imgCacheEntry> &two)
+                                         const nsRefPtr<imgCacheEntry> &two)
   {
     if (!one)
       return false;
     if (!two)
       return true;
 
-    const PRFloat64 sizeweight = 1.0 - sCacheTimeWeight;
-    PRInt32 diffsize = PRInt32(two->GetDataSize()) - PRInt32(one->GetDataSize());
-    PRInt32 difftime = one->GetTouchedTime() - two->GetTouchedTime();
-    return difftime * sCacheTimeWeight + diffsize * sizeweight < 0;
+    const double sizeweight = 1.0 - sCacheTimeWeight;
+
+    // We want large, old images to be evicted first (depending on their
+    // relative weights). Since a larger time is actually newer, we subtract
+    // time's weight, so an older image has a larger weight.
+    double oneweight = double(one->GetDataSize()) * sizeweight -
+                       double(one->GetTouchedTime()) * sCacheTimeWeight;
+    double twoweight = double(two->GetDataSize()) * sizeweight -
+                       double(two->GetTouchedTime()) * sCacheTimeWeight;
+
+    return oneweight < twoweight;
   }
 
   static void VerifyCacheSizes();
 
   // The image loader maintains a hash table of all imgCacheEntries. However,
   // only some of them will be evicted from the cache: those who have no
   // imgRequestProxies watching their imgRequests. 
   //