mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-15 21:28:31 +00:00
Bug 1355787, nsIdentifierMapEntry should let one to use either strings or atoms as keys to avoid slow string assignments when possible. r=nfroyd
--HG-- extra : rebase_source : 3e8c53462e4ed19ecaf611b7618a132d8606c0ae
This commit is contained in:
parent
43cc7ae337
commit
66df8dc927
@ -206,8 +206,7 @@ ShadowRoot::GetElementsByTagNameNS(const nsAString& aNamespaceURI,
|
||||
void
|
||||
ShadowRoot::AddToIdTable(Element* aElement, nsIAtom* aId)
|
||||
{
|
||||
nsIdentifierMapEntry *entry =
|
||||
mIdentifierMap.PutEntry(nsDependentAtomString(aId));
|
||||
nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aId);
|
||||
if (entry) {
|
||||
entry->AddIdElement(aElement);
|
||||
}
|
||||
@ -216,8 +215,7 @@ ShadowRoot::AddToIdTable(Element* aElement, nsIAtom* aId)
|
||||
void
|
||||
ShadowRoot::RemoveFromIdTable(Element* aElement, nsIAtom* aId)
|
||||
{
|
||||
nsIdentifierMapEntry *entry =
|
||||
mIdentifierMap.GetEntry(nsDependentAtomString(aId));
|
||||
nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aId);
|
||||
if (entry) {
|
||||
entry->RemoveIdElement(aElement);
|
||||
if (entry->IsEmpty()) {
|
||||
|
@ -529,7 +529,7 @@ nsIdentifierMapEntry::HasIdElementExposedAsHTMLDocumentProperty()
|
||||
size_t
|
||||
nsIdentifierMapEntry::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
||||
{
|
||||
return nsStringHashKey::SizeOfExcludingThis(aMallocSizeOf);
|
||||
return mKey.mString.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
||||
}
|
||||
|
||||
// Helper structs for the content->subdoc map
|
||||
@ -2848,8 +2848,7 @@ nsDocument::AddToNameTable(Element *aElement, nsIAtom* aName)
|
||||
"Only put elements that need to be exposed as document['name'] in "
|
||||
"the named table.");
|
||||
|
||||
nsIdentifierMapEntry *entry =
|
||||
mIdentifierMap.PutEntry(nsDependentAtomString(aName));
|
||||
nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aName);
|
||||
|
||||
// Null for out-of-memory
|
||||
if (entry) {
|
||||
@ -2868,8 +2867,7 @@ nsDocument::RemoveFromNameTable(Element *aElement, nsIAtom* aName)
|
||||
if (mIdentifierMap.Count() == 0)
|
||||
return;
|
||||
|
||||
nsIdentifierMapEntry *entry =
|
||||
mIdentifierMap.GetEntry(nsDependentAtomString(aName));
|
||||
nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aName);
|
||||
if (!entry) // Could be false if the element was anonymous, hence never added
|
||||
return;
|
||||
|
||||
@ -2883,8 +2881,7 @@ nsDocument::RemoveFromNameTable(Element *aElement, nsIAtom* aName)
|
||||
void
|
||||
nsDocument::AddToIdTable(Element *aElement, nsIAtom* aId)
|
||||
{
|
||||
nsIdentifierMapEntry *entry =
|
||||
mIdentifierMap.PutEntry(nsDependentAtomString(aId));
|
||||
nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aId);
|
||||
|
||||
if (entry) { /* True except on OOM */
|
||||
if (nsGenericHTMLElement::ShouldExposeIdAsHTMLDocumentProperty(aElement) &&
|
||||
@ -2906,8 +2903,7 @@ nsDocument::RemoveFromIdTable(Element *aElement, nsIAtom* aId)
|
||||
return;
|
||||
}
|
||||
|
||||
nsIdentifierMapEntry *entry =
|
||||
mIdentifierMap.GetEntry(nsDependentAtomString(aId));
|
||||
nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aId);
|
||||
if (!entry) // Can be null for XML elements with changing ids.
|
||||
return;
|
||||
|
||||
@ -5123,7 +5119,7 @@ nsDocument::AddIDTargetObserver(nsIAtom* aID, IDTargetObserver aObserver,
|
||||
if (!CheckGetElementByIdArg(id))
|
||||
return nullptr;
|
||||
|
||||
nsIdentifierMapEntry *entry = mIdentifierMap.PutEntry(id);
|
||||
nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aID);
|
||||
NS_ENSURE_TRUE(entry, nullptr);
|
||||
|
||||
entry->AddContentChangeCallback(aObserver, aData, aForImage);
|
||||
@ -5139,7 +5135,7 @@ nsDocument::RemoveIDTargetObserver(nsIAtom* aID, IDTargetObserver aObserver,
|
||||
if (!CheckGetElementByIdArg(id))
|
||||
return;
|
||||
|
||||
nsIdentifierMapEntry *entry = mIdentifierMap.GetEntry(id);
|
||||
nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aID);
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
|
@ -143,22 +143,45 @@ public:
|
||||
* Perhaps the document.all results should have their own hashtable
|
||||
* in nsHTMLDocument.
|
||||
*/
|
||||
class nsIdentifierMapEntry : public nsStringHashKey
|
||||
class nsIdentifierMapEntry : public PLDHashEntryHdr
|
||||
{
|
||||
public:
|
||||
struct AtomOrString
|
||||
{
|
||||
MOZ_IMPLICIT AtomOrString(nsIAtom* aAtom) : mAtom(aAtom) {}
|
||||
MOZ_IMPLICIT AtomOrString(const nsAString& aString) : mString(aString) {}
|
||||
AtomOrString(const AtomOrString& aOther)
|
||||
: mAtom(aOther.mAtom)
|
||||
, mString(aOther.mString)
|
||||
{
|
||||
}
|
||||
|
||||
AtomOrString(AtomOrString&& aOther)
|
||||
: mAtom(aOther.mAtom.forget())
|
||||
, mString(aOther.mString)
|
||||
{
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIAtom> mAtom;
|
||||
const nsString mString;
|
||||
};
|
||||
|
||||
typedef const AtomOrString& KeyType;
|
||||
typedef const AtomOrString* KeyTypePointer;
|
||||
|
||||
typedef mozilla::dom::Element Element;
|
||||
typedef mozilla::net::ReferrerPolicy ReferrerPolicy;
|
||||
|
||||
explicit nsIdentifierMapEntry(const nsAString& aKey) :
|
||||
nsStringHashKey(&aKey), mNameContentList(nullptr)
|
||||
explicit nsIdentifierMapEntry(const AtomOrString& aKey)
|
||||
: mKey(aKey)
|
||||
{
|
||||
}
|
||||
explicit nsIdentifierMapEntry(const nsAString* aKey) :
|
||||
nsStringHashKey(aKey), mNameContentList(nullptr)
|
||||
explicit nsIdentifierMapEntry(const AtomOrString* aKey)
|
||||
: mKey(aKey ? *aKey : nullptr)
|
||||
{
|
||||
}
|
||||
nsIdentifierMapEntry(nsIdentifierMapEntry&& aOther) :
|
||||
nsStringHashKey(&aOther.GetKey()),
|
||||
mKey(mozilla::Move(aOther.GetKey())),
|
||||
mIdContentList(mozilla::Move(aOther.mIdContentList)),
|
||||
mNameContentList(aOther.mNameContentList.forget()),
|
||||
mChangeCallbacks(aOther.mChangeCallbacks.forget()),
|
||||
@ -167,6 +190,42 @@ public:
|
||||
}
|
||||
~nsIdentifierMapEntry();
|
||||
|
||||
KeyType GetKey() const { return mKey; }
|
||||
|
||||
nsString GetKeyAsString() const
|
||||
{
|
||||
if (mKey.mAtom) {
|
||||
return nsAtomString(mKey.mAtom);
|
||||
}
|
||||
|
||||
return mKey.mString;
|
||||
}
|
||||
|
||||
bool KeyEquals(const KeyTypePointer aOtherKey) const
|
||||
{
|
||||
if (mKey.mAtom) {
|
||||
if (aOtherKey->mAtom) {
|
||||
return mKey.mAtom == aOtherKey->mAtom;
|
||||
}
|
||||
|
||||
return mKey.mAtom->Equals(aOtherKey->mString);
|
||||
}
|
||||
|
||||
if (aOtherKey->mAtom) {
|
||||
return aOtherKey->mAtom->Equals(mKey.mString);
|
||||
}
|
||||
|
||||
return mKey.mString.Equals(aOtherKey->mString);
|
||||
}
|
||||
|
||||
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
||||
|
||||
static PLDHashNumber HashKey(const KeyTypePointer aKey)
|
||||
{
|
||||
return aKey->mAtom ?
|
||||
aKey->mAtom->hash() : mozilla::HashString(aKey->mString);
|
||||
}
|
||||
|
||||
enum { ALLOW_MEMMOVE = false };
|
||||
|
||||
void AddNameElement(nsINode* aDocument, Element* aElement);
|
||||
@ -265,6 +324,7 @@ private:
|
||||
void FireChangeCallbacks(Element* aOldElement, Element* aNewElement,
|
||||
bool aImageOnly = false);
|
||||
|
||||
AtomOrString mKey;
|
||||
// empty if there are no elements with this ID.
|
||||
// The elements are stored as weak pointers.
|
||||
AutoTArray<Element*, 1> mIdContentList;
|
||||
|
@ -2312,7 +2312,7 @@ nsHTMLDocument::GetSupportedNames(nsTArray<nsString>& aNames)
|
||||
nsIdentifierMapEntry* entry = iter.Get();
|
||||
if (entry->HasNameElement() ||
|
||||
entry->HasIdElementExposedAsHTMLDocumentProperty()) {
|
||||
aNames.AppendElement(entry->GetKey());
|
||||
aNames.AppendElement(entry->GetKeyAsString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user