Reuse StylesheetContent for inline style sheets with identical content (#38540)

For duplicate style sheets with identical content, `StylesheetContents`
can be reused to avoid redundant parsing of the inline style sheets.
Since duplicate stylesheets is a common case with web components, this
change will significantly improve performance. Additionally, the cache
hit rate of stylo's `CascadeDataCache` can now be significantly
improved.

When shared `StylesheetContents` is modified, copy-on-write will occur
to avoid affecting other sharers. And then updates the references to
`CssRule` or `PropertyDeclarationBlock` stored in the CSSOMs to ensure
that modifications are made only on the new copy.

Signed-off-by: sharpshooter_pt <ibluegalaxy_taoj@163.com>
This commit is contained in:
JoeDow 2025-08-20 20:31:49 +08:00 committed by GitHub
parent f6b77f94e2
commit 6e6ef513a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 711 additions and 125 deletions

View file

@ -2,10 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::RefCell;
use cssparser::{Parser, ParserInput};
use dom_struct::dom_struct;
use servo_arc::Arc;
use style::shared_lock::{Locked, ToCssWithGuard};
use style::shared_lock::{Locked, SharedRwLockReadGuard, ToCssWithGuard};
use style::stylesheets::CssRuleType;
use style::stylesheets::keyframes_rule::{Keyframe, KeyframeSelector, KeyframesRule};
use style::values::KeyframesName;
@ -28,7 +30,7 @@ pub(crate) struct CSSKeyframesRule {
cssrule: CSSRule,
#[ignore_malloc_size_of = "Arc"]
#[no_trace]
keyframesrule: Arc<Locked<KeyframesRule>>,
keyframesrule: RefCell<Arc<Locked<KeyframesRule>>>,
rulelist: MutNullableDom<CSSRuleList>,
}
@ -39,7 +41,7 @@ impl CSSKeyframesRule {
) -> CSSKeyframesRule {
CSSKeyframesRule {
cssrule: CSSRule::new_inherited(parent_stylesheet),
keyframesrule,
keyframesrule: RefCell::new(keyframesrule),
rulelist: MutNullableDom::new(None),
}
}
@ -67,7 +69,7 @@ impl CSSKeyframesRule {
CSSRuleList::new(
self.global().as_window(),
parent_stylesheet,
RulesSource::Keyframes(self.keyframesrule.clone()),
RulesSource::Keyframes(self.keyframesrule.borrow().clone()),
can_gc,
)
})
@ -82,6 +84,7 @@ impl CSSKeyframesRule {
// This finds the *last* element matching a selector
// because that's the rule that applies. Thus, rposition
self.keyframesrule
.borrow()
.read_with(&guard)
.keyframes
.iter()
@ -90,6 +93,18 @@ impl CSSKeyframesRule {
None
}
}
pub(crate) fn update_rule(
&self,
keyframesrule: Arc<Locked<KeyframesRule>>,
guard: &SharedRwLockReadGuard,
) {
if let Some(rulelist) = self.rulelist.get() {
rulelist.update_rules(RulesSource::Keyframes(keyframesrule.clone()), guard);
}
*self.keyframesrule.borrow_mut() = keyframesrule;
}
}
impl CSSKeyframesRuleMethods<crate::DomTypeHolder> for CSSKeyframesRule {
@ -108,8 +123,10 @@ impl CSSKeyframesRuleMethods<crate::DomTypeHolder> for CSSKeyframesRule {
);
if let Ok(rule) = rule {
self.cssrule.parent_stylesheet().will_modify();
let mut guard = self.cssrule.shared_lock().write();
self.keyframesrule
.borrow()
.write_with(&mut guard)
.keyframes
.push(rule);
@ -135,7 +152,7 @@ impl CSSKeyframesRuleMethods<crate::DomTypeHolder> for CSSKeyframesRule {
// https://drafts.csswg.org/css-animations/#dom-csskeyframesrule-name
fn Name(&self) -> DOMString {
let guard = self.cssrule.shared_lock().read();
DOMString::from(&**self.keyframesrule.read_with(&guard).name.as_atom())
DOMString::from(&**self.keyframesrule.borrow().read_with(&guard).name.as_atom())
}
// https://drafts.csswg.org/css-animations/#dom-csskeyframesrule-name
@ -143,9 +160,10 @@ impl CSSKeyframesRuleMethods<crate::DomTypeHolder> for CSSKeyframesRule {
// Spec deviation: https://github.com/w3c/csswg-drafts/issues/801
// Setting this property to a CSS-wide keyword or `none` does not throw,
// it stores a value that serializes as a quoted string.
self.cssrule.parent_stylesheet().will_modify();
let name = KeyframesName::from_ident(&value);
let mut guard = self.cssrule.shared_lock().write();
self.keyframesrule.write_with(&mut guard).name = name;
self.keyframesrule.borrow().write_with(&mut guard).name = name;
self.cssrule.parent_stylesheet().notify_invalidations();
Ok(())
}
@ -159,6 +177,7 @@ impl SpecificCSSRule for CSSKeyframesRule {
fn get_css(&self) -> DOMString {
let guard = self.cssrule.shared_lock().read();
self.keyframesrule
.borrow()
.read_with(&guard)
.to_css_string(&guard)
.into()