Skip to content

Commit

Permalink
added pre-allocating of memory
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser committed Jun 14, 2024
1 parent 0172efb commit d0eb215
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/frontend/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ fn is_skippable(src: char) -> bool {
}

pub fn tokenize(source_code: &str) -> Vec<Token> {
let mut tokens = Vec::new();
let estimated_capacity = source_code.len() / 5; // The number can be changed depending.
// A bigger number results in a smaller initial allocation size.
let mut tokens = Vec::with_capacity(estimated_capacity);
let mut chars = source_code.chars().peekable();
while let Some(&c) = chars.peek() {
if is_skippable(c) {
Expand Down

0 comments on commit d0eb215

Please sign in to comment.