Once we've had enough ID lookup misses, make the ID table completely live. Bug

299689, r+sr=jst
This commit is contained in:
bzbarsky%mit.edu 2005-09-07 17:04:34 +00:00
parent cf64715ae5
commit 811b4e530c
2 changed files with 43 additions and 7 deletions

View File

@ -2377,12 +2377,25 @@ nsHTMLDocument::GetElementById(const nsAString& aElementId,
}
if (!e) {
NS_WARN_IF_FALSE(!aElementId.IsEmpty(),
"getElementById(\"\") called, fix caller?");
// If IdTableIsLive(), no need to look for the element in the document,
// since we're fully maintaining our table's state as the DOM mutates.
if (!IdTableIsLive()) {
if (IdTableShouldBecomeLive()) {
// Just make sure our table is up to date and call this method again
// to look up in the hashtable.
if (mRootContent) {
RegisterNamedItems(mRootContent);
}
return GetElementById(aElementId, aReturn);
}
if (mRootContent && !aElementId.IsEmpty()) {
e = MatchElementId(mRootContent, NS_ConvertUCS2toUTF8(aElementId),
aElementId);
NS_WARN_IF_FALSE(!aElementId.IsEmpty(),
"getElementById(\"\") called, fix caller?");
if (mRootContent && !aElementId.IsEmpty()) {
e = MatchElementId(mRootContent, NS_ConvertUCS2toUTF8(aElementId),
aElementId);
}
}
if (!e) {
@ -3015,12 +3028,14 @@ nsHTMLDocument::AddToIdTable(const nsAString& aId, nsIContent *aContent)
nsresult
nsHTMLDocument::UpdateIdTableEntry(const nsAString& aId, nsIContent *aContent)
{
PRBool liveTable = IdTableIsLive();
PLDHashOperator op = liveTable ? PL_DHASH_ADD : PL_DHASH_LOOKUP;
IdAndNameMapEntry *entry =
NS_STATIC_CAST(IdAndNameMapEntry *,
PL_DHashTableOperate(&mIdAndNameHashTable, &aId,
PL_DHASH_LOOKUP));
op));
if (PL_DHASH_ENTRY_IS_BUSY(entry)) {
if (entry && (liveTable || PL_DHASH_ENTRY_IS_BUSY(entry))) {
entry->mIdContent = aContent;
}

View File

@ -310,6 +310,27 @@ protected:
PRPackedBool mIsFrameset;
PRBool IdTableIsLive() const {
// live if we've had over 63 misses
return (mIdMissCount & 0x40) != 0;
}
PRBool IdTableShouldBecomeLive() {
NS_ASSERTION(!IdTableIsLive(),
"Shouldn't be called if table is already live!");
++mIdMissCount;
return IdTableIsLive();
}
PRUint8 mIdMissCount;
/* mIdAndNameHashTable works as follows for IDs:
* 1) Attribute changes affect the table immediately (removing and adding
* entries as needed).
* 2) Removals from the DOM affect the table immediately
* 3) Additions to the DOM always update existing entries, but only add new
* ones if IdTableIsLive() is true.
*/
PLDHashTable mIdAndNameHashTable;
nsCOMPtr<nsIWyciwygChannel> mWyciwygChannel;