Skip to content

Commit

Permalink
Attribute buffer is no longer shared, removing one RefCell.
Browse files Browse the repository at this point in the history
  • Loading branch information
orium committed Sep 4, 2024
1 parent 7db3d8d commit a9dfa1c
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/base/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub trait Align {
fn align(&mut self, offset: usize);
}

impl<T: Align> Align for Vec<T> {
impl<T: Align> Align for &mut [T] {
#[inline]
fn align(&mut self, offset: usize) {
for item in self.iter_mut() {
Expand Down
12 changes: 8 additions & 4 deletions src/parser/lexer/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,11 @@ impl<S: LexemeSink> StateMachineActions for Lexer<S> {

#[inline]
fn create_start_tag(&mut self, _context: &mut ParserContext<S>, _input: &[u8]) {
self.attr_buffer.borrow_mut().clear();

self.current_tag_token = Some(StartTag {
name: Range::default(),
name_hash: LocalNameHash::new(),
ns: Namespace::default(),
attributes: Rc::clone(&self.attr_buffer),
attributes: Vec::new(),
self_closing: false,
});
}
Expand Down Expand Up @@ -314,7 +312,13 @@ impl<S: LexemeSink> StateMachineActions for Lexer<S> {
#[inline]
fn finish_attr(&mut self, _context: &mut ParserContext<S>, _input: &[u8]) {
if let Some(attr) = self.current_attr.take() {
self.attr_buffer.borrow_mut().push(attr);
match self.current_tag_token.as_mut() {
None => unreachable!(),
Some(EndTag { .. }) => unreachable!(),
Some(StartTag { attributes, .. }) => {
attributes.push(attr);
}
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/parser/lexer/lexeme/token_outline.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::base::{Align, Range};
use crate::html::{LocalNameHash, Namespace, TextType};
use crate::parser::SharedAttributeBuffer;
use crate::parser::AttributeBuffer;

#[derive(Debug, Default, Copy, Clone)]
pub struct AttributeOutline {
Expand All @@ -24,7 +24,7 @@ pub enum TagTokenOutline {
name: Range,
name_hash: LocalNameHash,
ns: Namespace,
attributes: SharedAttributeBuffer,
attributes: AttributeBuffer,
self_closing: bool,
},

Expand Down Expand Up @@ -57,7 +57,7 @@ impl Align for TagTokenOutline {
name, attributes, ..
} => {
name.align(offset);
attributes.borrow_mut().align(offset);
attributes.as_mut_slice().align(offset);
}
TagTokenOutline::EndTag { name, .. } => name.align(offset),
}
Expand Down
14 changes: 3 additions & 11 deletions src/parser/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ mod actions;
mod conditions;
mod lexeme;

pub use self::lexeme::*;
use crate::base::{Align, Range};
use crate::html::{LocalNameHash, Namespace, TextType};
use crate::parser::state_machine::{
ActionError, ActionResult, FeedbackDirective, StateMachine, StateResult,
};
use crate::parser::{ParserContext, ParserDirective, ParsingAmbiguityError, TreeBuilderFeedback};
use crate::rewriter::RewritingError;
use std::cell::RefCell;
use std::rc::Rc;

pub use self::lexeme::*;

const DEFAULT_ATTR_BUFFER_CAPACITY: usize = 256;

pub trait LexemeSink {
fn handle_tag(&mut self, lexeme: &TagLexeme) -> Result<ParserDirective, RewritingError>;
Expand All @@ -27,7 +22,8 @@ pub trait LexemeSink {
}

pub type State<S> = fn(&mut Lexer<S>, context: &mut ParserContext<S>, &[u8]) -> StateResult;
pub type SharedAttributeBuffer = Rc<RefCell<Vec<AttributeOutline>>>;

pub type AttributeBuffer = Vec<AttributeOutline>;

pub struct Lexer<S: LexemeSink> {
next_pos: usize,
Expand All @@ -42,7 +38,6 @@ pub struct Lexer<S: LexemeSink> {
current_attr: Option<AttributeOutline>,
last_start_tag_name_hash: LocalNameHash,
closing_quote: u8,
attr_buffer: SharedAttributeBuffer,
last_text_type: TextType,
feedback_directive: FeedbackDirective,
}
Expand All @@ -62,9 +57,6 @@ impl<S: LexemeSink> Lexer<S> {
current_attr: None,
last_start_tag_name_hash: LocalNameHash::default(),
closing_quote: b'"',
attr_buffer: Rc::new(RefCell::new(Vec::with_capacity(
DEFAULT_ATTR_BUFFER_CAPACITY,
))),
last_text_type: TextType::Data,
feedback_directive: FeedbackDirective::None,
}
Expand Down
4 changes: 2 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ mod tree_builder_simulator;

use self::lexer::Lexer;
pub use self::lexer::{
AttributeOutline, Lexeme, LexemeSink, NonTagContentLexeme, NonTagContentTokenOutline,
SharedAttributeBuffer, TagLexeme, TagTokenOutline,
AttributeBuffer, AttributeOutline, Lexeme, LexemeSink, NonTagContentLexeme,
NonTagContentTokenOutline, TagLexeme, TagTokenOutline,
};
use self::state_machine::{ActionError, ParsingTermination, StateMachine};
pub use self::tag_scanner::TagHintSink;
Expand Down
4 changes: 2 additions & 2 deletions src/parser/tree_builder_simulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl TreeBuilderSimulator {
// to decide on foreign context exit
return request_lexeme(|this, lexeme| {
expect_tag!(lexeme, StartTag { ref attributes, .. } => {
for attr in attributes.borrow().iter() {
for attr in attributes.iter() {
let name = lexeme.part(attr.name);

if eq_case_insensitive(&name, b"color")
Expand Down Expand Up @@ -279,7 +279,7 @@ impl TreeBuilderSimulator {
let name = lexeme.part(name);

if !self_closing && eq_case_insensitive(&name, b"annotation-xml") {
for attr in attributes.borrow().iter() {
for attr in attributes.iter() {
let name = lexeme.part(attr.name);
let value = lexeme.part(attr.value);

Expand Down
11 changes: 5 additions & 6 deletions src/rewritable_units/tokens/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::base::Bytes;
use crate::parser::SharedAttributeBuffer;
use crate::parser::AttributeBuffer;
use crate::rewritable_units::Serialize;
use encoding_rs::Encoding;
use lazycell::LazyCell;
Expand Down Expand Up @@ -139,15 +139,15 @@ impl Debug for Attribute<'_> {

pub struct Attributes<'i> {
input: &'i Bytes<'i>,
attribute_buffer: SharedAttributeBuffer,
attribute_buffer: &'i AttributeBuffer,
items: LazyCell<Vec<Attribute<'i>>>,
encoding: &'static Encoding,
}

impl<'i> Attributes<'i> {
pub(super) fn new(
input: &'i Bytes<'i>,
attribute_buffer: SharedAttributeBuffer,
attribute_buffer: &'i AttributeBuffer,
encoding: &'static Encoding,
) -> Self {
Attributes {
Expand Down Expand Up @@ -196,7 +196,6 @@ impl<'i> Attributes<'i> {

fn init_items(&self) -> Vec<Attribute<'i>> {
self.attribute_buffer
.borrow()
.iter()
.map(|a| {
Attribute::new(
Expand Down Expand Up @@ -227,8 +226,8 @@ impl<'i> Attributes<'i> {
}

#[cfg(test)]
pub fn raw_attributes(&self) -> (&'i Bytes<'i>, SharedAttributeBuffer) {
(self.input, std::rc::Rc::clone(&self.attribute_buffer))
pub fn raw_attributes(&self) -> (&'i Bytes<'i>, &'i AttributeBuffer) {
(self.input, self.attribute_buffer)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/rewritable_units/tokens/capturer/to_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use super::*;
use crate::html::TextType;
use crate::parser::{NonTagContentLexeme, NonTagContentTokenOutline, TagLexeme, TagTokenOutline};
use encoding_rs::Encoding;
use std::rc::Rc;

pub enum ToTokenResult<'i> {
Token(Box<Token<'i>>),
Expand Down Expand Up @@ -44,7 +43,7 @@ impl ToToken for TagLexeme<'_> {

StartTag::new_token(
self.part(name),
Attributes::new(self.input(), Rc::clone(attributes), encoding),
Attributes::new(self.input(), attributes, encoding),
ns,
self_closing,
self.raw(),
Expand Down
2 changes: 1 addition & 1 deletion src/rewritable_units/tokens/start_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<'i> StartTag<'i> {
}

#[cfg(test)]
pub fn raw_attributes(&self) -> (&'i Bytes<'i>, crate::parser::SharedAttributeBuffer) {
pub fn raw_attributes(&self) -> (&'i Bytes<'i>, &'i crate::parser::AttributeBuffer) {
self.attributes.raw_attributes()
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/selectors_vm/attribute_matcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::compiler::AttrExprOperands;
use crate::base::Bytes;
use crate::html::Namespace;
use crate::parser::{AttributeOutline, SharedAttributeBuffer};
use crate::parser::{AttributeBuffer, AttributeOutline};
use encoding_rs::UTF_8;
use lazy_static::lazy_static;
use lazycell::LazyCell;
Expand All @@ -22,15 +22,15 @@ type MemoizedAttrValue<'i> = LazyCell<Option<Bytes<'i>>>;

pub struct AttributeMatcher<'i> {
input: &'i Bytes<'i>,
attributes: SharedAttributeBuffer,
attributes: &'i AttributeBuffer,
id: MemoizedAttrValue<'i>,
class: MemoizedAttrValue<'i>,
is_html_element: bool,
}

impl<'i> AttributeMatcher<'i> {
#[inline]
pub fn new(input: &'i Bytes<'i>, attributes: SharedAttributeBuffer, ns: Namespace) -> Self {
pub fn new(input: &'i Bytes<'i>, attributes: &'i AttributeBuffer, ns: Namespace) -> Self {
AttributeMatcher {
input,
attributes,
Expand All @@ -43,7 +43,6 @@ impl<'i> AttributeMatcher<'i> {
#[inline]
fn find(&self, lowercased_name: &Bytes) -> Option<AttributeOutline> {
self.attributes
.borrow()
.iter()
.find(|a| {
if lowercased_name.len() != a.name.end - a.name.start {
Expand Down
16 changes: 8 additions & 8 deletions src/transform_stream/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use super::*;
use crate::base::{Bytes, Range, SharedEncoding};
use crate::html::{LocalName, Namespace};
use crate::parser::{
Lexeme, LexemeSink, NonTagContentLexeme, ParserDirective, ParserOutputSink, TagHintSink,
TagLexeme, TagTokenOutline,
AttributeBuffer, Lexeme, LexemeSink, NonTagContentLexeme, ParserDirective, ParserOutputSink,
TagHintSink, TagLexeme, TagTokenOutline,
};
use crate::rewritable_units::{
DocumentEnd, Serialize, ToToken, Token, TokenCaptureFlags, TokenCapturer, TokenCapturerEvent,
};
use crate::rewriter::RewritingError;
use std::rc::Rc;

use TagTokenOutline::*;

pub struct AuxStartTagInfo<'i> {
pub input: &'i Bytes<'i>,
pub attr_buffer: SharedAttributeBuffer,
pub attr_buffer: &'i AttributeBuffer,
pub self_closing: bool,
}

Expand Down Expand Up @@ -189,7 +187,7 @@ where
&mut self.transform_controller,
AuxStartTagInfo {
input,
attr_buffer: Rc::clone($attributes),
attr_buffer: $attributes,
self_closing: $self_closing,
},
)
Expand All @@ -204,7 +202,9 @@ where
ref attributes,
self_closing,
..
} => get_flags_from_aux_info_res!(aux_info_req, attributes, self_closing),
} => {
get_flags_from_aux_info_res!(aux_info_req, &attributes, self_closing)
}
_ => unreachable!("Tag should be a start tag at this point"),
},

Expand All @@ -223,7 +223,7 @@ where
match self.transform_controller.handle_start_tag(name, ns) {
Ok(flags) => Ok(flags),
Err(DispatcherError::InfoRequest(aux_info_req)) => {
get_flags_from_aux_info_res!(aux_info_req, attributes, self_closing)
get_flags_from_aux_info_res!(aux_info_req, &attributes, self_closing)
}
Err(DispatcherError::RewritingError(e)) => Err(e),
}
Expand Down
2 changes: 1 addition & 1 deletion src/transform_stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod dispatcher;
use self::dispatcher::Dispatcher;
use crate::base::SharedEncoding;
use crate::memory::{Arena, SharedMemoryLimiter};
use crate::parser::{Parser, ParserDirective, SharedAttributeBuffer};
use crate::parser::{Parser, ParserDirective};
use crate::rewriter::RewritingError;

pub use self::dispatcher::{
Expand Down

0 comments on commit a9dfa1c

Please sign in to comment.