Bug 1844755 - Consider to not create ServoStyleSet for data documents, r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D191485
This commit is contained in:
Olli Pettay 2023-10-23 16:10:41 +00:00
Родитель ab9f35e995
Коммит fa8f7f9a3f
9 изменённых файлов: 81 добавлений и 57 удалений

Просмотреть файл

@ -7,6 +7,7 @@
#include "mozilla/dom/KeyframeEffect.h"
#include "mozilla/dom/Animation.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/KeyframeAnimationOptionsBinding.h"
// For UnrestrictedDoubleOrKeyframeAnimationOptions;
#include "mozilla/dom/KeyframeEffectBinding.h"
@ -1146,7 +1147,7 @@ static void CreatePropertyValue(
void KeyframeEffect::GetProperties(
nsTArray<AnimationPropertyDetails>& aProperties, ErrorResult& aRv) const {
const StylePerDocumentStyleData* rawData =
mDocument->StyleSetForPresShellOrMediaQueryEvaluation()->RawData();
mDocument->EnsureStyleSet().RawData();
for (const AnimationProperty& property : mProperties) {
AnimationPropertyDetails propertyDetails;
@ -1252,7 +1253,7 @@ void KeyframeEffect::GetKeyframes(JSContext* aCx, nsTArray<JSObject*>& aResult,
}
const StylePerDocumentStyleData* rawData =
mDocument->StyleSetForPresShellOrMediaQueryEvaluation()->RawData();
mDocument->EnsureStyleSet().RawData();
for (const Keyframe& keyframe : mKeyframes) {
// Set up a dictionary object for the explicit members

Просмотреть файл

@ -106,7 +106,6 @@
#include "mozilla/ScopeExit.h"
#include "mozilla/Components.h"
#include "mozilla/ServoStyleConsts.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/ServoTypes.h"
#include "mozilla/SizeOfState.h"
#include "mozilla/Span.h"
@ -2785,8 +2784,6 @@ nsresult Document::Init(nsIPrincipal* aPrincipal,
mFeaturePolicy = new dom::FeaturePolicy(this);
mFeaturePolicy->SetDefaultOrigin(NodePrincipal());
mStyleSet = MakeUnique<ServoStyleSet>(*this);
if (aPrincipal) {
SetPrincipals(aPrincipal, aPartitionedPrincipal);
} else {
@ -3051,14 +3048,15 @@ already_AddRefed<nsIPrincipal> Document::MaybeDowngradePrincipal(
size_t Document::FindDocStyleSheetInsertionPoint(const StyleSheet& aSheet) {
nsStyleSheetService* sheetService = nsStyleSheetService::GetInstance();
ServoStyleSet& styleSet = EnsureStyleSet();
// lowest index first
int32_t newDocIndex = StyleOrderIndexOfSheet(aSheet);
size_t count = mStyleSet->SheetCount(StyleOrigin::Author);
size_t count = styleSet.SheetCount(StyleOrigin::Author);
size_t index = 0;
for (; index < count; index++) {
auto* sheet = mStyleSet->SheetAt(StyleOrigin::Author, index);
auto* sheet = styleSet.SheetAt(StyleOrigin::Author, index);
MOZ_ASSERT(sheet);
int32_t sheetDocIndex = StyleOrderIndexOfSheet(*sheet);
if (sheetDocIndex > newDocIndex) {
@ -3089,12 +3087,13 @@ void Document::ResetStylesheetsToURI(nsIURI* aURI) {
MOZ_ASSERT(aURI);
ClearAdoptedStyleSheets();
ServoStyleSet& styleSet = EnsureStyleSet();
auto ClearSheetList = [&](nsTArray<RefPtr<StyleSheet>>& aSheetList) {
for (auto& sheet : Reversed(aSheetList)) {
sheet->ClearAssociatedDocumentOrShadowRoot();
if (mStyleSetFilled) {
mStyleSet->RemoveStyleSheet(*sheet);
styleSet.RemoveStyleSheet(*sheet);
}
}
aSheetList.Clear();
@ -3108,7 +3107,7 @@ void Document::ResetStylesheetsToURI(nsIURI* aURI) {
for (auto& sheet : Reversed(*ss->AuthorStyleSheets())) {
MOZ_ASSERT(!sheet->GetAssociatedDocumentOrShadowRoot());
if (sheet->IsApplicable()) {
mStyleSet->RemoveStyleSheet(*sheet);
styleSet.RemoveStyleSheet(*sheet);
}
}
}
@ -3125,7 +3124,7 @@ void Document::ResetStylesheetsToURI(nsIURI* aURI) {
if (mStyleSetFilled) {
FillStyleSetDocumentSheets();
if (mStyleSet->StyleSheetsHaveChanged()) {
if (styleSet.StyleSheetsHaveChanged()) {
ApplicableStylesChanged();
}
}
@ -3150,49 +3149,50 @@ void Document::FillStyleSetUserAndUASheets() {
"should never be creating a StyleSet after the style sheet "
"service has gone");
ServoStyleSet& styleSet = EnsureStyleSet();
for (StyleSheet* sheet : *sheetService->UserStyleSheets()) {
mStyleSet->AppendStyleSheet(*sheet);
styleSet.AppendStyleSheet(*sheet);
}
StyleSheet* sheet = IsInChromeDocShell() ? cache->GetUserChromeSheet()
: cache->GetUserContentSheet();
if (sheet) {
mStyleSet->AppendStyleSheet(*sheet);
styleSet.AppendStyleSheet(*sheet);
}
mStyleSet->AppendStyleSheet(*cache->UASheet());
styleSet.AppendStyleSheet(*cache->UASheet());
if (MOZ_LIKELY(NodeInfoManager()->MathMLEnabled())) {
mStyleSet->AppendStyleSheet(*cache->MathMLSheet());
styleSet.AppendStyleSheet(*cache->MathMLSheet());
}
if (MOZ_LIKELY(NodeInfoManager()->SVGEnabled())) {
mStyleSet->AppendStyleSheet(*cache->SVGSheet());
styleSet.AppendStyleSheet(*cache->SVGSheet());
}
mStyleSet->AppendStyleSheet(*cache->HTMLSheet());
styleSet.AppendStyleSheet(*cache->HTMLSheet());
if (nsLayoutUtils::ShouldUseNoFramesSheet(this)) {
mStyleSet->AppendStyleSheet(*cache->NoFramesSheet());
styleSet.AppendStyleSheet(*cache->NoFramesSheet());
}
mStyleSet->AppendStyleSheet(*cache->CounterStylesSheet());
styleSet.AppendStyleSheet(*cache->CounterStylesSheet());
// Only load the full XUL sheet if we'll need it.
if (LoadsFullXULStyleSheetUpFront()) {
mStyleSet->AppendStyleSheet(*cache->XULSheet());
styleSet.AppendStyleSheet(*cache->XULSheet());
}
mStyleSet->AppendStyleSheet(*cache->FormsSheet());
mStyleSet->AppendStyleSheet(*cache->ScrollbarsSheet());
styleSet.AppendStyleSheet(*cache->FormsSheet());
styleSet.AppendStyleSheet(*cache->ScrollbarsSheet());
for (StyleSheet* sheet : *sheetService->AgentStyleSheets()) {
mStyleSet->AppendStyleSheet(*sheet);
styleSet.AppendStyleSheet(*sheet);
}
MOZ_ASSERT(!mQuirkSheetAdded);
if (NeedsQuirksSheet()) {
mStyleSet->AppendStyleSheet(*cache->QuirkSheet());
styleSet.AppendStyleSheet(*cache->QuirkSheet());
mQuirkSheetAdded = true;
}
}
@ -3207,15 +3207,16 @@ void Document::FillStyleSet() {
void Document::RemoveContentEditableStyleSheets() {
MOZ_ASSERT(IsHTMLOrXHTML());
ServoStyleSet& styleSet = EnsureStyleSet();
auto* cache = GlobalStyleSheetCache::Singleton();
bool changed = false;
if (mDesignModeSheetAdded) {
mStyleSet->RemoveStyleSheet(*cache->DesignModeSheet());
styleSet.RemoveStyleSheet(*cache->DesignModeSheet());
mDesignModeSheetAdded = false;
changed = true;
}
if (mContentEditableSheetAdded) {
mStyleSet->RemoveStyleSheet(*cache->ContentEditableSheet());
styleSet.RemoveStyleSheet(*cache->ContentEditableSheet());
mContentEditableSheetAdded = false;
changed = true;
}
@ -3230,18 +3231,19 @@ void Document::AddContentEditableStyleSheetsToStyleSet(bool aDesignMode) {
MOZ_DIAGNOSTIC_ASSERT(mStyleSetFilled,
"Caller should ensure we're being rendered");
ServoStyleSet& styleSet = EnsureStyleSet();
auto* cache = GlobalStyleSheetCache::Singleton();
bool changed = false;
if (!mContentEditableSheetAdded) {
mStyleSet->AppendStyleSheet(*cache->ContentEditableSheet());
styleSet.AppendStyleSheet(*cache->ContentEditableSheet());
mContentEditableSheetAdded = true;
changed = true;
}
if (mDesignModeSheetAdded != aDesignMode) {
if (mDesignModeSheetAdded) {
mStyleSet->RemoveStyleSheet(*cache->DesignModeSheet());
styleSet.RemoveStyleSheet(*cache->DesignModeSheet());
} else {
mStyleSet->AppendStyleSheet(*cache->DesignModeSheet());
styleSet.AppendStyleSheet(*cache->DesignModeSheet());
}
mDesignModeSheetAdded = !mDesignModeSheetAdded;
changed = true;
@ -3252,7 +3254,8 @@ void Document::AddContentEditableStyleSheetsToStyleSet(bool aDesignMode) {
}
void Document::FillStyleSetDocumentSheets() {
MOZ_ASSERT(mStyleSet->SheetCount(StyleOrigin::Author) == 0,
ServoStyleSet& styleSet = EnsureStyleSet();
MOZ_ASSERT(styleSet.SheetCount(StyleOrigin::Author) == 0,
"Style set already has document sheets?");
// Sheets are added in reverse order to avoid worst-case time complexity when
@ -3263,39 +3266,44 @@ void Document::FillStyleSetDocumentSheets() {
// styleset from scratch anyway.
for (StyleSheet* sheet : Reversed(mStyleSheets)) {
if (sheet->IsApplicable()) {
mStyleSet->AddDocStyleSheet(*sheet);
styleSet.AddDocStyleSheet(*sheet);
}
}
EnumerateUniqueAdoptedStyleSheetsBackToFront([&](StyleSheet& aSheet) {
if (aSheet.IsApplicable()) {
mStyleSet->AddDocStyleSheet(aSheet);
styleSet.AddDocStyleSheet(aSheet);
}
});
nsStyleSheetService* sheetService = nsStyleSheetService::GetInstance();
for (StyleSheet* sheet : *sheetService->AuthorStyleSheets()) {
mStyleSet->AppendStyleSheet(*sheet);
styleSet.AppendStyleSheet(*sheet);
}
AppendSheetsToStyleSet(mStyleSet.get(), mAdditionalSheets[eAgentSheet]);
AppendSheetsToStyleSet(mStyleSet.get(), mAdditionalSheets[eUserSheet]);
AppendSheetsToStyleSet(mStyleSet.get(), mAdditionalSheets[eAuthorSheet]);
AppendSheetsToStyleSet(&styleSet, mAdditionalSheets[eAgentSheet]);
AppendSheetsToStyleSet(&styleSet, mAdditionalSheets[eUserSheet]);
AppendSheetsToStyleSet(&styleSet, mAdditionalSheets[eAuthorSheet]);
}
void Document::CompatibilityModeChanged() {
MOZ_ASSERT(IsHTMLOrXHTML());
CSSLoader()->SetCompatibilityMode(mCompatMode);
mStyleSet->CompatibilityModeChanged();
if (PresShell* presShell = GetPresShell()) {
// Selectors may have become case-sensitive / case-insensitive, the stylist
// has already performed the relevant invalidation.
presShell->EnsureStyleFlush();
if (mStyleSet) {
mStyleSet->CompatibilityModeChanged();
}
if (!mStyleSetFilled) {
MOZ_ASSERT(!mQuirkSheetAdded);
return;
}
MOZ_ASSERT(mStyleSet);
if (PresShell* presShell = GetPresShell()) {
// Selectors may have become case-sensitive / case-insensitive, the stylist
// has already performed the relevant invalidation.
presShell->EnsureStyleFlush();
}
if (mQuirkSheetAdded == NeedsQuirksSheet()) {
return;
}
@ -7318,13 +7326,13 @@ void Document::RemoveChildNode(nsIContent* aKid, bool aNotify) {
void Document::AddStyleSheetToStyleSets(StyleSheet& aSheet) {
if (mStyleSetFilled) {
mStyleSet->AddDocStyleSheet(aSheet);
EnsureStyleSet().AddDocStyleSheet(aSheet);
ApplicableStylesChanged();
}
}
void Document::RecordShadowStyleChange(ShadowRoot& aShadowRoot) {
mStyleSet->RecordShadowStyleChange(aShadowRoot);
EnsureStyleSet().RecordShadowStyleChange(aShadowRoot);
ApplicableStylesChanged(/* aKnownInShadowTree= */ true);
}
@ -7492,7 +7500,7 @@ nsresult Document::AddAdditionalStyleSheet(additionalSheetType aType,
mAdditionalSheets[aType].AppendElement(aSheet);
if (mStyleSetFilled) {
mStyleSet->AppendStyleSheet(*aSheet);
EnsureStyleSet().AppendStyleSheet(*aSheet);
ApplicableStylesChanged();
}
return NS_OK;
@ -7512,7 +7520,7 @@ void Document::RemoveAdditionalStyleSheet(additionalSheetType aType,
if (!mIsGoingAway) {
MOZ_ASSERT(sheetRef->IsApplicable());
if (mStyleSetFilled) {
mStyleSet->RemoveStyleSheet(*sheetRef);
EnsureStyleSet().RemoveStyleSheet(*sheetRef);
ApplicableStylesChanged();
}
}
@ -8803,7 +8811,7 @@ void Document::EnableStyleSheetsForSetInternal(const nsAString& aSheetSet,
if (aUpdateCSSLoader) {
CSSLoader()->DocumentStyleSheetSetChanged();
}
if (mStyleSet->StyleSheetsHaveChanged()) {
if (EnsureStyleSet().StyleSheetsHaveChanged()) {
ApplicableStylesChanged();
}
}
@ -15731,7 +15739,9 @@ void Document::DocAddSizeOfExcludingThis(nsWindowSizes& aWindowSizes) const {
mPresShell->AddSizeOfIncludingThis(aWindowSizes);
}
mStyleSet->AddSizeOfIncludingThis(aWindowSizes);
if (mStyleSet) {
mStyleSet->AddSizeOfIncludingThis(aWindowSizes);
}
aWindowSizes.mPropertyTablesSize +=
mPropertyTable.SizeOfExcludingThis(aWindowSizes.mState.mMallocSizeOf);
@ -16579,7 +16589,7 @@ void Document::FlushUserFontSet() {
RefPtr<PresShell> presShell = GetPresShell();
if (presShell) {
MOZ_ASSERT(mStyleSetFilled);
mStyleSet->AppendFontFaceRules(rules);
EnsureStyleSet().AppendFontFaceRules(rules);
}
if (!mFontFaceSet && !rules.IsEmpty()) {

Просмотреть файл

@ -1590,10 +1590,13 @@ class Document : public nsINode,
// Get the "head" element in the sense of document.head.
HTMLSharedElement* GetHead();
ServoStyleSet* StyleSetForPresShellOrMediaQueryEvaluation() const {
ServoStyleSet* StyleSetForPresShell() const {
MOZ_ASSERT(!!mStyleSet.get());
return mStyleSet.get();
}
inline ServoStyleSet& EnsureStyleSet() const;
// ShadowRoot has APIs that can change styles. This notifies the shell that
// stlyes applicable in the shadow tree have potentially changed.
void RecordShadowStyleChange(ShadowRoot&);

Просмотреть файл

@ -9,6 +9,7 @@
#include "mozilla/dom/Document.h"
#include "mozilla/PresShell.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/dom/HTMLBodyElement.h"
#include "nsContentUtils.h"
#include "nsPresContext.h"
@ -52,6 +53,15 @@ inline void Document::SetServoRestyleRootDirtyBits(uint32_t aDirtyBits) {
mServoRestyleRootDirtyBits = aDirtyBits;
}
inline ServoStyleSet& Document::EnsureStyleSet() const {
MOZ_ASSERT(NS_IsMainThread());
if (!mStyleSet) {
Document* doc = const_cast<Document*>(this);
doc->mStyleSet = MakeUnique<ServoStyleSet>(*doc);
}
return *(mStyleSet.get());
}
} // namespace mozilla::dom
#endif // mozilla_dom_DocumentInlines_h

Просмотреть файл

@ -67,7 +67,7 @@ void PresShell::SetNeedThrottledAnimationFlush() {
}
ServoStyleSet* PresShell::StyleSet() const {
return mDocument->StyleSetForPresShellOrMediaQueryEvaluation();
return mDocument->StyleSetForPresShell();
}
/* static */

Просмотреть файл

@ -12,7 +12,7 @@
#include "nsCSSFrameConstructor.h"
inline mozilla::ServoStyleSet* nsPresContext::StyleSet() const {
return mDocument->StyleSetForPresShellOrMediaQueryEvaluation();
return mDocument->StyleSetForPresShell();
}
inline nsCSSFrameConstructor* nsPresContext::FrameConstructor() const {

Просмотреть файл

@ -16,6 +16,7 @@
#include "nsIContentInlines.h"
#include "nsIScrollableFrame.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/HTMLTemplateElement.h"
#include "ChildIterator.h"
#include "nsComputedDOMStyle.h"
@ -860,9 +861,8 @@ void InspectorUtils::GetCSSRegisteredProperties(
GlobalObject& aGlobalObject, Document& aDocument,
nsTArray<InspectorCSSPropertyDefinition>& aResult) {
nsTArray<StylePropDef> result;
Servo_GetRegisteredCustomProperties(
aDocument.StyleSetForPresShellOrMediaQueryEvaluation()->RawData(),
&result);
Servo_GetRegisteredCustomProperties(aDocument.EnsureStyleSet().RawData(),
&result);
for (const auto& propDef : result) {
InspectorCSSPropertyDefinition& property = *aResult.AppendElement();

Просмотреть файл

@ -12,6 +12,7 @@
#include "mozilla/dom/HighlightRegistry.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentInlines.h"
#include "nsStyleUtil.h"
#include "xpcpublic.h"
@ -64,8 +65,7 @@ void CSS::RegisterProperty(const GlobalObject& aGlobal,
if (!doc) {
return aRv.ThrowUnknownError("No document associated to this global?");
}
doc->StyleSetForPresShellOrMediaQueryEvaluation()->RegisterProperty(
aDefinition, aRv);
doc->EnsureStyleSet().RegisterProperty(aDefinition, aRv);
}
} // namespace mozilla::dom

Просмотреть файл

@ -10,6 +10,7 @@
#include "mozAutoDocUpdate.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/MediaListBinding.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/ServoStyleSet.h"
@ -124,8 +125,7 @@ void MediaList::Delete(const nsACString& aOldMedium, ErrorResult& aRv) {
}
bool MediaList::Matches(const Document& aDocument) const {
const auto* rawData =
aDocument.StyleSetForPresShellOrMediaQueryEvaluation()->RawData();
const auto* rawData = aDocument.EnsureStyleSet().RawData();
MOZ_ASSERT(rawData, "The per doc data should be valid!");
return Servo_MediaList_Matches(mRawList, rawData);
}