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
8
u/awoocent Apr 12 '26
You're totally correct, I just wanted to add that space can be a big issue with lexers as well as time. Not only are you visiting every source byte, but a lot of source bytes will convert 1:1 into tokens - separators, operators, etc. If your token holds onto like, a string view, plus a source position (maybe even start and end), it can easily get up to the range of 16-32 bytes. I think I've seen tokens as big as 64 bytes in production! Since a lot of these separators don't show up directly in the footprint of the later AST, it is possible for the token list to be the single biggest compilation artifact in the whole pipeline, in memory terms - which of course also slows the lexer. So I think optimizing the data structures used in the lexer is actually really important for compilation time.