mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
1ccfbe810b
This patch enables sharing of an nsAttrValue's MiscContainer between nodes for style rules. MiscContainers of type eCSSStyleRule are now refcounted (with some clever struct packing to ensure that the amount of memory allocated for MiscContainer remains unchanged on 32 and 64 bit). This infrastructure can be used to share most MiscContainer types in the future if we find advantages to sharing other types than just eCSSStyleRuley. A cache mapping strings to MiscContainers has been added to nsHTMLCSSStyleSheet. MiscContainers can be shared between nsAttrValues when one nsAttrValue is SetTo another nsAttrValue or when there is a cache hit in this cache. This patch also adds the ability to tell a style rule that it belongs to an nsHTMLCSSStyleSheet, with appropriate accessor functions to separate that from the existing case of belonging to an nsCSSStyleSheet. The primary use case is to reduce memory use for pages that have lots of inline style attributes with the same value. This can happen easily with large pages that are automatically generated. An (admittedly pathological) testcase in Bug 686975 sees over 250 MB of memory savings with this change. Reusing the same MiscContainer for multiple nodes saves the overhead of maintaining separate copies of the string containing the serialized value of the style attribute and of creating separate style rules for each node. Eliminating duplicate style rules enables further savings in layout through style context sharing. The testcase sees the amount of memory used by style contexts go from over 250 MB to 10 KB. Because the cache is based on the text value of the style attribute, it will not handle attributes that have different text values but are parsed into identical style rules. We also do not attempt to share MiscContainers when the node's base URI differs from the document URI. The effect of these limitations is expected to be low.
291 lines
6.7 KiB
C++
291 lines
6.7 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
/*
|
|
* style sheet and style rule processor representing style attributes
|
|
*/
|
|
|
|
#include "nsHTMLCSSStyleSheet.h"
|
|
#include "nsCRT.h"
|
|
#include "nsIAtom.h"
|
|
#include "nsIURL.h"
|
|
#include "nsCSSPseudoElements.h"
|
|
#include "nsIStyleRule.h"
|
|
#include "mozilla/css/StyleRule.h"
|
|
#include "nsIStyleRuleProcessor.h"
|
|
#include "nsPresContext.h"
|
|
#include "nsIDocument.h"
|
|
#include "nsCOMPtr.h"
|
|
#include "nsRuleWalker.h"
|
|
#include "nsRuleData.h"
|
|
#include "nsRuleProcessorData.h"
|
|
#include "mozilla/dom/Element.h"
|
|
#include "nsAttrValue.h"
|
|
#include "nsAttrValueInlines.h"
|
|
|
|
using namespace mozilla::dom;
|
|
namespace css = mozilla::css;
|
|
|
|
namespace {
|
|
|
|
PLDHashOperator
|
|
ClearAttrCache(const nsAString& aKey, MiscContainer*& aValue, void*)
|
|
{
|
|
// Ideally we'd just call MiscContainer::Evict, but we can't do that since
|
|
// we're iterating the hashtable.
|
|
MOZ_ASSERT(aValue->mType == nsAttrValue::eCSSStyleRule);
|
|
|
|
aValue->mValue.mCSSStyleRule->SetHTMLCSSStyleSheet(nullptr);
|
|
aValue->mValue.mCached = 0;
|
|
|
|
return PL_DHASH_REMOVE;
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
nsHTMLCSSStyleSheet::nsHTMLCSSStyleSheet()
|
|
: mDocument(nullptr)
|
|
{
|
|
}
|
|
|
|
nsHTMLCSSStyleSheet::~nsHTMLCSSStyleSheet()
|
|
{
|
|
// We may go away before all of our cached style attributes do,
|
|
// so clean up any that are left.
|
|
mCachedStyleAttrs.Enumerate(ClearAttrCache, nullptr);
|
|
}
|
|
|
|
NS_IMPL_ISUPPORTS2(nsHTMLCSSStyleSheet,
|
|
nsIStyleSheet,
|
|
nsIStyleRuleProcessor)
|
|
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::RulesMatching(ElementRuleProcessorData* aData)
|
|
{
|
|
Element* element = aData->mElement;
|
|
|
|
// just get the one and only style rule from the content's STYLE attribute
|
|
css::StyleRule* rule = element->GetInlineStyleRule();
|
|
if (rule) {
|
|
rule->RuleMatched();
|
|
aData->mRuleWalker->Forward(rule);
|
|
}
|
|
|
|
rule = element->GetSMILOverrideStyleRule();
|
|
if (rule) {
|
|
if (aData->mPresContext->IsProcessingRestyles() &&
|
|
!aData->mPresContext->IsProcessingAnimationStyleChange()) {
|
|
// Non-animation restyle -- don't process SMIL override style, because we
|
|
// don't want SMIL animation to trigger new CSS transitions. Instead,
|
|
// request an Animation restyle, so we still get noticed.
|
|
aData->mPresContext->PresShell()->RestyleForAnimation(element,
|
|
eRestyle_Self);
|
|
} else {
|
|
// Animation restyle (or non-restyle traversal of rules)
|
|
// Now we can walk SMIL overrride style, without triggering transitions.
|
|
rule->RuleMatched();
|
|
aData->mRuleWalker->Forward(rule);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::RulesMatching(PseudoElementRuleProcessorData* aData)
|
|
{
|
|
}
|
|
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::RulesMatching(AnonBoxRuleProcessorData* aData)
|
|
{
|
|
}
|
|
|
|
#ifdef MOZ_XUL
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::RulesMatching(XULTreeRuleProcessorData* aData)
|
|
{
|
|
}
|
|
#endif
|
|
|
|
nsresult
|
|
nsHTMLCSSStyleSheet::Init(nsIURI* aURL, nsIDocument* aDocument)
|
|
{
|
|
NS_PRECONDITION(aURL && aDocument, "null ptr");
|
|
if (! aURL || ! aDocument)
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
if (mURL || mDocument)
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
mDocument = aDocument; // not refcounted!
|
|
mURL = aURL;
|
|
mCachedStyleAttrs.Init();
|
|
return NS_OK;
|
|
}
|
|
|
|
// Test if style is dependent on content state
|
|
/* virtual */ nsRestyleHint
|
|
nsHTMLCSSStyleSheet::HasStateDependentStyle(StateRuleProcessorData* aData)
|
|
{
|
|
return nsRestyleHint(0);
|
|
}
|
|
|
|
/* virtual */ bool
|
|
nsHTMLCSSStyleSheet::HasDocumentStateDependentStyle(StateRuleProcessorData* aData)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Test if style is dependent on attribute
|
|
/* virtual */ nsRestyleHint
|
|
nsHTMLCSSStyleSheet::HasAttributeDependentStyle(AttributeRuleProcessorData* aData)
|
|
{
|
|
// Perhaps should check that it's XUL, SVG, (or HTML) namespace, but
|
|
// it doesn't really matter.
|
|
if (aData->mAttrHasChanged && aData->mAttribute == nsGkAtoms::style) {
|
|
return eRestyle_Self;
|
|
}
|
|
|
|
return nsRestyleHint(0);
|
|
}
|
|
|
|
/* virtual */ bool
|
|
nsHTMLCSSStyleSheet::MediumFeaturesChanged(nsPresContext* aPresContext)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
/* virtual */ size_t
|
|
nsHTMLCSSStyleSheet::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf) const
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
/* virtual */ size_t
|
|
nsHTMLCSSStyleSheet::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const
|
|
{
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
}
|
|
|
|
void
|
|
nsHTMLCSSStyleSheet::CacheStyleAttr(const nsAString& aSerialized,
|
|
MiscContainer* aValue)
|
|
{
|
|
mCachedStyleAttrs.Put(aSerialized, aValue);
|
|
}
|
|
|
|
void
|
|
nsHTMLCSSStyleSheet::EvictStyleAttr(const nsAString& aSerialized,
|
|
MiscContainer* aValue)
|
|
{
|
|
#ifdef DEBUG
|
|
{
|
|
NS_ASSERTION(aValue = mCachedStyleAttrs.Get(aSerialized),
|
|
"Cached value does not match?!");
|
|
}
|
|
#endif
|
|
mCachedStyleAttrs.Remove(aSerialized);
|
|
}
|
|
|
|
MiscContainer*
|
|
nsHTMLCSSStyleSheet::LookupStyleAttr(const nsAString& aSerialized)
|
|
{
|
|
return mCachedStyleAttrs.Get(aSerialized);
|
|
}
|
|
|
|
void
|
|
nsHTMLCSSStyleSheet::Reset(nsIURI* aURL)
|
|
{
|
|
mURL = aURL;
|
|
}
|
|
|
|
/* virtual */ nsIURI*
|
|
nsHTMLCSSStyleSheet::GetSheetURI() const
|
|
{
|
|
return mURL;
|
|
}
|
|
|
|
/* virtual */ nsIURI*
|
|
nsHTMLCSSStyleSheet::GetBaseURI() const
|
|
{
|
|
return mURL;
|
|
}
|
|
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::GetTitle(nsString& aTitle) const
|
|
{
|
|
aTitle.AssignLiteral("Internal HTML/CSS Style Sheet");
|
|
}
|
|
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::GetType(nsString& aType) const
|
|
{
|
|
aType.AssignLiteral("text/html");
|
|
}
|
|
|
|
/* virtual */ bool
|
|
nsHTMLCSSStyleSheet::HasRules() const
|
|
{
|
|
// Say we always have rules, since we don't know.
|
|
return true;
|
|
}
|
|
|
|
/* virtual */ bool
|
|
nsHTMLCSSStyleSheet::IsApplicable() const
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::SetEnabled(bool aEnabled)
|
|
{ // these can't be disabled
|
|
}
|
|
|
|
/* virtual */ bool
|
|
nsHTMLCSSStyleSheet::IsComplete() const
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::SetComplete()
|
|
{
|
|
}
|
|
|
|
// style sheet owner info
|
|
/* virtual */ nsIStyleSheet*
|
|
nsHTMLCSSStyleSheet::GetParentSheet() const
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
/* virtual */ nsIDocument*
|
|
nsHTMLCSSStyleSheet::GetOwningDocument() const
|
|
{
|
|
return mDocument;
|
|
}
|
|
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::SetOwningDocument(nsIDocument* aDocument)
|
|
{
|
|
mDocument = aDocument;
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
/* virtual */ void
|
|
nsHTMLCSSStyleSheet::List(FILE* out, int32_t aIndent) const
|
|
{
|
|
// Indent
|
|
for (int32_t index = aIndent; --index >= 0; ) fputs(" ", out);
|
|
|
|
fputs("HTML CSS Style Sheet: ", out);
|
|
nsAutoCString urlSpec;
|
|
mURL->GetSpec(urlSpec);
|
|
if (!urlSpec.IsEmpty()) {
|
|
fputs(urlSpec.get(), out);
|
|
}
|
|
fputs("\n", out);
|
|
}
|
|
#endif
|