Skip to content

Commit f1b6891

Browse files
pre-allocate vec for lexing of template
1 parent 948ce3f commit f1b6891

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

crates/djls-templates/src/lexer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ impl Lexer {
2020
}
2121

2222
pub fn tokenize(&mut self) -> Vec<Token> {
23-
let mut tokens = Vec::new();
23+
// Conservative estimate: most templates have 1 token per 15-20 chars
24+
// Min 32 to avoid reallocation for tiny templates
25+
// Max 1024 to avoid over-allocation for huge templates
26+
let estimated_tokens = (self.source.len() / 15).clamp(32, 1024);
27+
let mut tokens = Vec::with_capacity(estimated_tokens);
2428

2529
while !self.is_at_end() {
2630
self.start = self.current;

0 commit comments

Comments
 (0)