From 0421ef29005d4ab3ef3abd8b49fed3a6df0d4d2f Mon Sep 17 00:00:00 2001 From: Eric Rahm Date: Fri, 16 Feb 2018 15:58:30 -0800 Subject: [PATCH] Bug 1439047 - Part 2: Cleanup StartupCache::PutBuffer hashtable usage. r=froydnj This switches over to using `LookupForAdd` which allows us to avoid a second lookup when adding the entry. Addtionally `nsDependentCString` is used to avoid copying the id string when looking up the entry. --- startupcache/StartupCache.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/startupcache/StartupCache.cpp b/startupcache/StartupCache.cpp index f3ec02d31c4a..fefe3ef541d6 100644 --- a/startupcache/StartupCache.cpp +++ b/startupcache/StartupCache.cpp @@ -310,11 +310,11 @@ StartupCache::PutBuffer(const char* id, UniquePtr&& inbuf, uint32_t len) return NS_ERROR_NOT_AVAILABLE; } - nsCString idStr(id); + nsDependentCString idStr(id); // Cache it for now, we'll write all together later. - CacheEntry* entry; + auto entry = mTable.LookupForAdd(idStr); - if (mTable.Get(idStr)) { + if (entry) { NS_WARNING("Existing entry in StartupCache."); // Double-caching is undesirable but not an error. return NS_OK; @@ -327,8 +327,9 @@ StartupCache::PutBuffer(const char* id, UniquePtr&& inbuf, uint32_t len) } #endif - entry = new CacheEntry(Move(inbuf), len); - mTable.Put(idStr, entry); + entry.OrInsert([&inbuf, &len]() { + return new CacheEntry(Move(inbuf), len); + }); mPendingWrites.AppendElement(idStr); return ResetStartupWriteTimer(); }