Bug 1038357 - Added automatic pruning for mForcedValidEntries in CacheStorage Service. r=honzab
☠☠ backed out by 42935adf0c70 ☠ ☠
authorJeremy Poulin <jpoulin@cs.uml.edu>
Tue, 15 Jul 2014 09:23:09 -0700
changeset 217171 98fa8afd916933a650dcf670873f9ef78c33b3c9
parent 217170 9701d7726c6286fdcb0fa47faf3867292e5536fc
child 217172 0ce58071a3321a3338f903bbbd0c6d18330c9203
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershonzab
bugs1038357
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1038357 - Added automatic pruning for mForcedValidEntries in CacheStorage Service. r=honzab
netwerk/cache2/CacheStorageService.cpp
netwerk/cache2/CacheStorageService.h
--- a/netwerk/cache2/CacheStorageService.cpp
+++ b/netwerk/cache2/CacheStorageService.cpp
@@ -1062,23 +1062,52 @@ bool CacheStorageService::IsForcedValidE
 
 // Allows a cache entry to be loaded directly from cache without further
 // validation - see nsICacheEntry.idl for further details
 void CacheStorageService::ForceEntryValidFor(nsACString &aCacheEntryKey,
                                              uint32_t aSecondsToTheFuture)
 {
   mozilla::MutexAutoLock lock(mLock);
 
+  TimeStamp now = TimeStamp::NowLoRes();
+  ForcedValidEntriesPrune(now);
+
   // This will be the timeout
-  TimeStamp validUntil = TimeStamp::NowLoRes() +
-    TimeDuration::FromSeconds(aSecondsToTheFuture);
+  TimeStamp validUntil = now + TimeDuration::FromSeconds(aSecondsToTheFuture);
 
   mForcedValidEntries.Put(aCacheEntryKey, validUntil);
 }
 
+namespace { // anon
+
+PLDHashOperator PruneForcedValidEntries(
+  const nsACString& aKey, TimeStamp& aTimeStamp, void* aClosure)
+{
+  TimeStamp* now = static_cast<TimeStamp*>(aClosure);
+  if (aTimeStamp < *now) {
+    return PL_DHASH_REMOVE;
+  }
+
+  return PL_DHASH_NEXT;
+}
+
+} // anon
+
+// Cleans out the old entries in mForcedValidEntries
+void CacheStorageService::ForcedValidEntriesPrune(TimeStamp &now)
+{
+  static TimeDuration const oneMinute = TimeDuration::FromSeconds(60);
+  static TimeStamp dontPruneUntil = now + oneMinute;
+  if (now < dontPruneUntil)
+    return;
+
+  mForcedValidEntries.Enumerate(PruneForcedValidEntries, &now);
+  dontPruneUntil = now + oneMinute;
+}
+
 void
 CacheStorageService::OnMemoryConsumptionChange(CacheMemoryConsumer* aConsumer,
                                                uint32_t aCurrentMemoryConsumption)
 {
   LOG(("CacheStorageService::OnMemoryConsumptionChange [consumer=%p, size=%u]",
     aConsumer, aCurrentMemoryConsumption));
 
   uint32_t savedMemorySize = aConsumer->mReportedMemoryConsumption;
--- a/netwerk/cache2/CacheStorageService.h
+++ b/netwerk/cache2/CacheStorageService.h
@@ -275,16 +275,18 @@ private:
   nsresult AddStorageEntry(nsCSubstring const& aContextKey,
                            nsIURI* aURI,
                            const nsACString & aIdExtension,
                            bool aWriteToDisk,
                            bool aCreateIfNotExist,
                            bool aReplace,
                            CacheEntryHandle** aResult);
 
+  void ForcedValidEntriesPrune(TimeStamp &now);
+
   static CacheStorageService* sSelf;
 
   mozilla::Mutex mLock;
 
   // Tracks entries that may be forced valid.
   nsDataHashtable<nsCStringHashKey, TimeStamp> mForcedValidEntries;
 
   bool mShutdown;