Bug 1129786 (part 2) - Instantiate nsDOMAttributeMap::mAttributeCache eagerly. r=bz.

Now that empty PLDHashTables don't allocate any entry storage, we can undo the
lazification of nsDOMAttributeMap::mAttributeCache from bug 1059056.
This commit is contained in:
Nicholas Nethercote 2015-02-03 00:03:28 -08:00
parent b9f87624af
commit 562969d4cd
2 changed files with 29 additions and 52 deletions

View File

@ -51,17 +51,13 @@ RemoveMapRef(nsAttrHashKey::KeyType aKey, nsRefPtr<Attr>& aData,
nsDOMAttributeMap::~nsDOMAttributeMap()
{
if (mAttributeCache) {
mAttributeCache->Enumerate(RemoveMapRef, nullptr);
}
mAttributeCache.Enumerate(RemoveMapRef, nullptr);
}
void
nsDOMAttributeMap::DropReference()
{
if (mAttributeCache) {
mAttributeCache->Enumerate(RemoveMapRef, nullptr);
}
mAttributeCache.Enumerate(RemoveMapRef, nullptr);
mContent = nullptr;
}
@ -87,9 +83,7 @@ TraverseMapEntry(nsAttrHashKey::KeyType aKey, nsRefPtr<Attr>& aData,
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMAttributeMap)
if (tmp->mAttributeCache) {
tmp->mAttributeCache->Enumerate(TraverseMapEntry, &cb);
}
tmp->mAttributeCache.Enumerate(TraverseMapEntry, &cb);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
@ -143,10 +137,9 @@ SetOwnerDocumentFunc(nsAttrHashKey::KeyType aKey,
nsresult
nsDOMAttributeMap::SetOwnerDocument(nsIDocument* aDocument)
{
if (mAttributeCache) {
uint32_t n = mAttributeCache->Enumerate(SetOwnerDocumentFunc, aDocument);
NS_ENSURE_TRUE(n == mAttributeCache->Count(), NS_ERROR_FAILURE);
}
uint32_t n = mAttributeCache.Enumerate(SetOwnerDocumentFunc, aDocument);
NS_ENSURE_TRUE(n == mAttributeCache.Count(), NS_ERROR_FAILURE);
return NS_OK;
}
@ -154,15 +147,13 @@ void
nsDOMAttributeMap::DropAttribute(int32_t aNamespaceID, nsIAtom* aLocalName)
{
nsAttrKey attr(aNamespaceID, aLocalName);
if (mAttributeCache) {
Attr *node = mAttributeCache->GetWeak(attr);
if (node) {
// Break link to map
node->SetMap(nullptr);
Attr *node = mAttributeCache.GetWeak(attr);
if (node) {
// Break link to map
node->SetMap(nullptr);
// Remove from cache
mAttributeCache->Remove(attr);
}
// Remove from cache
mAttributeCache.Remove(attr);
}
}
@ -174,13 +165,7 @@ nsDOMAttributeMap::RemoveAttribute(mozilla::dom::NodeInfo* aNodeInfo)
nsAttrKey attr(aNodeInfo->NamespaceID(), aNodeInfo->NameAtom());
nsRefPtr<Attr> node;
if (mAttributeCache && mAttributeCache->Get(attr, getter_AddRefs(node))) {
// Break link to map
node->SetMap(nullptr);
// Remove from cache
mAttributeCache->Remove(attr);
} else {
if (!mAttributeCache.Get(attr, getter_AddRefs(node))) {
nsAutoString value;
// As we are removing the attribute we need to set the current value in
// the attribute node.
@ -188,6 +173,13 @@ nsDOMAttributeMap::RemoveAttribute(mozilla::dom::NodeInfo* aNodeInfo)
nsRefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
node = new Attr(nullptr, ni.forget(), value, true);
}
else {
// Break link to map
node->SetMap(nullptr);
// Remove from cache
mAttributeCache.Remove(attr);
}
return node.forget();
}
@ -199,13 +191,12 @@ nsDOMAttributeMap::GetAttribute(mozilla::dom::NodeInfo* aNodeInfo, bool aNsAware
nsAttrKey attr(aNodeInfo->NamespaceID(), aNodeInfo->NameAtom());
EnsureAttributeCache();
Attr* node = mAttributeCache->GetWeak(attr);
Attr* node = mAttributeCache.GetWeak(attr);
if (!node) {
nsRefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
nsRefPtr<Attr> newAttr =
new Attr(this, ni.forget(), EmptyString(), aNsAware);
mAttributeCache->Put(attr, newAttr);
mAttributeCache.Put(attr, newAttr);
node = newAttr;
}
@ -251,14 +242,6 @@ nsDOMAttributeMap::GetNamedItem(const nsAString& aAttrName,
return NS_OK;
}
void
nsDOMAttributeMap::EnsureAttributeCache()
{
if (!mAttributeCache) {
mAttributeCache = MakeUnique<AttrCache>();
}
}
NS_IMETHODIMP
nsDOMAttributeMap::SetNamedItem(nsIDOMAttr* aAttr, nsIDOMAttr** aReturn)
{
@ -365,8 +348,7 @@ nsDOMAttributeMap::SetNamedItemInternal(Attr& aAttr,
// Add the new attribute to the attribute map before updating
// its value in the element. @see bug 364413.
nsAttrKey attrkey(ni->NamespaceID(), ni->NameAtom());
EnsureAttributeCache();
mAttributeCache->Put(attrkey, &aAttr);
mAttributeCache.Put(attrkey, &aAttr);
aAttr.SetMap(this);
rv = mContent->SetAttr(ni->NamespaceID(), ni->NameAtom(),
@ -554,14 +536,14 @@ nsDOMAttributeMap::RemoveNamedItemNS(const nsAString& aNamespaceURI,
uint32_t
nsDOMAttributeMap::Count() const
{
return mAttributeCache ? mAttributeCache->Count() : 0;
return mAttributeCache.Count();
}
uint32_t
nsDOMAttributeMap::Enumerate(AttrCache::EnumReadFunction aFunc,
void *aUserArg) const
{
return mAttributeCache ? mAttributeCache->EnumerateRead(aFunc, aUserArg) : 0;
return mAttributeCache.EnumerateRead(aFunc, aUserArg);
}
size_t
@ -577,10 +559,8 @@ size_t
nsDOMAttributeMap::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
{
size_t n = aMallocSizeOf(this);
n += mAttributeCache
? mAttributeCache->SizeOfExcludingThis(AttrCacheSizeEnumerator,
aMallocSizeOf)
: 0;
n += mAttributeCache.SizeOfExcludingThis(AttrCacheSizeEnumerator,
aMallocSizeOf);
// NB: mContent is non-owning and thus not counted.
return n;

View File

@ -11,7 +11,6 @@
#define nsDOMAttributeMap_h
#include "mozilla/MemoryReporting.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/Attr.h"
#include "mozilla/ErrorResult.h"
#include "nsCycleCollectionParticipant.h"
@ -186,11 +185,9 @@ private:
nsCOMPtr<Element> mContent;
/**
* Cache of Attrs. It's usually empty, and thus initialized lazily.
* Cache of Attrs.
*/
mozilla::UniquePtr<AttrCache> mAttributeCache;
void EnsureAttributeCache();
AttrCache mAttributeCache;
/**
* SetNamedItem() (aWithNS = false) and SetNamedItemNS() (aWithNS =