r/ProgrammingLanguages • u/RedCrafter_LP • Apr 12 '26
Discussion Made a tokenizer
I wrote the tokenizer for my language and it parses 372000Tokens per second.
My specs are i7 6700k 16gb ram.
I wonder rather my tokenizer is slow or fast and I can't find any other benchmarks to compare against.
Update:
I changed some stuff about the code and it's now performing properly. I parse a 1.3M loc test file in 0.9s And for comparison to before it's producing 5M tokens
I replaced the buffered reader with mmap. And the keywords lookup now uses a 2d hashmap grouping the keywords by length instead of having 1 hashmap and checking prefixes.
The project can be found on github: Chorus
View all comments
Show parent comment
13
u/munificent Apr 12 '26
Many years ago I was talking to one of the V8 folks and somehow scanner performance came up. (It may be because I noticed the front end had a whole perfect hash thing and trie for recognizing keywords.) He pointed out that the scanner is the only pass in the front end that has to look at each individual character in the source code and also look at each character in comments.
So while the scanner is
O(n), thatnis significantly larger than the subsequent passes which are operating on more granular objects like entire tokens or AST nodes. That means that scanning can be a bottleneck if you aren't careful and can be worth optimizing if you've done what you can with the other passes.At the same time, some of the semantic analysis and codegen passes may be greater than
O(n), so even though thenis smaller, most compilers still spend (much) more time in the later half of the pipeline.I wouldn't spend significant optimization time in the scanner until I had the whole thing up and running and could see how big a slice of pie it was in the entire pipeline.