Skip to content

Truly independent web browser

LibWeb: Reuse the memory of the builder of CSS declaration blocks

Andreas Klingbc6551ecc8c9
diff --git a/Libraries/LibWeb/Rust/src/css/declaration_block.rs b/Libraries/LibWeb/Rust/src/css/declaration_block.rs
index 820de16ea57..2dc32984e22 100644
--- a/Libraries/LibWeb/Rust/src/css/declaration_block.rs
+++ b/Libraries/LibWeb/Rust/src/css/declaration_block.rs
@@ -582,7 +582,12 @@ pub(crate) struct DeclarationBlockBuilder {
}
impl DeclarationBlockBuilder {
- pub(crate) fn finish(self) -> SharedDeclarationBlock {
+ pub(crate) fn finish(mut self) -> SharedDeclarationBlock {
+ self.finish_and_reuse()
+ }
+
+ /// Freezes the declarations into a block, and keeps the memory of the builder for the next one.
+ pub(crate) fn finish_and_reuse(&mut self) -> SharedDeclarationBlock {
let property_count = u32::try_from(self.properties.len()).expect("declaration block too large");
let custom_property_count = u32::try_from(self.custom_properties.len()).expect("declaration block too large");
let layout = block_layout(self.properties.len(), self.custom_properties.len());
@@ -620,11 +625,11 @@ impl DeclarationBlockBuilder {
let entries = base.add(layout.entries).cast::<DeclarationEntry>();
let custom_properties = self
.custom_properties
- .into_iter()
+ .drain(..)
.map(|property| (Some(property.name), property.declaration));
let declarations = self
.properties
- .into_iter()
+ .drain(..)
.map(|property| (None, property))
.chain(custom_properties);
for (index, (name, declaration)) in declarations.enumerate() {
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
index 484e2a9da32..09276d530b9 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
@@ -1928,6 +1928,8 @@ struct SyntaxParseBuilder<'a> {
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 memory for the declarations of the block that is built.
+ block_scratch: DeclarationBlockBuilder,
/// The names that the selectors of the style rules name.
names: RefCell<crate::css::selector_parser::NameCache>,
/// What a `&` in the rules being appended names, or none outside every rule.
@@ -2788,6 +2790,7 @@ impl<'a> SyntaxParseBuilder<'a> {
roots_are_items: false,
value_scratch: Vec::new(),
text_scratch: Vec::new(),
+ block_scratch: DeclarationBlockBuilder::default(),
names: RefCell::default(),
nesting: Cell::new(None),
parent: Cell::new(None),
@@ -3139,7 +3142,7 @@ impl<'a> SyntaxParseBuilder<'a> {
font_feature_maximum_value_count: Option<usize>,
) -> ParsedRuleChild {
let (start, count, descriptor_block) = self.append_declarations(declarations, font_feature_maximum_value_count);
- let mut block = DeclarationBlockBuilder::default();
+ let mut block = std::mem::take(&mut self.block_scratch);
self.append_declaration_block(
&mut block,
start,
@@ -3154,7 +3157,8 @@ impl<'a> SyntaxParseBuilder<'a> {
)
}),
);
- let declaration_block = Some(block.finish());
+ let declaration_block = Some(block.finish_and_reuse());
+ self.block_scratch = block;
let local_identity = self.next_identity();
let identity_index = usize::try_from(local_identity).expect("parsed declaration identity overflow");
self.declaration_ranges
@@ -3570,7 +3574,7 @@ impl<'a> SyntaxParseBuilder<'a> {
}) {
Some(shared.clone())
} else {
- let mut block = DeclarationBlockBuilder::default();
+ let mut block = std::mem::take(&mut self.block_scratch);
self.append_declaration_block(
&mut block,
declarations_start,
@@ -3585,7 +3589,9 @@ impl<'a> SyntaxParseBuilder<'a> {
}
}
}
- let block = block.finish();
+ let finished = block.finish_and_reuse();
+ self.block_scratch = block;
+ let block = finished;
if let Some(hash) = block_hash {
self.dedup.blocks.insert_unique(
hash,