Truly independent web browser
LibWeb: Use scratch memory for names and values in the CSS parser
Andreas Klinge18ea5bdda30
diff --git a/Libraries/LibWeb/Rust/src/css/parser/lexer.rs b/Libraries/LibWeb/Rust/src/css/parser/lexer.rsindex 1844dd11288..1c113f71ea3 100644--- a/Libraries/LibWeb/Rust/src/css/parser/lexer.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/lexer.rs@@ -562,8 +562,30 @@ pub(crate) enum Escapes {Url,}+/// What takes the code units that a text means.+pub(crate) trait UnitSink {+ fn push(&mut self, unit: u16);+}++impl UnitSink for Vec<u16> {+ #[inline]+ fn push(&mut self, unit: u16) {+ Vec::push(self, unit);+ }+}++impl<const N: usize> UnitSink for smallvec::SmallVec<[u16; N]>+where+ [u16; N]: smallvec::Array<Item = u16>,+{+ #[inline]+ fn push(&mut self, unit: u16) {+ smallvec::SmallVec::push(self, unit);+ }+}+/// Appends what `units` mean to `output`, which is not the units themselves where they have escapes.-pub(crate) fn unescape<U: Unit>(units: &[U], escapes: Escapes, output: &mut Vec<u16>) {+pub(crate) fn unescape<U: Unit>(units: &[U], escapes: Escapes, output: &mut impl UnitSink) {let at = |index: usize| units.get(index).map_or(END, |unit| unit.code());let mut position = 0;loop {diff --git a/Libraries/LibWeb/Rust/src/css/parser/rows.rs b/Libraries/LibWeb/Rust/src/css/parser/rows.rsindex 98f2f7d0e8c..1f2f5e9ee6c 100644--- a/Libraries/LibWeb/Rust/src/css/parser/rows.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/rows.rs@@ -272,12 +272,13 @@ impl<'a> Cv<'a> {(TokenizerInput::Latin1(units), None) => units[start..end].iter().map(|&unit| u16::from(unit)).collect(),(TokenizerInput::Utf16(units), None) => SmallVec::from_slice(&units[start..end]),(source, Some(escapes)) => {- let mut output = Vec::with_capacity(end - start);+ // NB: What the escapes mean is not longer than the escapes.+ let mut output = Name::new();match source {TokenizerInput::Latin1(units) => unescape(&units[start..end], escapes, &mut output),TokenizerInput::Utf16(units) => unescape(&units[start..end], escapes, &mut output),}- SmallVec::from_vec(output)+ output}}}diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rsindex 708c5750b42..be9775fff59 100644--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs@@ -1911,6 +1911,10 @@ struct SyntaxParseBuilder<'a> {open_declarations: Vec<Declaration>,/// Whether the rules that are in no rule are the children of a block, and not the rules of a sheet.roots_are_items: bool,+ /// The memory for the component values of the value that is parsed.+ value_scratch: Vec<ComponentValue>,+ /// The memory for the text of the value that is parsed, where the source does not have it as UTF-16.+ text_scratch: Vec<u16>,/// The tokens of the prelude that is parsed.rows: RefCell<crate::css::parser::rows::Rows>,/// The names that the selectors of the style rules name.@@ -2711,6 +2715,8 @@ impl<'a> SyntaxParseBuilder<'a> {open_children: Vec::new(),open_declarations: Vec::new(),roots_are_items: false,+ value_scratch: Vec::new(),+ text_scratch: Vec::new(),rows: RefCell::default(),names: RefCell::default(),nesting: Cell::new(None),@@ -2938,12 +2944,13 @@ impl<'a> SyntaxParseBuilder<'a> {let mut context = *parse_context;context.value_contexts = ak::Span::new(&value_contexts);- let outcome = parse_css_value_with_utf16_source(- &context,- property_id,- &declaration.value.component_values(),- &declaration.value.text(),- );+ // NB: Each value uses the memory of the one before it for its component values and its text.+ let mut values = std::mem::take(&mut self.value_scratch);+ declaration.value.append_component_values_to(&mut values);+ let text = declaration.value.text_in(&mut self.text_scratch);+ let outcome = parse_css_value_with_utf16_source(&context, property_id, &values, text);+ values.clear();+ self.value_scratch = values;if let Some(random_function_index) = parse_context.random_function_index {random_function_index.set(0);}@@ -3032,13 +3039,16 @@ impl<'a> SyntaxParseBuilder<'a> {};if declaration.property_id == property_id::CUSTOM {let name = self.declaration_name(declaration);- let name = self- .dedup- .custom_property_names- .borrow_mut()- .entry(name.into())- .or_insert_with(|| CssString::from_utf16(name))- .clone();+ let mut names = self.dedup.custom_property_names.borrow_mut();+ let name = match names.get(name) {+ Some(string) => string.clone(),+ None => {+ let string = CssString::from_utf16(name);+ names.insert(name.into(), string.clone());+ string+ }+ };+ drop(names);block.append_custom_property(name, property);} else {block.append_in_specified_order(property);diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rsindex 70067aa6f64..c58e7c4f9a0 100644--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs@@ -356,10 +356,20 @@ impl<'a, U: Unit> Stream<'a, U> {/// The component values of the text in `range` of a source.fn component_values_in<U: Unit>(units: &[U], storage: &Arc<SourceStorage>, range: Range<usize>) -> Vec<ComponentValue> {- let mut stream = Stream::new(units, storage, range);let mut values = Vec::with_capacity(4);+ append_component_values_in(units, storage, range, &mut values);+ values+}++fn append_component_values_in<U: Unit>(+ units: &[U],+ storage: &Arc<SourceStorage>,+ range: Range<usize>,+ values: &mut Vec<ComponentValue>,+) {+ let mut stream = Stream::new(units, storage, range);loop {- stream.push_gap(&mut values);+ stream.push_gap(values);if stream.token.kind == Kind::EndOfFile {break;}@@ -368,7 +378,6 @@ fn component_values_in<U: Unit>(units: &[U], storage: &Arc<SourceStorage>, range};values.push(value);}- values}/// The value of a declaration: where it is in the source. It ends where the last component value before a@@ -390,9 +399,9 @@ impl DeclarationValue {pub(crate) fn name(&self) -> Name {fn name_in<U: Unit>(units: &[U], is_escaped: bool) -> Name {if is_escaped {- let mut output = Vec::with_capacity(units.len());+ let mut output = Name::new();unescape(units, Escapes::Name, &mut output);- return Name::from_vec(output);+ return output;}units.iter().map(|unit| unit.code() as u16).collect()}@@ -415,6 +424,14 @@ impl DeclarationValue {}}+ /// Appends the component values of the value to `values`.+ pub(crate) fn append_component_values_to(&self, values: &mut Vec<ComponentValue>) {+ match self.source.as_ref() {+ SourceStorage::Latin1(units) => append_component_values_in(units, &self.source, self.range(), values),+ SourceStorage::Utf16(units) => append_component_values_in(units, &self.source, self.range(), values),+ }+ }+/// The text of the value.pub(crate) fn text(&self) -> Cow<'_, [u16]> {match self.source.as_ref() {@@ -423,6 +440,18 @@ impl DeclarationValue {}}+ /// The text of the value, which uses the memory of `scratch` where the source does not have it as UTF-16.+ pub(crate) fn text_in<'a>(&'a self, scratch: &'a mut Vec<u16>) -> &'a [u16] {+ match self.source.as_ref() {+ SourceStorage::Latin1(units) => {+ scratch.clear();+ scratch.extend(units[self.range()].iter().copied().map(u16::from));+ scratch+ }+ SourceStorage::Utf16(units) => &units[self.range()],+ }+ }+/// A hash of the declaration as written, without its `!important`.pub(super) fn hash_of_text(&self, hasher: &impl std::hash::BuildHasher) -> u64 {let name = self.name.start as usize..self.name.end as usize;@@ -530,10 +559,9 @@ impl Prelude {}pub(crate) fn text(&self) -> Vec<u16> {- match self.source.as_ref() {- SourceStorage::Latin1(units) => units[self.range()].iter().map(|&unit| u16::from(unit)).collect(),- SourceStorage::Utf16(units) => units[self.range()].to_vec(),- }+ let mut text = Vec::new();+ self.input().slice(self.range()).append_to(&mut text);+ text}}diff --git a/Libraries/LibWeb/Rust/src/css/serialize.rs b/Libraries/LibWeb/Rust/src/css/serialize.rsindex ced3491560c..4b8fa0ddbb2 100644--- a/Libraries/LibWeb/Rust/src/css/serialize.rs+++ b/Libraries/LibWeb/Rust/src/css/serialize.rs@@ -322,7 +322,9 @@ pub(crate) fn serialize_component_values_to_utf16(values: &[crate::css::parser::component_value::ComponentValue],mode: ComponentSerializationMode,) -> Vec<u16> {+ // NB: Most values are short, and the sink then grows once at most.let mut sink = TextSink::new();+ sink.ascii.reserve(64);if !serialize_component_values_into(&mut sink, values, mode) {sink = TextSink::new();assert!(serialize_component_values_into(diff --git a/Libraries/LibWeb/Rust/src/css/style/atoms.rs b/Libraries/LibWeb/Rust/src/css/style/atoms.rsindex 09fc4f9d370..d2fac97a2d1 100644--- a/Libraries/LibWeb/Rust/src/css/style/atoms.rs+++ b/Libraries/LibWeb/Rust/src/css/style/atoms.rs@@ -402,7 +402,7 @@ impl AtomTable {{atom} else {- let lowercase: Vec<u16> = text+ let lowercase: smallvec::SmallVec<[u16; 60]> = text.iter().map(|&unit| crate::css::ffi_support::ascii_lowercase(unit)).collect();