Skip to content

Commit

Permalink
Use of many shadow trees with unique style triggers significant CPU u…
Browse files Browse the repository at this point in the history
…sage and a rise in process size

https://bugs.webkit.org/show_bug.cgi?id=260925
rdar://114731415

Reviewed by Alan Baradlay.

Style resolver sharing cache in Style::Scope has unlimited size and the test case keeps adding more items
there. Low memory handler clears the cache causing memory use to eventually drop.

* Source/WebCore/dom/InlineStyleSheetOwner.cpp:
(WebCore::InlineStyleSheetOwner::createSheet):

Increase the size of the inline stylesheet sharing cache  (50 -> 256 items). Content that uses many unique
shadow tree style (like this test) could overrun this smallish cache easily rendering it ineffective.

Style resolver sharing in Style::Scope depends on inline stylesheet getting shared so this also made
that cache work poorly.

* Source/WebCore/style/StyleScope.cpp:
(WebCore::Style::Scope::createOrFindSharedShadowTreeResolver):

Add a limit to the resolver sharing cache size.

Canonical link: https://commits.webkit.org/269142@main
  • Loading branch information
anttijk committed Oct 10, 2023
1 parent 6126b9b commit b3c5d42
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Source/WebCore/dom/InlineStyleSheetOwner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void InlineStyleSheetOwner::createSheet(Element& element, const String& text)
inlineStyleSheetCache().add(*cacheKey, &m_sheet->contents());

// Prevent pathological growth.
const size_t maximumInlineStyleSheetCacheSize = 50;
static constexpr auto maximumInlineStyleSheetCacheSize = 256;
if (inlineStyleSheetCache().size() > maximumInlineStyleSheetCacheSize) {
auto toRemove = inlineStyleSheetCache().random();
toRemove->value->removedFromMemoryCache();
Expand Down
5 changes: 5 additions & 0 deletions Source/WebCore/style/StyleScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ void Scope::createOrFindSharedShadowTreeResolver()
if (!result.isNewEntry) {
m_resolver = result.iterator->value.ptr();
m_resolver->setSharedBetweenShadowTrees();
return;
}

static constexpr auto maximimumSharedResolverCount = 256;
if (documentScope().m_sharedShadowTreeResolvers.size() > maximimumSharedResolverCount)
documentScope().m_sharedShadowTreeResolvers.remove(documentScope().m_sharedShadowTreeResolvers.random());
}

void Scope::unshareShadowTreeResolverBeforeMutation()
Expand Down

0 comments on commit b3c5d42

Please sign in to comment.