From e3476d33ed811937c296711c79d2e2db1f9456ac Mon Sep 17 00:00:00 2001 From: Francis Dominic Fajardo Date: Tue, 22 Jul 2025 12:29:41 +0800 Subject: [PATCH] Issue #2828 - Part 12: Implement cascade layer SizeOfIncludingThis --- layout/style/CascadeLayerRuleProcessor.cpp | 5 ++-- layout/style/RuleCascadeData.cpp | 28 ++++++++++++++++++++++ layout/style/RuleCascadeData.h | 2 ++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/layout/style/CascadeLayerRuleProcessor.cpp b/layout/style/CascadeLayerRuleProcessor.cpp index 897a010280..b5652b7423 100644 --- a/layout/style/CascadeLayerRuleProcessor.cpp +++ b/layout/style/CascadeLayerRuleProcessor.cpp @@ -223,8 +223,9 @@ CascadeLayerRuleProcessor::SizeOfExcludingThis( mozilla::MallocSizeOf aMallocSizeOf) const { size_t n = 0; - n += mCascade->SizeOfIncludingThis(aMallocSizeOf); - // FIXME: size of attached cascade layer is not included. + // The cascade layer owns the rule cascade, so we don't count it here. + // We do count the layer itself, though. + n += mLayer->SizeOfIncludingThis(aMallocSizeOf); return n; } diff --git a/layout/style/RuleCascadeData.cpp b/layout/style/RuleCascadeData.cpp index 465225d792..3a78e3e84e 100644 --- a/layout/style/RuleCascadeData.cpp +++ b/layout/style/RuleCascadeData.cpp @@ -1642,6 +1642,34 @@ CascadeLayer::~CascadeLayer() delete mData; } +size_t +CascadeLayer::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const +{ + size_t n = aMallocSizeOf(this); + if (mData) { + n += mData->SizeOfIncludingThis(aMallocSizeOf); + } + n += mName.SizeOfExcludingThisIfUnshared(aMallocSizeOf); + n += mStyleRules.ShallowSizeOfExcludingThis(aMallocSizeOf); + // While we do create the child layers, they are not owned by us, so we + // don't count them. Their ownership is managed by the rule processor + // to which they are eventually attached (see nsCSSRuleProcessor). + n += mPreLayers.ShallowSizeOfExcludingThis(aMallocSizeOf); + n += mPostLayers.ShallowSizeOfExcludingThis(aMallocSizeOf); + n += mLayers.ShallowSizeOfExcludingThis(aMallocSizeOf); + for (auto iter = mLayers.ConstIter(); !iter.Done(); iter.Next()) { + // We don't own the CascadeLayer objects so we don't count them. We + // do care about the size of the keys' nsAString members' buffers though. + // + // Note that we depend on nsStringHashKey::GetKey() returning a reference, + // since otherwise aKey would be a copy of the string key and we would not + // be measuring the right object here. + n += iter.Key().SizeOfExcludingThisIfUnshared(aMallocSizeOf); + } + + return n; +} + CascadeLayer* CascadeLayer::CreateNamedChildLayer(const nsTArray& aPath) { diff --git a/layout/style/RuleCascadeData.h b/layout/style/RuleCascadeData.h index 1986377924..1a8b4efd07 100644 --- a/layout/style/RuleCascadeData.h +++ b/layout/style/RuleCascadeData.h @@ -336,6 +336,8 @@ struct CascadeLayer nsMediaQueryResultCacheKey& aCacheKey); ~CascadeLayer(); + size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const; + nsPresContext* mPresContext; nsString mName; bool mIsAnonymous;