make nsStaticNameTable use PLDHash rather than nsHashtable, to reduce startup allocations from 1315 to 2

r=dougt, sr=dveditz
bug 154275
This commit is contained in:
alecf%netscape.com 2002-06-26 03:46:39 +00:00
Родитель 083f85bb7c
Коммит e2f4a1a1ad
2 изменённых файлов: 73 добавлений и 21 удалений

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

@ -36,13 +36,57 @@
#include "nscore.h" #include "nscore.h"
#include "nsString.h" #include "nsString.h"
#include "nsStaticNameTable.h"
#include "nsReadableUtils.h" #include "nsReadableUtils.h"
#define PL_ARENA_CONST_ALIGN_MASK 3
#include "nsStaticNameTable.h"
struct nameTableEntry : public PLDHashEntryHdr
{
// no ownership here!
const char *mKey;
PRInt32 mIndex;
};
PR_STATIC_CALLBACK(PRBool)
matchNameKeys(PLDHashTable*, const PLDHashEntryHdr* aHdr,
const void* key)
{
const nameTableEntry* entry =
NS_STATIC_CAST(const nameTableEntry *, aHdr);
const char *keyValue = NS_STATIC_CAST(const char*, key);
return (strcmp(entry->mKey, keyValue)==0);
}
PR_STATIC_CALLBACK(const void*)
getNameKey(PLDHashTable*, PLDHashEntryHdr* aHdr)
{
nameTableEntry* entry =
NS_STATIC_CAST(nameTableEntry*, aHdr);
return entry->mKey;
}
struct PLDHashTableOps nametable_HashTableOps = {
PL_DHashAllocTable,
PL_DHashFreeTable,
getNameKey,
PL_DHashStringKey,
matchNameKeys,
PL_DHashMoveEntryStub,
PL_DHashClearEntryStub,
PL_DHashFinalizeStub,
nsnull,
};
nsStaticCaseInsensitiveNameTable::nsStaticCaseInsensitiveNameTable() nsStaticCaseInsensitiveNameTable::nsStaticCaseInsensitiveNameTable()
: mNameArray(nsnull), mNameTable(nsnull), mCount(0), mNullStr("") : mNameArray(nsnull), mCount(0), mNullStr("")
{ {
MOZ_COUNT_CTOR(nsStaticCaseInsensitiveNameTable); MOZ_COUNT_CTOR(nsStaticCaseInsensitiveNameTable);
mNameTable.ops = nsnull;
} }
nsStaticCaseInsensitiveNameTable::~nsStaticCaseInsensitiveNameTable() nsStaticCaseInsensitiveNameTable::~nsStaticCaseInsensitiveNameTable()
@ -52,7 +96,7 @@ nsStaticCaseInsensitiveNameTable::~nsStaticCaseInsensitiveNameTable()
mNameArray[index].~nsDependentCString(); mNameArray[index].~nsDependentCString();
} }
nsMemory::Free((void*)mNameArray); nsMemory::Free((void*)mNameArray);
delete mNameTable; PL_DHashTableFinish(&mNameTable);
MOZ_COUNT_DTOR(nsStaticCaseInsensitiveNameTable); MOZ_COUNT_DTOR(nsStaticCaseInsensitiveNameTable);
} }
@ -60,15 +104,15 @@ PRBool
nsStaticCaseInsensitiveNameTable::Init(const char* Names[], PRInt32 Count) nsStaticCaseInsensitiveNameTable::Init(const char* Names[], PRInt32 Count)
{ {
NS_ASSERTION(!mNameArray, "double Init"); NS_ASSERTION(!mNameArray, "double Init");
NS_ASSERTION(!mNameTable, "double Init"); NS_ASSERTION(!mNameTable.ops, "double Init");
NS_ASSERTION(Names, "null name table"); NS_ASSERTION(Names, "null name table");
NS_ASSERTION(Count, "0 count"); NS_ASSERTION(Count, "0 count");
mCount = Count; mCount = Count;
mNameArray = (nsDependentCString*)nsMemory::Alloc(Count * sizeof(nsDependentCString)); mNameArray = (nsDependentCString*)nsMemory::Alloc(Count * sizeof(nsDependentCString));
// XXX best bucket count heuristic? PL_DHashTableInit(&mNameTable, &nametable_HashTableOps, nsnull,
mNameTable = new nsHashtable(Count<16 ? Count : Count<128 ? Count/4 : 128); sizeof(nameTableEntry), Count);
if (!mNameArray || !mNameTable) { if (!mNameArray || !mNameTable.ops) {
return PR_FALSE; return PR_FALSE;
} }
@ -86,29 +130,38 @@ nsStaticCaseInsensitiveNameTable::Init(const char* Names[], PRInt32 Count)
#endif #endif
// use placement-new to initialize the string object // use placement-new to initialize the string object
new (&mNameArray[index]) nsDependentCString(raw); new (&mNameArray[index]) nsDependentCString(raw);
nsCStringKey key(raw, len, nsCStringKey::NEVER_OWN);
mNameTable->Put(&key, (void*)(index+1)); // to make 0 != nsnull nameTableEntry *entry =
NS_STATIC_CAST(nameTableEntry*,
PL_DHashTableOperate(&mNameTable, raw, PL_DHASH_ADD));
NS_ASSERTION(entry->mKey == 0, "Entry already exists!");
entry->mKey = raw; // not owned!
entry->mIndex = index;
} }
return PR_TRUE; return PR_TRUE;
} }
inline PRInt32 inline PRInt32
LookupLowercasedKeyword(const nsACString& aLowercasedKeyword, LookupLowercasedKeyword(const nsACString& aLowercasedKeyword,
nsHashtable* aTable) PLDHashTable& aTable)
{ {
const nsPromiseFlatCString& flatString = PromiseFlatCString(aLowercasedKeyword); const nsPromiseFlatCString& flatString = PromiseFlatCString(aLowercasedKeyword);
nsCStringKey key(flatString); nameTableEntry *entry =
NS_STATIC_CAST(nameTableEntry*,
void* val = aTable->Get(&key); PL_DHashTableOperate(&aTable, flatString.get(), PL_DHASH_LOOKUP));
return val ? NS_PTR_TO_INT32(val) - 1 : if (PL_DHASH_ENTRY_IS_FREE(entry))
nsStaticCaseInsensitiveNameTable::NOT_FOUND; return nsStaticCaseInsensitiveNameTable::NOT_FOUND;
return entry->mIndex;
} }
PRInt32 PRInt32
nsStaticCaseInsensitiveNameTable::Lookup(const nsACString& aName) nsStaticCaseInsensitiveNameTable::Lookup(const nsACString& aName)
{ {
NS_ASSERTION(mNameArray, "not inited"); NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable, "not inited"); NS_ASSERTION(mNameTable.ops, "not inited");
NS_ASSERTION(mCount, "not inited"); NS_ASSERTION(mCount, "not inited");
nsCAutoString strLower(aName); nsCAutoString strLower(aName);
@ -120,7 +173,7 @@ PRInt32
nsStaticCaseInsensitiveNameTable::Lookup(const nsAString& aName) nsStaticCaseInsensitiveNameTable::Lookup(const nsAString& aName)
{ {
NS_ASSERTION(mNameArray, "not inited"); NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable, "not inited"); NS_ASSERTION(mNameTable.ops, "not inited");
NS_ASSERTION(mCount, "not inited"); NS_ASSERTION(mCount, "not inited");
nsCAutoString strLower; nsCAutoString strLower;
@ -133,7 +186,7 @@ const nsAFlatCString&
nsStaticCaseInsensitiveNameTable::GetStringValue(PRInt32 index) nsStaticCaseInsensitiveNameTable::GetStringValue(PRInt32 index)
{ {
NS_ASSERTION(mNameArray, "not inited"); NS_ASSERTION(mNameArray, "not inited");
NS_ASSERTION(mNameTable, "not inited"); NS_ASSERTION(mNameTable.ops, "not inited");
NS_ASSERTION(mCount, "not inited"); NS_ASSERTION(mCount, "not inited");
if ((NOT_FOUND < index) && (index < mCount)) { if ((NOT_FOUND < index) && (index < mCount)) {

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

@ -37,8 +37,7 @@
#ifndef nsStaticNameTable_h___ #ifndef nsStaticNameTable_h___
#define nsStaticNameTable_h___ #define nsStaticNameTable_h___
#include "nsHashtable.h" #include "pldhash.h"
/* This class supports case insensitve lookup. /* This class supports case insensitve lookup.
* *
* It differs from atom tables: * It differs from atom tables:
@ -70,7 +69,7 @@ public:
private: private:
nsDependentCString* mNameArray; nsDependentCString* mNameArray;
nsHashtable* mNameTable; PLDHashTable mNameTable;
PRInt32 mCount; PRInt32 mCount;
nsDependentCString mNullStr; nsDependentCString mNullStr;
}; };