Skip to content

Commit

Permalink
inline: store attributes in vec (squash)
Browse files Browse the repository at this point in the history
benchmarking of this fluctuates a lot..
  • Loading branch information
hellux committed Mar 27, 2023
1 parent 2fe35f2 commit 14f6c41
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
45 changes: 26 additions & 19 deletions src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,23 @@ pub enum QuoteType {
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EventKind<'s> {
pub enum EventKind {
Enter(Container),
Exit(Container),
Atom(Atom),
Str,
Attributes {
container: bool,
attrs: attr::Attributes<'s>,
attrs: AttributesIndex,
},
Placeholder,
}

type AttributesIndex = u32;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Event<'s> {
pub kind: EventKind<'s>,
pub struct Event {
pub kind: EventKind,
pub span: Span,
}

Expand Down Expand Up @@ -208,13 +210,15 @@ pub struct Parser<'s> {
openers: Vec<(Opener, usize)>,
/// Buffer queue for next events. Events are buffered until no modifications due to future
/// characters are needed.
events: std::collections::VecDeque<Event<'s>>,
events: std::collections::VecDeque<Event>,
/// State if inside a verbatim container.
verbatim: Option<VerbatimState>,
/// State if currently parsing potential attributes.
attributes: Option<AttributesElementType>,
/// Storage of cow strs, used to reduce size of [`Container`].
pub(crate) cow_strs: Vec<CowStr<'s>>,
/// Storage of attributes, used to reduce size of [`Container`].
pub(crate) attributes_store: Vec<attr::Attributes<'s>>,
}

pub enum ControlFlow {
Expand All @@ -238,6 +242,7 @@ impl<'s> Parser<'s> {
verbatim: None,
attributes: None,
cow_strs: Vec::new(),
attributes_store: Vec::new(),
}
}

Expand All @@ -253,12 +258,12 @@ impl<'s> Parser<'s> {
debug_assert!(self.verbatim.is_none());
}

fn push_sp(&mut self, kind: EventKind<'s>, span: Span) -> Option<ControlFlow> {
fn push_sp(&mut self, kind: EventKind, span: Span) -> Option<ControlFlow> {
self.events.push_back(Event { kind, span });
Some(Continue)
}

fn push(&mut self, kind: EventKind<'s>) -> Option<ControlFlow> {
fn push(&mut self, kind: EventKind) -> Option<ControlFlow> {
self.push_sp(kind, self.input.span)
}

Expand Down Expand Up @@ -454,10 +459,12 @@ impl<'s> Parser<'s> {
self.input.lexer = lex::Lexer::new(&self.input.src[end_attr..line_end]);

if !attrs.is_empty() {
let attr_index = self.attributes_store.len() as AttributesIndex;
self.attributes_store.push(attrs);
let attr_event = Event {
kind: EventKind::Attributes {
container: matches!(elem_ty, AttributesElementType::Container { .. }),
attrs,
attrs: attr_index,
},
span: self.input.span,
};
Expand Down Expand Up @@ -845,7 +852,7 @@ impl<'s> Parser<'s> {
self.push(EventKind::Atom(atom))
}

fn merge_str_events(&mut self, span_str: Span) -> Event<'s> {
fn merge_str_events(&mut self, span_str: Span) -> Event {
let mut span = span_str;
let should_merge = |e: &Event, span: Span| {
matches!(e.kind, EventKind::Str | EventKind::Placeholder)
Expand All @@ -872,7 +879,7 @@ impl<'s> Parser<'s> {
}
}

fn apply_word_attributes(&mut self, span_str: Span) -> Event<'s> {
fn apply_word_attributes(&mut self, span_str: Span) -> Event {
if let Some(i) = span_str
.of(self.input.src)
.bytes()
Expand Down Expand Up @@ -1047,7 +1054,7 @@ impl From<Opener> for DelimEventKind {
}

impl<'s> Iterator for Parser<'s> {
type Item = Event<'s>;
type Item = Event;

fn next(&mut self) -> Option<Self::Item> {
while self.events.is_empty()
Expand Down Expand Up @@ -1180,7 +1187,7 @@ mod test {
(
Attributes {
container: true,
attrs: [("id", "id")].into_iter().collect()
attrs: 0,
},
"{#id}"
),
Expand Down Expand Up @@ -1371,7 +1378,7 @@ mod test {
(
Attributes {
container: false,
attrs: [("class", "cls")].into_iter().collect(),
attrs: 0,
},
"{.cls}"
),
Expand Down Expand Up @@ -1420,7 +1427,7 @@ mod test {
(
Attributes {
container: true,
attrs: [("class", "def")].into_iter().collect(),
attrs: 0,
},
"{.def}"
),
Expand All @@ -1438,7 +1445,7 @@ mod test {
(
Attributes {
container: true,
attrs: [("class", "bar_")].into_iter().collect(),
attrs: 0,
},
"{.bar_}"
),
Expand Down Expand Up @@ -1548,7 +1555,7 @@ mod test {
(
Attributes {
container: true,
attrs: [("class", "attr")].into_iter().collect(),
attrs: 0,
},
"{.attr}"
),
Expand Down Expand Up @@ -1582,7 +1589,7 @@ mod test {
(
Attributes {
container: true,
attrs: [("class", "a b c")].into_iter().collect(),
attrs: 0,
},
"{.a}{.b}{.c}"
),
Expand All @@ -1600,7 +1607,7 @@ mod test {
(
Attributes {
container: false,
attrs: [("a", "b")].into_iter().collect()
attrs: 0,
},
"{a=b}"
),
Expand All @@ -1614,7 +1621,7 @@ mod test {
(
Attributes {
container: false,
attrs: [("class", "a b")].into_iter().collect(),
attrs: 0,
},
"{.a}{.b}"
),
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,10 @@ impl<'s> Parser<'s> {
inline::Event {
kind: inline::EventKind::Attributes { attrs, .. },
..
} => (self.inline_parser.next(), attrs),
} => (
self.inline_parser.next(),
self.inline_parser.attributes_store[attrs as usize].clone(),
),
inline => (Some(inline), Attributes::new()),
};

Expand Down

0 comments on commit 14f6c41

Please sign in to comment.