Bug 1491151 - Part 2: Use MruCache for eTLD table. r=smaug

--HG--
extra : rebase_source : 5c588e2b2615549e5a5cc2fb6aa7dc9922d05c99
This commit is contained in:
Eric Rahm 2018-09-10 14:46:29 -07:00
Родитель b9e8fd399e
Коммит 338bc1ef5f
2 изменённых файлов: 23 добавлений и 30 удалений

Просмотреть файл

@ -10,6 +10,7 @@
#include "mozilla/ArrayUtils.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "MainThreadUtils.h"
@ -214,17 +215,20 @@ nsEffectiveTLDService::GetBaseDomainInternal(nsCString &aHostname,
// Lookup in the cache if this is a normal query. This is restricted to
// main thread-only as the cache is not thread-safe.
TLDCacheEntry* entry = nullptr;
Maybe<TldCache::Entry> entry;
if (aAdditionalParts == 1 && NS_IsMainThread()) {
if (LookupForAdd(aHostname, &entry)) {
auto p = mMruTable.Lookup(aHostname);
if (p) {
// There was a match, just return the cached value.
aBaseDomain = entry->mBaseDomain;
aBaseDomain = p.Data().mBaseDomain;
if (trailingDot) {
aBaseDomain.Append('.');
}
return NS_OK;
}
entry = Some(p);
}
// Walk up the domain tree, most specific to least specific,
@ -310,8 +314,7 @@ nsEffectiveTLDService::GetBaseDomainInternal(nsCString &aHostname,
// Update the MRU table if in use.
if (entry) {
entry->mHost = aHostname;
entry->mBaseDomain = aBaseDomain;
entry->Set(TLDCacheEntry{aHostname, nsCString(aBaseDomain)});
}
// add on the trailing dot, if applicable
@ -337,16 +340,6 @@ nsEffectiveTLDService::NormalizeHostname(nsCString &aHostname)
return NS_OK;
}
bool
nsEffectiveTLDService::LookupForAdd(const nsACString& aHost, TLDCacheEntry** aEntry)
{
MOZ_ASSERT(NS_IsMainThread());
const uint32_t hash = HashString(aHost.BeginReading(), aHost.Length());
*aEntry = &mMruTable[hash % kTableSize];
return (*aEntry)->mHost == aHost;
}
NS_IMETHODIMP
nsEffectiveTLDService::HasRootDomain(const nsACString& aInput,
const nsACString& aHost,

Просмотреть файл

@ -8,12 +8,14 @@
#include "nsIEffectiveTLDService.h"
#include "nsHashKeys.h"
#include "nsIMemoryReporter.h"
#include "nsString.h"
#include "nsCOMPtr.h"
#include "mozilla/Attributes.h"
#include "mozilla/Dafsa.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/MruCache.h"
class nsIIDNService;
@ -55,22 +57,20 @@ private:
// cache the result. During standard browsing the same domains are repeatedly
// fed into |GetBaseDomainInternal| so this ends up being an effective
// mitigation getting about a 99% hit rate with four tabs open.
//
// A size of 31 is used rather than a more logical power-of-two such as 32
// since it is a prime number and provides fewer collisions when when used
// with our hash algorithms.
static const uint32_t kTableSize = 31;
TLDCacheEntry mMruTable[kTableSize];
struct TldCache :
public mozilla::MruCache<nsACString, TLDCacheEntry, TldCache>
{
static mozilla::HashNumber Hash(const nsACString& aKey)
{
return mozilla::HashString(aKey);
}
static bool Match(const nsACString& aKey, const TLDCacheEntry& aVal)
{
return aKey == aVal.mHost;
}
};
/**
* Performs a lookup on the MRU table and provides a pointer to the hash
* entry that matched or should be used for adding this host.
*
* @param aHost The host to lookup.
* @param aEntry Out param, the entry in the MRU table to use.
* @return True if a match was found, false if there was a miss.
*/
inline bool LookupForAdd(const nsACString& aHost, TLDCacheEntry** aEntry);
TldCache mMruTable;
};
#endif // EffectiveTLDService_h