Truly independent web browser
LibWeb: Keep short selectors in place while they are parsed
Andreas Klinga907903d671f
diff --git a/Libraries/LibWeb/Rust/src/css/selector_arena.rs b/Libraries/LibWeb/Rust/src/css/selector_arena.rsindex 8f5cb2cf8b2..cb9f79c7abb 100644--- a/Libraries/LibWeb/Rust/src/css/selector_arena.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_arena.rs@@ -1606,6 +1606,9 @@ pub(crate) enum NestingContext<'a> {Rule(&'a SelectorListRef),}+/// What the tests of a compound read, by which compounds have one shape. In place for a short compound.+type CompoundShapeKey = smallvec::SmallVec<[(u32, u32); 6]>;+impl Drop for SheetSelectorsBuilder {fn drop(&mut self) {let arena = std::mem::take(&mut self.builder).finish();@@ -2323,7 +2326,7 @@ impl SelectorArena {/// Note what matching asks of each compound as a whole, and where each compound starts.fn note_compound_facts(&mut self) {let mut compounds = Vec::new();- let mut shapes: crate::css::style::fast_hash::FastMap<Vec<(u32, u32)>, u32> = Default::default();+ let mut shapes: crate::css::style::fast_hash::FastMap<CompoundShapeKey, u32> = Default::default();let words = self.components.len().div_ceil(64);let mut starts = vec![0u64; words];let mut start = 0;@@ -2387,7 +2390,7 @@ impl SelectorArena {fn compound_shape(&self,components: std::ops::Range<usize>,- shapes: &mut crate::css::style::fast_hash::FastMap<Vec<(u32, u32)>, u32>,+ shapes: &mut crate::css::style::fast_hash::FastMap<CompoundShapeKey, u32>,) -> u32 {// NB: Only what a compound's own tests read counts toward its shape: not the combinator its last component// carries, nor the facts the arena notes of a selector or a compound.@@ -2413,7 +2416,7 @@ impl SelectorArena {if reads_its_selector {return CompoundFacts::NO_SHAPE;}- let key: Vec<(u32, u32)> = self.components[components]+ let key: CompoundShapeKey = self.components[components].iter().map(|component| (component.bits.0 & SHAPE_BITS, component.data)).collect();diff --git a/Libraries/LibWeb/Rust/src/css/selector_operations.rs b/Libraries/LibWeb/Rust/src/css/selector_operations.rsindex fc0b0d546d9..4128e29ed7f 100644--- a/Libraries/LibWeb/Rust/src/css/selector_operations.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_operations.rs@@ -37,7 +37,7 @@ fn scope_selector() -> Arc<CompiledSelector> {CompiledSelector::new(Box::new([CompoundSelector {combinator: Combinator::None,is_implicit_universal_anchor: false,- simple_selectors: Box::new([pseudo_class(PseudoClassType::Scope, Box::new([]))]),+ simple_selectors: smallvec::smallvec![pseudo_class(PseudoClassType::Scope, Box::new([]))],}]))}@@ -50,7 +50,7 @@ pub(crate) fn scope_root_selector_list() -> SelectorList {Box::new([CompiledSelector::new(Box::new([CompoundSelector {combinator: Combinator::None,is_implicit_universal_anchor: false,- simple_selectors: Box::new([scope_root_anchor()]),+ simple_selectors: smallvec::smallvec![scope_root_anchor()],}]))])}@@ -189,7 +189,7 @@ fn absolutize(selector: &CompiledSelector, replacement: &SimpleSelector) -> Opti_ => simples.push(simple.clone()),}}- compound.simple_selectors = simples.into_boxed_slice();+ compound.simple_selectors = simples.into();}Some(CompiledSelector::new(compounds))}@@ -199,7 +199,7 @@ pub(crate) fn relative_to(selector: &CompiledSelector, parent: SimpleSelector) -compounds.push(CompoundSelector {combinator: Combinator::None,is_implicit_universal_anchor: false,- simple_selectors: Box::new([parent]),+ simple_selectors: smallvec::smallvec![parent],});compounds.extend(selectordiff --git a/Libraries/LibWeb/Rust/src/css/selector_parser.rs b/Libraries/LibWeb/Rust/src/css/selector_parser.rsindex 63537e02144..5f644927fa1 100644--- a/Libraries/LibWeb/Rust/src/css/selector_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_parser.rs@@ -24,8 +24,8 @@ use super::selector::{use super::selector::{NameSelector, QualifiedName, RustSelector, SelectorListRef, SelectorRef};use super::selector_tree::{AttributeSelector, CompiledSelector, CompoundSelector, LanguageRange, PseudoClassSelector, PseudoElementSelector,- PseudoElementValue, SelectorList, SimpleSelector, TransitionNameAndClassSelector, TransitionNameSelector,- search_text_match_filter,+ PseudoElementValue, SelectorList, SimpleSelector, SimpleSelectorVec, TransitionNameAndClassSelector,+ TransitionNameSelector, search_text_match_filter,};use crate::css::retained_fly_string::RetainedUtf16FlyString;use crate::css::style::AtomLease;@@ -382,7 +382,7 @@ impl<'a> SelectorParser<'a> {Combinator::Descendant},is_implicit_universal_anchor: false,- simple_selectors: vec![SimpleSelector::Invalid(source.into())].into_boxed_slice(),+ simple_selectors: smallvec::smallvec![SimpleSelector::Invalid(source.into())],}].into_boxed_slice(),));@@ -1095,7 +1095,7 @@ impl<'a> SelectorParser<'a> {Ok(CompoundSelector {combinator: Combinator::None,is_implicit_universal_anchor: false,- simple_selectors: boxed(simple_selectors?),+ simple_selectors: simple_selectors?,})}@@ -1174,25 +1174,24 @@ fn normalize_pseudo_element_transitions(compounds: SmallVec<[CompoundSelector; 4let mut normalized = SmallVec::new();for compound in compounds {let mut combinator = compound.combinator;- let mut current = Vec::new();+ let mut current = SimpleSelectorVec::new();for simple in compound.simple_selectors {if matches!(simple, SimpleSelector::PseudoElement(_)) {if current.is_empty() {normalized.push(CompoundSelector {combinator,is_implicit_universal_anchor: true,- simple_selectors: vec![SimpleSelector::Universal(Box::new(QualifiedName::new(+ simple_selectors: smallvec::smallvec![SimpleSelector::Universal(Box::new(QualifiedName::new(NamespaceType::Any,StyleAtomID::NONE,StyleAtomID::NONE,- )))]- .into_boxed_slice(),+ )))],});} else {normalized.push(CompoundSelector {combinator,is_implicit_universal_anchor: false,- simple_selectors: std::mem::take(&mut current).into_boxed_slice(),+ simple_selectors: std::mem::take(&mut current),});}combinator = Combinator::PseudoElement;@@ -1203,7 +1202,7 @@ fn normalize_pseudo_element_transitions(compounds: SmallVec<[CompoundSelector; 4normalized.push(CompoundSelector {combinator,is_implicit_universal_anchor: false,- simple_selectors: current.into_boxed_slice(),+ simple_selectors: current,});}}@@ -1397,7 +1396,7 @@ fn parse_pseudo_element_selector(compounds.push(CompoundSelector {combinator: Combinator::None,is_implicit_universal_anchor: false,- simple_selectors: Box::new([simple]),+ simple_selectors: smallvec::smallvec![simple],});let selector = CompiledSelector::from_compounds(normalize_pseudo_element_transitions(compounds));let list = SelectorListRef::pack(&[selector]);diff --git a/Libraries/LibWeb/Rust/src/css/selector_tree.rs b/Libraries/LibWeb/Rust/src/css/selector_tree.rsindex 21431c6282f..7067cb57ec7 100644--- a/Libraries/LibWeb/Rust/src/css/selector_tree.rs+++ b/Libraries/LibWeb/Rust/src/css/selector_tree.rs@@ -111,9 +111,12 @@ pub struct CompoundSelector {/// leftmost compound therefore has `Combinator::None`.pub combinator: Combinator,pub is_implicit_universal_anchor: bool,- pub simple_selectors: Box<[SimpleSelector]>,+ pub simple_selectors: SimpleSelectorVec,}+/// The simple selectors of a compound to build a selector from. Most compounds have few, which are then in place.+pub type SimpleSelectorVec = SmallVec<[SimpleSelector; 4]>;+/// One compound of a complex selector, borrowed from its flat components.#[derive(Clone, Copy, Debug)]pub struct CompoundRef<'a> {@@ -130,7 +133,7 @@ impl CompoundRef<'_> {CompoundSelector {combinator: self.combinator,is_implicit_universal_anchor: self.is_implicit_universal_anchor,- simple_selectors: self.simple_selectors.into(),+ simple_selectors: self.simple_selectors.iter().cloned().collect(),}}}@@ -139,7 +142,8 @@ impl CompoundRef<'_> {/// [`SimpleSelector::Compound`] naming its combinator, followed by its simple selectors.#[derive(Debug, PartialEq, Eq)]pub struct CompiledSelector {- components: Box<[SimpleSelector]>,+ /// In place where the selector is short, so that it is one allocation.+ components: SmallVec<[SimpleSelector; 6]>,}impl CompiledSelector {@@ -152,7 +156,7 @@ impl CompiledSelector {.iter().map(|compound| compound.simple_selectors.len() + 1).sum();- let mut components = Vec::with_capacity(count);+ let mut components = SmallVec::with_capacity(count);for compound in compound_selectors {components.push(SimpleSelector::Compound {combinator: compound.combinator,@@ -160,9 +164,7 @@ impl CompiledSelector {});components.extend(compound.simple_selectors);}- Arc::new(Self {- components: components.into_boxed_slice(),- })+ Arc::new(Self { components })}/// The compounds of the selector, left to right.