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_LogInit();
NS_InitAtomTable();
mozilla::LogModule::Init(); mozilla::LogModule::Init();
JS_SetCurrentEmbedderTimeFunction(TimeSinceProcessCreation); JS_SetCurrentEmbedderTimeFunction(TimeSinceProcessCreation);
@ -1026,7 +1028,7 @@ ShutdownXPCOM(nsIServiceManager* aServMgr)
nsComponentManagerImpl::gComponentManager = nullptr; nsComponentManagerImpl::gComponentManager = nullptr;
nsCategoryManager::Destroy(); nsCategoryManager::Destroy();
NS_PurgeAtomTable(); NS_ShutdownAtomTable();
NS_IF_RELEASE(gDebug); NS_IF_RELEASE(gDebug);

View File

@ -382,28 +382,6 @@ static const PLDHashTableOps AtomTableOps = {
AtomTableInitEntry 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() DynamicAtom::~DynamicAtom()
@ -464,34 +442,48 @@ static StaticAtomTable* gStaticAtomTable = nullptr;
*/ */
static bool gStaticAtomTableSealed = false; 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 void
NS_PurgeAtomTable() NS_InitAtomTable()
{
MOZ_ASSERT(!gAtomTable);
gAtomTable = new PLDHashTable(&AtomTableOps, sizeof(AtomTableEntry),
ATOM_HASHTABLE_INITIAL_LENGTH);
}
void
NS_ShutdownAtomTable()
{ {
delete gStaticAtomTable; delete gStaticAtomTable;
gStaticAtomTable = nullptr; gStaticAtomTable = nullptr;
if (gAtomTable) { // XXXbholley: it would be good to assert gAtomTable->EntryCount() == 0
// XXXbholley: it would be good to assert gAtomTable->EntryCount() == 0 // here, but that currently fails. Probably just a few things that need
// here, but that currently fails. Probably just a few things that need // to be fixed up.
// to be fixed up. delete gAtomTable;
delete gAtomTable; gAtomTable = nullptr;
gAtomTable = nullptr;
}
} }
void void
NS_SizeOfAtomTablesIncludingThis(MallocSizeOf aMallocSizeOf, NS_SizeOfAtomTablesIncludingThis(MallocSizeOf aMallocSizeOf,
size_t* aMain, size_t* aStatic) size_t* aMain, size_t* aStatic)
{ {
*aMain = 0; *aMain = gAtomTable->ShallowSizeOfIncludingThis(aMallocSizeOf);
if (gAtomTable) { for (auto iter = gAtomTable->Iter(); !iter.Done(); iter.Next()) {
*aMain += gAtomTable->ShallowSizeOfIncludingThis(aMallocSizeOf); auto entry = static_cast<AtomTableEntry*>(iter.Get());
for (auto iter = gAtomTable->Iter(); !iter.Done(); iter.Next()) { *aMain += entry->mAtom->SizeOfIncludingThis(aMallocSizeOf);
auto entry = static_cast<AtomTableEntry*>(iter.Get());
*aMain += entry->mAtom->SizeOfIncludingThis(aMallocSizeOf);
}
} }
// The atoms pointed to by gStaticAtomTable are also pointed to by gAtomTable, // The atoms pointed to by gStaticAtomTable are also pointed to by gAtomTable,
@ -505,7 +497,6 @@ static inline AtomTableEntry*
GetAtomHashEntry(const char* aString, uint32_t aLength, uint32_t* aHashOut) GetAtomHashEntry(const char* aString, uint32_t aLength, uint32_t* aHashOut)
{ {
MOZ_ASSERT(NS_IsMainThread(), "wrong thread"); MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
EnsureTableExists();
AtomTableKey key(aString, aLength, aHashOut); AtomTableKey key(aString, aLength, aHashOut);
// This is an infallible add. // This is an infallible add.
return static_cast<AtomTableEntry*>(gAtomTable->Add(&key)); 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) GetAtomHashEntry(const char16_t* aString, uint32_t aLength, uint32_t* aHashOut)
{ {
MOZ_ASSERT(NS_IsMainThread(), "wrong thread"); MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
EnsureTableExists();
AtomTableKey key(aString, aLength, aHashOut); AtomTableKey key(aString, aLength, aHashOut);
// This is an infallible add. // This is an infallible add.
return static_cast<AtomTableEntry*>(gAtomTable->Add(&key)); return static_cast<AtomTableEntry*>(gAtomTable->Add(&key));

View File

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