From 48500ef79fe8a6a06229d0f2e5a19d9580247dcb Mon Sep 17 00:00:00 2001 From: William Bain Date: Sat, 29 Jun 2024 19:29:47 -0400 Subject: [PATCH] do not compute line offsets after the last token Previously when constructing the root Pairs instance line offsets were computed for the full input string. If the parse stopped before EOI this can require a substantial amount of unnecessary work. --- pest/src/iterators/flat_pairs.rs | 3 ++- pest/src/iterators/pairs.rs | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pest/src/iterators/flat_pairs.rs b/pest/src/iterators/flat_pairs.rs index 6c4f16c1..8af70fd2 100644 --- a/pest/src/iterators/flat_pairs.rs +++ b/pest/src/iterators/flat_pairs.rs @@ -32,13 +32,14 @@ pub struct FlatPairs<'i, R> { pub fn new<'i, R: RuleType>( queue: Rc>>, input: &'i str, + line_index: Rc, start: usize, end: usize, ) -> FlatPairs<'i, R> { FlatPairs { queue, input, - line_index: Rc::new(LineIndex::new(input)), + line_index, start, end, } diff --git a/pest/src/iterators/pairs.rs b/pest/src/iterators/pairs.rs index a98ceb71..d8106299 100644 --- a/pest/src/iterators/pairs.rs +++ b/pest/src/iterators/pairs.rs @@ -51,7 +51,17 @@ pub fn new<'i, R: RuleType>( ) -> Pairs<'i, R> { let line_index = match line_index { Some(line_index) => line_index, - None => Rc::new(LineIndex::new(input)), + None => { + let last_input_pos = queue + .last() + .map(|token| match *token { + QueueableToken::Start { input_pos, .. } + | QueueableToken::End { input_pos, .. } => input_pos, + }) + .unwrap_or(0); + + Rc::new(LineIndex::new(&input[..last_input_pos])) + } }; let mut pairs_count = 0; @@ -205,7 +215,13 @@ impl<'i, R: RuleType> Pairs<'i, R> { /// ``` #[inline] pub fn flatten(self) -> FlatPairs<'i, R> { - flat_pairs::new(self.queue, self.input, self.start, self.end) + flat_pairs::new( + self.queue, + self.input, + self.line_index, + self.start, + self.end, + ) } /// Finds the first pair that has its node or branch tagged with the provided