Bug 569709 - Figure out the max number of entries we should store in the disk cache, and bump the default size of the disk cache to 250MB; r=jduell
authorEhsan Akhgari <ehsan@mozilla.com>
Thu, 10 Jun 2010 22:46:51 -0400
changeset 43560 cc8369aa7bd345649077a34a0ee1e592fb65b724
parent 43559 e227a12b97a5ba9f1073742b63b11b15899bc7fd
child 43561 3224dabf012e1de8aa52bdd76dd94f84c60d10c4
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersjduell
bugs569709
milestone1.9.3a6pre
Bug 569709 - Figure out the max number of entries we should store in the disk cache, and bump the default size of the disk cache to 250MB; r=jduell
modules/libpref/src/init/all.js
netwerk/cache/nsDiskCacheDevice.cpp
netwerk/cache/nsDiskCacheMap.cpp
netwerk/cache/nsDiskCacheMap.h
--- a/modules/libpref/src/init/all.js
+++ b/modules/libpref/src/init/all.js
@@ -55,17 +55,17 @@ pref("general.config.obscure_value", 13)
 
 pref("general.warnOnAboutConfig", true);
 
 // maximum number of dated backups to keep at any time
 pref("browser.bookmarks.max_backups",       5);
 
 pref("browser.cache.disk.enable",           true);
 #ifndef WINCE
-pref("browser.cache.disk.capacity",         51200);
+pref("browser.cache.disk.capacity",         256000); // 250MB
 #else
 pref("browser.cache.disk.capacity",         20000);
 #endif
 pref("browser.cache.memory.enable",         true);
 //pref("browser.cache.memory.capacity",     -1);
 // -1 = determine dynamically, 0 = none, n = memory capacity in kilobytes
 pref("browser.cache.disk_cache_ssl",        true);
 // 0 = once-per-session, 1 = each-time, 2 = never, 3 = when-appropriate/automatically
--- a/netwerk/cache/nsDiskCacheDevice.cpp
+++ b/netwerk/cache/nsDiskCacheDevice.cpp
@@ -1029,16 +1029,18 @@ void
 nsDiskCacheDevice::SetCapacity(PRUint32  capacity)
 {
     // Units are KiB's
     mCacheCapacity = capacity;
     if (Initialized()) {
         // start evicting entries if the new size is smaller!
         EvictDiskCacheEntries(mCacheCapacity);
     }
+    // Let cache map know of the new capacity
+    mCacheMap.NotifyCapacityChange(capacity);
 }
 
 
 PRUint32 nsDiskCacheDevice::getCacheCapacity()
 {
     return mCacheCapacity;
 }
 
--- a/netwerk/cache/nsDiskCacheMap.cpp
+++ b/netwerk/cache/nsDiskCacheMap.cpp
@@ -289,24 +289,24 @@ nsDiskCacheMap::GetBucketRank(PRUint32 b
                 rank = records[i].EvictionRank();
     }
     return rank;
 }
 
 nsresult
 nsDiskCacheMap::GrowRecords()
 {
-    if (mHeader.mRecordCount >= kMaxRecordCount)
+    if (mHeader.mRecordCount >= mMaxRecordCount)
         return NS_OK;
     CACHE_LOG_DEBUG(("CACHE: GrowRecords\n"));
 
     // Resize the record array
-    PRUint32 newCount = mHeader.mRecordCount << 1;
-    if (newCount > kMaxRecordCount)
-        newCount = kMaxRecordCount;
+    PRInt32 newCount = mHeader.mRecordCount << 1;
+    if (newCount > mMaxRecordCount)
+        newCount = mMaxRecordCount;
     nsDiskCacheRecord *newArray = (nsDiskCacheRecord *)
             PR_REALLOC(mRecordArray, newCount * sizeof(nsDiskCacheRecord));
     if (!newArray)
         return NS_ERROR_OUT_OF_MEMORY;
 
     // Space out the buckets
     PRUint32 oldRecordsPerBucket = GetRecordsPerBucket();
     PRUint32 newRecordsPerBucket = newCount / kBuckets;
@@ -1039,8 +1039,22 @@ nsDiskCacheMap::EnsureBuffer(PRUint32 bu
             mBufferSize = 0;
             return NS_ERROR_OUT_OF_MEMORY;
         }
         mBuffer = buf;
         mBufferSize = bufSize;
     }
     return NS_OK;
 }        
+
+void
+nsDiskCacheMap::NotifyCapacityChange(PRUint32 capacity)
+{
+  // Heuristic 1. average cache entry size is probably around 1KB
+  // Heuristic 2. we don't want more than 32MB reserved to store the record
+  //              map in memory.
+  const PRInt32 RECORD_COUNT_LIMIT = 32 * 1024 * 1024 / sizeof(nsDiskCacheRecord);
+  PRInt32 maxRecordCount = PR_MIN(PRInt32(capacity), RECORD_COUNT_LIMIT);
+  if (mMaxRecordCount < maxRecordCount) {
+    // We can only grow
+    mMaxRecordCount = maxRecordCount;
+  }
+}
--- a/netwerk/cache/nsDiskCacheMap.h
+++ b/netwerk/cache/nsDiskCacheMap.h
@@ -83,17 +83,16 @@ struct nsDiskCacheEntry;
  *                       its actual size.  (XXX This is broken in places -darin)
  *
  *****************************************************************************/
 
 #define BLOCK_SIZE_FOR_INDEX(index)  ((index) ? (256 << (2 * ((index) - 1))) : 0)
 
 // Min and max values for the number of records in the DiskCachemap
 #define kMinRecordCount    512
-#define kMaxRecordCount    16384
 
 #define kSeparateFile      0
 #define kMaxDataFileSize   0x4000000   // 64 MiB
 #define kBuckets           (1 << 5)    // must be a power of 2!
 
 class nsDiskCacheRecord {
 
 private:
@@ -391,17 +390,19 @@ struct nsDiskCacheHeader {
 class nsDiskCacheMap {
 public:
 
      nsDiskCacheMap() : 
         mCacheDirectory(nsnull),
         mMapFD(nsnull),
         mRecordArray(nsnull),
         mBufferSize(0),
-        mBuffer(nsnull) { }
+        mBuffer(nsnull),
+        mMaxRecordCount(16384) // this default value won't matter
+    { }
 
     ~nsDiskCacheMap() {
         (void) Close(PR_TRUE);
     }
 
 /**
  *  File Operations
  *
@@ -412,16 +413,18 @@ public:
  */
     nsresult  Open( nsILocalFile *  cacheDirectory);
     nsresult  Close(PRBool flush);
     nsresult  Trim();
 
     nsresult  FlushHeader();
     nsresult  FlushRecords( PRBool unswap);
 
+    void      NotifyCapacityChange(PRUint32 capacity);
+
 /**
  *  Record operations
  */
     nsresult AddRecord( nsDiskCacheRecord *  mapRecord, nsDiskCacheRecord * oldRecord);
     nsresult UpdateRecord( nsDiskCacheRecord *  mapRecord);
     nsresult FindRecord( PRUint32  hashNumber, nsDiskCacheRecord *  mapRecord);
     nsresult DeleteRecord( nsDiskCacheRecord *  mapRecord);
     nsresult VisitRecords( nsDiskCacheRecordVisitor * visitor);
@@ -536,11 +539,12 @@ private:
 private:
     nsCOMPtr<nsILocalFile>  mCacheDirectory;
     PRFileDesc *            mMapFD;
     nsDiskCacheRecord *     mRecordArray;
     nsDiskCacheBlockFile    mBlockFile[3];
     PRUint32                mBufferSize;
     char *                  mBuffer;
     nsDiskCacheHeader       mHeader;
+    PRInt32                 mMaxRecordCount;
 };
 
 #endif // _nsDiskCacheMap_h_