mirror of
https://github.com/servo/servo.git
synced 2025-09-30 00:29:14 +01:00
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:
parent
f6b77f94e2
commit
6e6ef513a9
25 changed files with 711 additions and 125 deletions
|
@ -2,6 +2,8 @@
|
|||
* 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;
|
||||
|
@ -26,7 +28,7 @@ pub(crate) struct MediaList {
|
|||
parent_stylesheet: Dom<CSSStyleSheet>,
|
||||
#[ignore_malloc_size_of = "Arc"]
|
||||
#[no_trace]
|
||||
media_queries: Arc<Locked<StyleMediaList>>,
|
||||
media_queries: RefCell<Arc<Locked<StyleMediaList>>>,
|
||||
}
|
||||
|
||||
impl MediaList {
|
||||
|
@ -38,7 +40,7 @@ impl MediaList {
|
|||
MediaList {
|
||||
parent_stylesheet: Dom::from_ref(parent_stylesheet),
|
||||
reflector_: Reflector::new(),
|
||||
media_queries,
|
||||
media_queries: RefCell::new(media_queries),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,7 +59,7 @@ impl MediaList {
|
|||
}
|
||||
|
||||
fn shared_lock(&self) -> &SharedRwLock {
|
||||
&self.parent_stylesheet.style_stylesheet().shared_lock
|
||||
self.parent_stylesheet.shared_lock()
|
||||
}
|
||||
|
||||
/// <https://drafts.csswg.org/cssom/#parse-a-media-query-list>
|
||||
|
@ -109,7 +111,11 @@ impl MediaList {
|
|||
|
||||
pub(crate) fn clone_media_list(&self) -> StyleMediaList {
|
||||
let guard = self.shared_lock().read();
|
||||
self.media_queries.read_with(&guard).clone()
|
||||
self.media_queries.borrow().read_with(&guard).clone()
|
||||
}
|
||||
|
||||
pub(crate) fn update_media_list(&self, media_queries: Arc<Locked<StyleMediaList>>) {
|
||||
*self.media_queries.borrow_mut() = media_queries;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,14 +123,21 @@ impl MediaListMethods<crate::DomTypeHolder> for MediaList {
|
|||
/// <https://drafts.csswg.org/cssom/#dom-medialist-mediatext>
|
||||
fn MediaText(&self) -> DOMString {
|
||||
let guard = self.shared_lock().read();
|
||||
DOMString::from(self.media_queries.read_with(&guard).to_css_string())
|
||||
DOMString::from(
|
||||
self.media_queries
|
||||
.borrow()
|
||||
.read_with(&guard)
|
||||
.to_css_string(),
|
||||
)
|
||||
}
|
||||
|
||||
/// <https://drafts.csswg.org/cssom/#dom-medialist-mediatext>
|
||||
fn SetMediaText(&self, value: DOMString) {
|
||||
self.parent_stylesheet.will_modify();
|
||||
let global = self.global();
|
||||
let mut guard = self.shared_lock().write();
|
||||
let media_queries = self.media_queries.write_with(&mut guard);
|
||||
let media_queries_borrowed = self.media_queries.borrow();
|
||||
let media_queries = media_queries_borrowed.write_with(&mut guard);
|
||||
*media_queries = Self::parse_media_list(&value, global.as_window());
|
||||
self.parent_stylesheet.notify_invalidations();
|
||||
}
|
||||
|
@ -132,13 +145,18 @@ impl MediaListMethods<crate::DomTypeHolder> for MediaList {
|
|||
// https://drafts.csswg.org/cssom/#dom-medialist-length
|
||||
fn Length(&self) -> u32 {
|
||||
let guard = self.shared_lock().read();
|
||||
self.media_queries.read_with(&guard).media_queries.len() as u32
|
||||
self.media_queries
|
||||
.borrow()
|
||||
.read_with(&guard)
|
||||
.media_queries
|
||||
.len() as u32
|
||||
}
|
||||
|
||||
/// <https://drafts.csswg.org/cssom/#dom-medialist-item>
|
||||
fn Item(&self, index: u32) -> Option<DOMString> {
|
||||
let guard = self.shared_lock().read();
|
||||
self.media_queries
|
||||
.borrow()
|
||||
.read_with(&guard)
|
||||
.media_queries
|
||||
.get(index as usize)
|
||||
|
@ -160,18 +178,28 @@ impl MediaListMethods<crate::DomTypeHolder> for MediaList {
|
|||
return;
|
||||
}
|
||||
// Step 3
|
||||
let m_serialized = m.clone().unwrap().to_css_string();
|
||||
let mut guard = self.shared_lock().write();
|
||||
let mq = self.media_queries.write_with(&mut guard);
|
||||
let any = mq
|
||||
.media_queries
|
||||
.iter()
|
||||
.any(|q| m_serialized == q.to_css_string());
|
||||
if any {
|
||||
return;
|
||||
{
|
||||
let m_serialized = m.clone().unwrap().to_css_string();
|
||||
let guard = self.shared_lock().read();
|
||||
let any = self
|
||||
.media_queries
|
||||
.borrow()
|
||||
.read_with(&guard)
|
||||
.media_queries
|
||||
.iter()
|
||||
.any(|q| m_serialized == q.to_css_string());
|
||||
if any {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Step 4
|
||||
mq.media_queries.push(m.unwrap());
|
||||
self.parent_stylesheet.will_modify();
|
||||
let mut guard = self.shared_lock().write();
|
||||
self.media_queries
|
||||
.borrow()
|
||||
.write_with(&mut guard)
|
||||
.media_queries
|
||||
.push(m.unwrap());
|
||||
self.parent_stylesheet.notify_invalidations();
|
||||
}
|
||||
|
||||
|
@ -185,9 +213,11 @@ impl MediaListMethods<crate::DomTypeHolder> for MediaList {
|
|||
return;
|
||||
}
|
||||
// Step 3
|
||||
self.parent_stylesheet.will_modify();
|
||||
let m_serialized = m.unwrap().to_css_string();
|
||||
let mut guard = self.shared_lock().write();
|
||||
let media_list = self.media_queries.write_with(&mut guard);
|
||||
let media_queries_borrowed = self.media_queries.borrow();
|
||||
let media_list = media_queries_borrowed.write_with(&mut guard);
|
||||
let new_vec = media_list
|
||||
.media_queries
|
||||
.drain(..)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue