From cafe175700183b44d62ce488435ea630e460e113 Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Mon, 12 Feb 2024 14:11:16 +0100 Subject: [PATCH] parser: allocate vector with exact capacity (#894) This tweaks `parse_content()` logic in order to reduce the amount of vector growing, as the final vector capacity is already known beforehand. --- src/parser/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 0a5bf388..07982c1a 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -1044,9 +1044,10 @@ fn parse_if(pair: Pair) -> TeraResult { } fn parse_content(pair: Pair) -> TeraResult> { - let mut nodes = vec![]; + let pairs = pair.into_inner(); + let mut nodes = Vec::with_capacity(pairs.len()); - for p in pair.into_inner() { + for p in pairs { match p.as_rule() { Rule::include_tag => nodes.push(parse_include(p)), Rule::comment_tag => nodes.push(parse_comment_tag(p)),