Truly independent web browser
LibWeb: Allocate less for the scratch lists and text of CSS values
Andreas Kling1d419ead75eb
diff --git a/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rsindex 8407b181ee3..2c805e5ef18 100644--- a/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/descriptor_parser.rs@@ -37,16 +37,14 @@ pub(crate) struct ParsedDescriptor {pub value: StyleValueArc,}-fn non_whitespace(values: &[ComponentValue]) -> Vec<&ComponentValue> {+fn non_whitespace(values: &[ComponentValue]) -> smallvec::SmallVec<[&ComponentValue; 8]> {values.iter().filter(|value| !value.is_whitespace()).collect()}fn single_non_whitespace(values: &[ComponentValue]) -> Option<&ComponentValue> {- let values = non_whitespace(values);- if values.len() != 1 {- return None;- }- Some(values[0])+ let mut values = values.iter().filter(|value| !value.is_whitespace());+ let value = values.next()?;+ values.next().is_none().then_some(value)}fn is_css_wide_keyword(value: &StyleValueData) -> bool {@@ -61,7 +59,8 @@ fn is_css_wide_keyword(value: &StyleValueData) -> bool {fn parse_keyword(values: &[ComponentValue], expected: &str) -> Option<StyleValueData> {let identifier = single_non_whitespace(values)?.ident()?;let parsed = keyword_from_ascii_case_insensitive(identifier)?;- let expected = keyword_from_ascii_case_insensitive(&expected.encode_utf16().collect::<Vec<_>>())?;+ let expected =+ keyword_from_ascii_case_insensitive(&expected.encode_utf16().collect::<smallvec::SmallVec<[u16; 32]>>())?;(parsed == expected).then_some(StyleValueData::Keyword { keyword: parsed })}diff --git a/Libraries/LibWeb/Rust/src/css/parser/transforms_effects_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/transforms_effects_parser.rsindex 30630bf966b..c921cc98f43 100644--- a/Libraries/LibWeb/Rust/src/css/parser/transforms_effects_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/transforms_effects_parser.rs@@ -213,7 +213,7 @@ pub(crate) fn parse_transform_function(Some(transformation(property_id::TRANSFORM, function as u8, values))}-fn non_whitespace(values: &[ComponentValue]) -> Vec<&ComponentValue> {+fn non_whitespace(values: &[ComponentValue]) -> smallvec::SmallVec<[&ComponentValue; 8]> {values.iter().filter(|value| !value.is_whitespace()).collect()}diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rsindex 5bd73ea33d9..0495a2138dd 100644--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs@@ -3823,7 +3823,10 @@ fn parse_generic_numeric_property(context: &ParseContext, property: u16, values:return ParseOutcome::NotHandled;}- let non_whitespace_values = values.iter().filter(|value| !value.is_whitespace()).collect::<Vec<_>>();+ let non_whitespace_values = values+ .iter()+ .filter(|value| !value.is_whitespace())+ .collect::<smallvec::SmallVec<[_; 4]>>();let accepted_types = property_accepted_value_types(property);let single_value = (non_whitespace_values.len() == 1).then(|| non_whitespace_values[0]);let mut parsed = accepted_types@@ -6387,17 +6390,28 @@ pub(crate) fn parse_css_value_with_utf16_source(return parse_css_value_after_substitution_scan(context, property_id, values, source, substitution_presence);}if substitution_presence.has_any() {- let unresolved_source = crate::css::serialize::serialize_component_values_to_utf16(+ thread_local! {+ static UNRESOLVED_SOURCE: std::cell::Cell<Vec<u16>> = const { std::cell::Cell::new(Vec::new()) };+ }+ // NB: An unresolved value copies the text that it keeps, thus the text is made in the memory of the last one.+ let mut unresolved_source = UNRESOLVED_SOURCE.take();+ crate::css::serialize::serialize_component_values_into_utf16(values,crate::css::parser::component_value::ComponentSerializationMode::PreserveNumericSource,+ &mut unresolved_source,);- return parse_css_value_after_substitution_scan(+ let outcome = parse_css_value_after_substitution_scan(context,property_id,values,&unresolved_source,substitution_presence,);+ // NB: The thread keeps the memory for the next value, but not the memory of one very large value.+ if unresolved_source.capacity() <= 4096 {+ UNRESOLVED_SOURCE.set(unresolved_source);+ }+ return outcome;}parse_css_value_after_substitution_scan(context, property_id, values, &[], substitution_presence)}diff --git a/Libraries/LibWeb/Rust/src/css/serialize.rs b/Libraries/LibWeb/Rust/src/css/serialize.rsindex 4b8fa0ddbb2..e7609025b77 100644--- a/Libraries/LibWeb/Rust/src/css/serialize.rs+++ b/Libraries/LibWeb/Rust/src/css/serialize.rs@@ -123,6 +123,13 @@ impl TextSink {}}+ /// Empties the sink and keeps its memory.+ fn clear(&mut self) {+ self.ascii.clear();+ self.utf16.clear();+ self.is_ascii = true;+ }+/// The current length in code units.fn len(&self) -> usize {if self.is_ascii {@@ -336,6 +343,39 @@ pub(crate) fn serialize_component_values_to_utf16(sink.into_utf16()}+thread_local! {+ static SERIALIZATION_SINK: std::cell::Cell<Option<TextSink>> = const { std::cell::Cell::new(None) };+}++/// Serializes `values` into `out` in place of what it holds, with the memory of the last serialization of the thread+/// for the work in between.+pub(crate) fn serialize_component_values_into_utf16(+ values: &[crate::css::parser::component_value::ComponentValue],+ mode: ComponentSerializationMode,+ out: &mut Vec<u16>,+) {+ let mut sink = SERIALIZATION_SINK.take().unwrap_or_default();+ sink.clear();+ if !serialize_component_values_into(&mut sink, values, mode) {+ sink.clear();+ assert!(serialize_component_values_into(+ &mut sink,+ values,+ ComponentSerializationMode::Normalized,+ ));+ }+ out.clear();+ if sink.is_ascii {+ out.extend(sink.ascii.iter().map(|&byte| u16::from(byte)));+ } else {+ out.extend_from_slice(&sink.utf16);+ }+ // NB: The thread keeps the memory for the next serialization, but not the memory of one very large one.+ if sink.ascii.capacity() <= 4096 && sink.utf16.capacity() <= 4096 {+ SERIALIZATION_SINK.set(Some(sink));+ }+}+pub(crate) fn normalized_component_values_equal(first: &[crate::css::parser::component_value::ComponentValue],second: &[crate::css::parser::component_value::ComponentValue],diff --git a/Meta/Linters/check_rust_process_wide_state.py b/Meta/Linters/check_rust_process_wide_state.pyindex f3620163f06..4d65a1e3460 100755--- a/Meta/Linters/check_rust_process_wide_state.py+++ b/Meta/Linters/check_rust_process_wide_state.py@@ -84,6 +84,8 @@ RENDER_STATE_ALLOWED = {SCRATCH,["css/cascaded_properties.rs:STORE_POOL",+ "css/parser/value_parser.rs:UNRESOLVED_SOURCE",+ "css/serialize.rs:SERIALIZATION_SINK",],),**render_state_entries(