Truly independent web browser
LibWeb: Take a spare attribute list only for tags with attributes
Andreas Kling5e8b1866690a
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/token.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/token.rsindex 7a4a0fef9a2..ec0d0f72046 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/token.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/token.rs@@ -114,16 +114,6 @@ pub struct Attribute {pub value_end_offset: SourceOffset,}-impl Attribute {- /// An empty attribute, with a spare value buffer when a dropped tag token left one.- pub fn with_spare_value_buffer() -> Self {- Self {- value: SPARE_ATTRIBUTE_VALUES.with_borrow_mut(Vec::pop).unwrap_or_default(),- ..Default::default()- }- }-}-const MAX_SPARE_ATTRIBUTE_LISTS: usize = 4;const MAX_SPARE_ATTRIBUTE_VALUES: usize = 64;// Buffers grown past these sizes by an unusually large tag are freed rather than kept, so the spare@@ -137,13 +127,25 @@ thread_local! {static SPARE_ATTRIBUTE_VALUES: RefCell<Vec<CodeUnits>> = const { RefCell::new(Vec::new()) };}-/// The attributes of a tag token, backed by a spare buffer when one is available.+/// The attributes of a tag token, backed by a spare buffer when there is one and the tag has attributes.#[derive(Debug)]pub struct AttributeList(Vec<Attribute>);impl AttributeList {pub fn new() -> Self {- Self(SPARE_ATTRIBUTE_LISTS.with_borrow_mut(Vec::pop).unwrap_or_default())+ Self(Vec::new())+ }++ /// Adds an empty attribute, with a spare value buffer when a dropped tag token left one.+ pub fn start_new_attribute(&mut self) {+ // NB: Most tags have no attributes, so a list only takes a spare buffer once it has an attribute to hold.+ if self.0.capacity() == 0 {+ self.0 = SPARE_ATTRIBUTE_LISTS.with_borrow_mut(Vec::pop).unwrap_or_default();+ }+ self.0.push(Attribute {+ value: SPARE_ATTRIBUTE_VALUES.with_borrow_mut(Vec::pop).unwrap_or_default(),+ ..Default::default()+ });}}@@ -562,6 +564,14 @@ impl Token {}}+ #[inline(always)]+ pub fn start_new_attribute(&mut self) {+ match &mut self.payload {+ TokenPayload::Tag { attributes, .. } => attributes.start_new_attribute(),+ _ => panic!("start_new_attribute called on non-tag token"),+ }+ }+#[inline(always)]pub fn attributes_mut(&mut self) -> &mut Vec<Attribute> {match &mut self.payload {@@ -642,8 +652,13 @@ mod spare_buffer_tests {drop(list);assert_eq!(spare_value_capacities().len(), 2);assert_eq!(spare_list_capacities().len(), 1);- assert!(AttributeList::new().capacity() >= 2);- assert!(Attribute::with_spare_value_buffer().value.capacity() >= 16);+ let mut list = AttributeList::new();+ assert_eq!(list.capacity(), 0);+ assert_eq!(spare_list_capacities().len(), 1);+ list.start_new_attribute();+ assert!(list.capacity() >= 2);+ assert!(list[0].value.capacity() >= 16);+ assert!(spare_list_capacities().is_empty());}#[test]diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rsindex 405c4c2a92b..6ce3df6f753 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/tokenizer.rs@@ -10,7 +10,6 @@ use crate::code_units::CodeUnits;use crate::code_units::InputRun;use crate::entities::NamedCharacterReferenceMatcher;use crate::scan;-use crate::token::Attribute;use crate::token::DoctypeData;use crate::token::HtmlName;use crate::token::Position;@@ -1290,17 +1289,13 @@ impl HtmlTokenizer {}Some(0x3D) => {// '=' - parse error- self.current_token- .attributes_mut()- .push(Attribute::with_spare_value_buffer());+ self.current_token.start_new_attribute();self.current_builder.push_ascii(b'=');self.state = State::AttributeName;continue;}Some(_) => {- self.current_token- .attributes_mut()- .push(Attribute::with_spare_value_buffer());+ self.current_token.start_new_attribute();self.reconsume(State::AttributeName);continue;}@@ -1398,9 +1393,7 @@ impl HtmlTokenizer {return self.queued_tokens.pop_front();}Some(_) => {- self.current_token- .attributes_mut()- .push(Attribute::with_spare_value_buffer());+ self.current_token.start_new_attribute();self.reconsume(State::AttributeName);continue;}