Bug 1275755 - Use an explicit init routine for the atom table. r=froydnj

This commit is contained in:
Bobby Holley 2016-05-25 18:45:17 -07:00
parent 60591b7a86
commit 3eec1c3284
3 changed files with 35 additions and 42 deletions

View File

@ -492,6 +492,8 @@ NS_InitXPCOM2(nsIServiceManager** aResult,
NS_LogInit();
NS_InitAtomTable();
mozilla::LogModule::Init();
JS_SetCurrentEmbedderTimeFunction(TimeSinceProcessCreation);
@ -1026,7 +1028,7 @@ ShutdownXPCOM(nsIServiceManager* aServMgr)
nsComponentManagerImpl::gComponentManager = nullptr;
nsCategoryManager::Destroy();
NS_PurgeAtomTable();
NS_ShutdownAtomTable();
NS_IF_RELEASE(gDebug);

View File

@ -382,28 +382,6 @@ static const PLDHashTableOps AtomTableOps = {
AtomTableInitEntry
};
// The atom table very quickly gets 10,000+ entries in it (or even 100,000+).
// But choosing the best initial length has some subtleties: we add ~2700
// static atoms to the table at start-up, and then we start adding and removing
// dynamic atoms. If we make the table too big to start with, when the first
// dynamic atom gets removed the load factor will be < 25% and so we will
// shrink it to 4096 entries.
//
// By choosing an initial length of 4096, we get an initial capacity of 8192.
// That's the biggest initial capacity that will let us be > 25% full when the
// first dynamic atom is removed (when the count is ~2700), thus avoiding any
// shrinking.
#define ATOM_HASHTABLE_INITIAL_LENGTH 4096
static inline void
EnsureTableExists()
{
if (!gAtomTable) {
gAtomTable = new PLDHashTable(&AtomTableOps, sizeof(AtomTableEntry),
ATOM_HASHTABLE_INITIAL_LENGTH);
}
}
//----------------------------------------------------------------------
DynamicAtom::~DynamicAtom()
@ -464,35 +442,49 @@ static StaticAtomTable* gStaticAtomTable = nullptr;
*/
static bool gStaticAtomTableSealed = false;
//----------------------------------------------------------------------
// The atom table very quickly gets 10,000+ entries in it (or even 100,000+).
// But choosing the best initial length has some subtleties: we add ~2700
// static atoms to the table at start-up, and then we start adding and removing
// dynamic atoms. If we make the table too big to start with, when the first
// dynamic atom gets removed the load factor will be < 25% and so we will
// shrink it to 4096 entries.
//
// By choosing an initial length of 4096, we get an initial capacity of 8192.
// That's the biggest initial capacity that will let us be > 25% full when the
// first dynamic atom is removed (when the count is ~2700), thus avoiding any
// shrinking.
#define ATOM_HASHTABLE_INITIAL_LENGTH 4096
void
NS_PurgeAtomTable()
NS_InitAtomTable()
{
MOZ_ASSERT(!gAtomTable);
gAtomTable = new PLDHashTable(&AtomTableOps, sizeof(AtomTableEntry),
ATOM_HASHTABLE_INITIAL_LENGTH);
}
void
NS_ShutdownAtomTable()
{
delete gStaticAtomTable;
gStaticAtomTable = nullptr;
if (gAtomTable) {
// XXXbholley: it would be good to assert gAtomTable->EntryCount() == 0
// here, but that currently fails. Probably just a few things that need
// to be fixed up.
delete gAtomTable;
gAtomTable = nullptr;
}
}
void
NS_SizeOfAtomTablesIncludingThis(MallocSizeOf aMallocSizeOf,
size_t* aMain, size_t* aStatic)
{
*aMain = 0;
if (gAtomTable) {
*aMain += gAtomTable->ShallowSizeOfIncludingThis(aMallocSizeOf);
*aMain = gAtomTable->ShallowSizeOfIncludingThis(aMallocSizeOf);
for (auto iter = gAtomTable->Iter(); !iter.Done(); iter.Next()) {
auto entry = static_cast<AtomTableEntry*>(iter.Get());
*aMain += entry->mAtom->SizeOfIncludingThis(aMallocSizeOf);
}
}
// The atoms pointed to by gStaticAtomTable are also pointed to by gAtomTable,
// and they're measured by the loop above. So no need to measure them here.
@ -505,7 +497,6 @@ static inline AtomTableEntry*
GetAtomHashEntry(const char* aString, uint32_t aLength, uint32_t* aHashOut)
{
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
EnsureTableExists();
AtomTableKey key(aString, aLength, aHashOut);
// This is an infallible add.
return static_cast<AtomTableEntry*>(gAtomTable->Add(&key));
@ -515,7 +506,6 @@ static inline AtomTableEntry*
GetAtomHashEntry(const char16_t* aString, uint32_t aLength, uint32_t* aHashOut)
{
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
EnsureTableExists();
AtomTableKey key(aString, aLength, aHashOut);
// This is an infallible add.
return static_cast<AtomTableEntry*>(gAtomTable->Add(&key));

View File

@ -10,7 +10,8 @@
#include "mozilla/MemoryReporting.h"
#include <stddef.h>
void NS_PurgeAtomTable();
void NS_InitAtomTable();
void NS_ShutdownAtomTable();
void NS_SizeOfAtomTablesIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
size_t* aMain, size_t* aStatic);