Skip to content

Truly independent web browser

LibWeb: Make the CSS declaration tables flat and hash them faster

Andreas Kling029df8b2bdb3
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
index 1f36dade934..aa60602d7cb 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
@@ -163,7 +163,6 @@ pub(crate) struct QualifiedRule {
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct Declaration {
- pub name: ParserString,
pub value: DeclarationValue,
pub important: bool,
pub source_position: SourcePosition,
@@ -171,6 +170,13 @@ pub(crate) struct Declaration {
pub rule_context: RuleContext,
}
+impl Declaration {
+ /// The name of the declaration, which is not its text where that has escapes.
+ pub(crate) fn name(&self) -> crate::css::parser::rows::Name {
+ self.value.name()
+ }
+}
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Nested {
No,
@@ -951,20 +957,14 @@ fn parse_property_prelude(rule: &AtRule, declarations: &[Declaration], context:
let mut initial_value_source = None;
let mut initial_value_is_invalid = false;
for declaration in declarations {
+ let name = declaration.name();
+ let name = name.as_slice();
let source = declaration.value.text();
let declaration_values = declaration.value.component_values();
- if parse_descriptor(
- context,
- 2,
- declaration.name.as_ref(),
- &declaration_values,
- source.as_ref(),
- )
- .is_none()
- {
+ if parse_descriptor(context, 2, name, &declaration_values, source.as_ref()).is_none() {
// NB: A var() reference matches the descriptor's <declaration-value>? grammar, so it replaces an earlier
// initial value. It is not computationally independent, so it makes the rule invalid.
- if equals_ascii_case_insensitive(declaration.name.as_ref(), b"initial-value")
+ if equals_ascii_case_insensitive(name, b"initial-value")
&& !declaration.important
&& declaration_value_is_valid(&declaration_values)
&& initial_value_contains_variable_reference(&declaration_values) == Some(true)
@@ -973,7 +973,7 @@ fn parse_property_prelude(rule: &AtRule, declarations: &[Declaration], context:
}
continue;
}
- if equals_ascii_case_insensitive(declaration.name.as_ref(), b"syntax") {
+ if equals_ascii_case_insensitive(name, b"syntax") {
let values = non_whitespace(&declaration_values);
syntax_source = values
.as_slice()
@@ -981,7 +981,7 @@ fn parse_property_prelude(rule: &AtRule, declarations: &[Declaration], context:
.and_then(|value| value.string())
.filter(|_| values.len() == 1)
.map(|source| source.to_vec().into_boxed_slice().into());
- } else if equals_ascii_case_insensitive(declaration.name.as_ref(), b"inherits") {
+ } else if equals_ascii_case_insensitive(name, b"inherits") {
let values = non_whitespace(&declaration_values);
inherits = values
.as_slice()
@@ -997,7 +997,7 @@ fn parse_property_prelude(rule: &AtRule, declarations: &[Declaration], context:
None
}
});
- } else if equals_ascii_case_insensitive(declaration.name.as_ref(), b"initial-value") {
+ } else if equals_ascii_case_insensitive(name, b"initial-value") {
initial_value_is_invalid = false;
initial_value_source = Some(serialize_component_values_to_utf16(
&declaration_values,
@@ -1448,7 +1448,7 @@ pub(crate) fn supports_declaration_matches(context: &ParseContext, source: &[u16
let [declaration] = found.declarations.as_slice() else {
return false;
};
- let Some(property_id) = property_id_from_name(declaration.name.as_ref()) else {
+ let Some(property_id) = property_id_from_name(declaration.name().as_ref()) else {
return false;
};
@@ -1913,23 +1913,37 @@ struct DeclarationDedup {
/// What each declaration text parsed to, by the hash of the text. A text is the name and the value of a declaration
/// as the source has them, and the declarations with one text have one number, which is its index in the order the
/// texts came in.
- values: std::collections::HashMap<u64, Vec<(DeclarationValue, u32, ParsedDeclarationValue)>>,
+ values: hashbrown::HashTable<DeclarationText>,
text_count: u32,
/// For each declaration, by index: twice the number of its text, plus one if it is important. `UNSHARED` for one
/// that is never shared.
declaration_keys: Vec<u32>,
/// The blocks that runs of declarations made, by the hash of the keys of the run. Each is with where the
/// declarations of the run that made it are.
- blocks: std::collections::HashMap<u64, Vec<(std::ops::Range<usize>, SharedDeclarationBlock)>>,
+ blocks: hashbrown::HashTable<(u64, std::ops::Range<usize>, SharedDeclarationBlock)>,
/// The values the sheet's declarations parsed to, by their content's hash, so that declarations
/// of different properties, or differently written, that parse to the same value hold one
/// allocation of it.
- parsed_values: std::collections::HashMap<u64, Vec<StyleValueArc>>,
+ parsed_values: hashbrown::HashTable<(u64, StyleValueArc)>,
/// The names of the custom properties the sheet declares, so that the declarations of one name
/// hold one string of it.
- custom_property_names: RefCell<std::collections::HashMap<Box<[u16]>, CssString>>,
+ custom_property_names: RefCell<hashbrown::HashMap<Box<[u16]>, CssString, foldhash::fast::RandomState>>,
/// Keyed at random, so that a page cannot make many keys fall into one bucket.
- hasher: std::collections::hash_map::RandomState,
+ hasher: foldhash::fast::RandomState,
+}
+
+/// A declaration text that the sheet has, and what it parsed to.
+struct DeclarationText {
+ hash: u64,
+ /// Where the first declaration with the text has its name and its value in the source.
+ name: std::ops::Range<u32>,
+ value: std::ops::Range<u32>,
+ /// The number of the text.
+ number: u32,
+ /// Where the name of the declaration is in the text that the parse keeps.
+ name_offset: usize,
+ name_length: usize,
+ parsed: ParsedDeclarationValue,
}
impl DeclarationDedup {
@@ -1938,11 +1952,14 @@ impl DeclarationDedup {
/// The allocation of a value equal to `value` the sheet already holds, or `value` itself, which
/// later equal values then share.
fn intern_value(&mut self, value: &StyleValueArc) -> StyleValueArc {
- let candidates = self.parsed_values.entry(value.content_hash()).or_default();
- if let Some(interned) = candidates.iter().find(|candidate| ***candidate == **value) {
+ let hash = value.content_hash();
+ if let Some((_, interned)) = self.parsed_values.find(hash, |(candidate_hash, candidate)| {
+ *candidate_hash == hash && **candidate == **value
+ }) {
return interned.clone();
}
- candidates.push(value.clone());
+ self.parsed_values
+ .insert_unique(hash, (hash, value.clone()), |(hash, _)| *hash);
value.clone()
}
@@ -2245,7 +2262,7 @@ fn collect_descriptors<'a>(
let Some(descriptor) = parse_descriptor(
context,
at_rule,
- declaration.name.as_ref(),
+ declaration.name().as_ref(),
&declaration.value.component_values(),
declaration.value.text().as_ref(),
) else {
@@ -2281,7 +2298,7 @@ fn collect_descriptors<'a>(
&mut descriptors,
DescriptorData {
id: descriptor.id,
- name: CssString::from_utf16(declaration.name.as_ref()),
+ name: CssString::from_utf16(declaration.name().as_ref()),
value: descriptor.value,
},
);
@@ -2761,47 +2778,55 @@ impl<'a> SyntaxParseBuilder<'a> {
declaration: &Declaration,
font_feature_maximum_value_count: Option<usize>,
) -> usize {
- let (name_offset, name_length) = self.append_declaration_text(declaration.name.as_ref());
let shareable = declaration.is_property && !declaration.value.may_not_be_shared;
let hash = shareable.then(|| declaration.value.hash_of_text(&self.dedup.hasher));
- let shared_value = hash.and_then(|hash| {
- self.dedup
- .values
- .get(&hash)?
- .iter()
- .find(|(text, ..)| text.is_same_text(&declaration.value))
- .map(|(_, number, value)| (*number, value.clone()))
+ let shared = hash.and_then(|hash| {
+ self.dedup.values.find(hash, |text| {
+ text.hash == hash && declaration.value.is_same_text_as(&text.name, &text.value)
+ })
});
- let (text_number, (property_id, descriptor_id, rejection, parsed_value)) = match shared_value {
- Some((number, value)) => (number, value),
- None => {
- let mut value = self.parse_declaration_value(declaration);
- // NB: A value taking part in a substitution keeps its own: it is resolved for the
- // property declaring it, and the longhands a shorthand awaiting substitution
- // expands to name it by identity.
- if shareable
- && let Some(parsed) = &mut value.3
- && !matches!(
- **parsed,
- StyleValueData::Unresolved(_)
- | StyleValueData::PendingSubstitution { .. }
- | StyleValueData::Shorthand(_)
- )
- {
- *parsed = self.dedup.intern_value(parsed);
- }
- let number = self.dedup.text_count;
- if let Some(hash) = hash {
- self.dedup.text_count += 1;
- self.dedup
- .values
- .entry(hash)
- .or_default()
- .push((declaration.value.clone(), number, value.clone()));
+ let (text_number, name_offset, name_length, (property_id, descriptor_id, rejection, parsed_value)) =
+ match shared {
+ // NB: Declarations with the same text have the same name, and share the kept text of it.
+ Some(text) => (text.number, text.name_offset, text.name_length, text.parsed.clone()),
+ None => {
+ let (name_offset, name_length) = self.append_declaration_text(declaration.name().as_ref());
+ let mut value = self.parse_declaration_value(declaration);
+ // NB: A value taking part in a substitution keeps its own: it is resolved for the
+ // property declaring it, and the longhands a shorthand awaiting substitution
+ // expands to name it by identity.
+ if shareable
+ && let Some(parsed) = &mut value.3
+ && !matches!(
+ **parsed,
+ StyleValueData::Unresolved(_)
+ | StyleValueData::PendingSubstitution { .. }
+ | StyleValueData::Shorthand(_)
+ )
+ {
+ *parsed = self.dedup.intern_value(parsed);
+ }
+ let number = self.dedup.text_count;
+ if let Some(hash) = hash {
+ self.dedup.text_count += 1;
+ let (name, range) = declaration.value.ranges();
+ self.dedup.values.insert_unique(
+ hash,
+ DeclarationText {
+ hash,
+ name,
+ value: range,
+ number,
+ name_offset,
+ name_length,
+ parsed: value.clone(),
+ },
+ |text| text.hash,
+ );
+ }
+ (number, name_offset, name_length, value)
}
- (number, value)
- }
- };
+ };
let declaration_key = match hash {
Some(_) => text_number * 2 + u32::from(declaration.important),
None => DeclarationDedup::UNSHARED,
@@ -2864,7 +2889,7 @@ impl<'a> SyntaxParseBuilder<'a> {
return match parse_descriptor(
parse_context,
at_rule,
- declaration.name.as_ref(),
+ declaration.name().as_ref(),
&declaration.value.component_values(),
&declaration.value.text(),
) {
@@ -2877,10 +2902,11 @@ impl<'a> SyntaxParseBuilder<'a> {
None => (u16::MAX, u8::MAX, FfiDeclarationRejection::None, None),
};
}
- let Some(property_id) = property_id_from_name(declaration.name.as_ref()) else {
+ let name = declaration.name();
+ let Some(property_id) = property_id_from_name(name.as_slice()) else {
// NB: Mirrors the ignored-vendor and unknown-property distinction from commit
// dbe9950abd9 in Libraries/LibWeb/CSS/Parser/Parser.cpp:360.
- let rejection = if has_ignored_vendor_prefix(declaration.name.as_ref().into()) {
+ let rejection = if has_ignored_vendor_prefix(name.as_slice().into()) {
FfiDeclarationRejection::IgnoredVendorPrefix
} else {
FfiDeclarationRejection::UnknownProperty
@@ -2888,7 +2914,8 @@ impl<'a> SyntaxParseBuilder<'a> {
return (u16::MAX, u8::MAX, rejection, None);
};
- let mut value_contexts = parse_context.value_contexts.as_slice().to_vec();
+ let mut value_contexts =
+ smallvec::SmallVec::<[FfiValueParsingContext; 8]>::from_slice(parse_context.value_contexts.as_slice());
value_contexts.push(FfiValueParsingContext {
kind: FfiValueParsingContextKind::Property,
value: property_id,
@@ -3435,10 +3462,10 @@ impl<'a> SyntaxParseBuilder<'a> {
if let Some(shared) = block_key.and_then(|(hash, keys)| {
self.dedup
.blocks
- .get(&hash)?
- .iter()
- .find(|(run, _)| self.dedup.declaration_keys[run.clone()] == *keys)
- .map(|(_, block)| block)
+ .find(hash, |(block_hash, run, _)| {
+ *block_hash == hash && self.dedup.declaration_keys[run.clone()] == *keys
+ })
+ .map(|(_, _, block)| block)
}) {
Some(shared.clone())
} else {
@@ -3459,10 +3486,15 @@ impl<'a> SyntaxParseBuilder<'a> {
}
let block = block.finish();
if let Some(hash) = block_hash {
- self.dedup.blocks.entry(hash).or_default().push((
- declarations_start..declarations_start + declaration_count,
- block.clone(),
- ));
+ self.dedup.blocks.insert_unique(
+ hash,
+ (
+ hash,
+ declarations_start..declarations_start + declaration_count,
+ block.clone(),
+ ),
+ |(hash, ..)| *hash,
+ );
}
Some(block)
}
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs
index d56ffb864b0..944cf9a6e75 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser/structure.rs
@@ -27,8 +27,10 @@ use crate::css::parser::block::Unit;
use crate::css::parser::component_value::{
ComponentKind, ComponentValue, ComponentValueList, MAXIMUM_COMPONENT_VALUE_NESTING_DEPTH,
};
-use crate::css::parser::lexer::{Kind, Lexer, Text, Token, flag, name_of, number_of, string_of, url_of};
-use crate::css::parser::rows::closer_of;
+use crate::css::parser::lexer::{
+ Escapes, Kind, Lexer, Text, Token, flag, name_of, number_of, string_of, unescape, url_of,
+};
+use crate::css::parser::rows::{Name, closer_of};
use crate::css::property_metadata::property_id_from_name;
use crate::css::selector_parser::StyleNestingParent;
use smallvec::SmallVec;
@@ -340,10 +342,15 @@ impl<'a, U: Unit> Stream<'a, U> {
let (start, end) = token.name_range();
let name = &self.units[start..end];
name.len() == expected.len()
- && name
- .iter()
- .zip(expected)
- .all(|(unit, &expected)| unit.code() | 0x20 == u32::from(expected) && unit.code() >= 0x41)
+ && name.iter().zip(expected).all(|(unit, &expected)| {
+ let code = unit.code();
+ let lowercase = if (0x41..=0x5A).contains(&code) {
+ code | 0x20
+ } else {
+ code
+ };
+ lowercase == u32::from(expected)
+ })
}
}
@@ -369,8 +376,9 @@ fn component_values_in<U: Unit>(units: &[U], storage: &Arc<SourceStorage>, range
#[derive(Clone)]
pub(crate) struct DeclarationValue {
source: Arc<SourceStorage>,
- /// Where the name of the declaration is, as written.
+ /// Where the name of the declaration is, as written, and whether it has escapes.
name: Range<u32>,
+ name_is_escaped: bool,
range: Range<u32>,
/// Whether parsing the value may do more than produce a value, which sharing the result would skip: `attr()`
/// records the attribute names it reads, and `random()` values have an identity of their own.
@@ -378,6 +386,23 @@ pub(crate) struct DeclarationValue {
}
impl DeclarationValue {
+ /// The name of the declaration.
+ 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());
+ unescape(units, Escapes::Name, &mut output);
+ return Name::from_vec(output);
+ }
+ units.iter().map(|unit| unit.code() as u16).collect()
+ }
+ let range = self.name.start as usize..self.name.end as usize;
+ match self.source.as_ref() {
+ SourceStorage::Latin1(units) => name_in(&units[range], self.name_is_escaped),
+ SourceStorage::Utf16(units) => name_in(&units[range], self.name_is_escaped),
+ }
+ }
+
fn range(&self) -> Range<usize> {
self.range.start as usize..self.range.end as usize
}
@@ -407,19 +432,19 @@ impl DeclarationValue {
}
}
- /// Whether the declaration is written like that of `other`, which is in the same source.
- pub(super) fn is_same_text(&self, other: &Self) -> bool {
- debug_assert!(Arc::ptr_eq(&self.source, &other.source));
+ /// Where the name and the value of the declaration are in the source.
+ pub(super) fn ranges(&self) -> (Range<u32>, Range<u32>) {
+ (self.name.clone(), self.range.clone())
+ }
+
+ /// Whether the declaration is written like one whose name and value are at `name` and `value` in the same source.
+ pub(super) fn is_same_text_as(&self, name: &Range<u32>, value: &Range<u32>) -> bool {
fn same<U: Unit>(units: &[U], first: &Range<u32>, second: &Range<u32>) -> bool {
units[first.start as usize..first.end as usize] == units[second.start as usize..second.end as usize]
}
match self.source.as_ref() {
- SourceStorage::Latin1(units) => {
- same(units, &self.name, &other.name) && same(units, &self.range, &other.range)
- }
- SourceStorage::Utf16(units) => {
- same(units, &self.name, &other.name) && same(units, &self.range, &other.range)
- }
+ SourceStorage::Latin1(units) => same(units, &self.name, name) && same(units, &self.range, value),
+ SourceStorage::Utf16(units) => same(units, &self.name, name) && same(units, &self.range, value),
}
}
}
@@ -899,18 +924,25 @@ impl<'a, U: Unit> Core<'a, U> {
(ends[2], count)
};
- let name = self.stream.name(name_token);
- let is_custom_property = starts_with_ascii(name.as_ref(), b"--");
- if is_custom_property && name.as_ref().len() == 2 {
- return None;
+ let is_custom_property = self.stream.name_starts_with_dashes(name_token);
+ if is_custom_property {
+ // NB: A name of only the two dashes is not a custom property name.
+ let length = if name_token.has(flag::ESCAPED) {
+ self.stream.name(name_token).as_ref().len()
+ } else {
+ (name_token.end - name_token.start) as usize
+ };
+ if length == 2 {
+ return None;
+ }
}
// INTEROP: The initial-value descriptor accepts arbitrary component values, including mixed curly blocks.
let is_initial_value_descriptor = self.rule_context.last() == Some(&RuleContext::AtProperty)
- && equals_ascii_case_insensitive(name.as_ref(), b"initial-value");
+ && self.stream.name_is(name_token, b"initial-value");
if !is_custom_property && !is_initial_value_descriptor && curly_block_count != 0 && curly_block_count != count {
return None;
}
- if !self.declaration_is_valid(name.as_ref()) {
+ if !self.declaration_is_valid(name_token) {
return None;
}
let is_property = self.declaration_is_property();
@@ -920,10 +952,10 @@ impl<'a, U: Unit> Core<'a, U> {
self.rule_context.last().copied().unwrap_or(RuleContext::Unknown)
};
Some(Declaration {
- name,
value: DeclarationValue {
source: self.stream.storage.clone(),
name: name_token.start..name_token.end,
+ name_is_escaped: name_token.has(flag::ESCAPED),
range: value_start as u32..value_end as u32,
may_not_be_shared,
},
@@ -953,7 +985,8 @@ impl<'a, U: Unit> Core<'a, U> {
}
}
- fn declaration_is_valid(&self, name: &[u16]) -> bool {
+ /// Whether a declaration whose name is the token `name` can be where the parser is.
+ fn declaration_is_valid(&self, name: Token) -> bool {
let Some(context) = self.rule_context.last() else {
return false;
};
@@ -971,12 +1004,14 @@ impl<'a, U: Unit> Core<'a, U> {
b"animation-timeline",
]
.iter()
- .any(|property| equals_ascii_case_insensitive(name, property)),
+ .any(|property| self.stream.name_is(name, property)),
RuleContext::AtContainer | RuleContext::AtLayer | RuleContext::AtMedia | RuleContext::AtSupports => self
.rule_context
.iter()
.any(|context| matches!(context, RuleContext::Style | RuleContext::AtFunction)),
- RuleContext::AtPositionTry => property_id_from_name(name).is_some_and(is_accepted_position_try_property),
+ RuleContext::AtPositionTry => {
+ property_id_from_name(self.stream.name(name).as_ref()).is_some_and(is_accepted_position_try_property)
+ }
_ => true,
}
}
@@ -1130,11 +1165,9 @@ impl<'a> Parser<'a> {
#[cfg(test)]
mod tests {
- use super::{Declaration, LineCursor, Parser, Rule, Sink, component_values_in, source_of};
+ use super::{LineCursor, component_values_in, source_of};
use crate::css::css_tokenizer::{ParserTokenKind, SourceStorage, tokenize_for_parser};
use crate::css::parser::component_value::{ComponentKind, ComponentValue, consume_a_list_of_component_values};
- use crate::css::parser::syntax_parser::RuleContext;
-
use std::fmt::Write;
/// A token, with its string as text: the two tokenizers hold strings in different ways.
@@ -1283,6 +1316,9 @@ mod tests {
#[test]
fn a_value_has_no_whitespace_from_after_it() {
+ use super::{Declaration, Parser, Rule, Sink, source_of};
+ use crate::css::parser::syntax_parser::RuleContext;
+
#[derive(Default)]
struct Declarations(Vec<Declaration>);
impl Sink for Declarations {