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
32
u/Big-Rub9545 Apr 12 '26
You can look at available speeds for production-grade compilers, but there are two points to keep in mind here:
14
u/matthieum Apr 12 '26
AFAIK tokenization tends to be measured in input size (bytes/second or lines/second) rather than number of tokens emitted.
I do think the number of tokens matter too -- there's overhead emitting each token -- but since the tokenizer is likely to scan every byte, input size definitely matters too. Consider, for example, that comments until end of line, or strings, can be dozens to hundreds of bytes long, yet represented as a single token.
You may be interested in Chandler's Carruth CppNow 2023 talk about Carbon: https://www.youtube.com/watch?v=ZI198eFghJk
The slides are available at https://chandlerc.blog/slides/2023-cppnow-compiler/index.html (though they do not display so well for me), and in particular performance targets at https://chandlerc.blog/slides/2023-cppnow-compiler/index.html#/27/1/3:
So... 400k tokens/s is pretty low compare to that.
2
u/GoblinsGym Apr 12 '26
That seems pretty slow to me. I'm afraid I can't give you quantified numbers, as I don't have the tokenizer split out.
If you care about performance, read the entire source file into a buffer first (eliminates getc overhead). Use a hashed symbol table for fast lookup of symbols / keywords.
1
u/matthieum Apr 13 '26
Beware that
mmapmeans that updates to the file are reflected in the memory, this may cause:If using Rust, I suggest
fs::read_to_stringto load the entire file toStringat once, which has several benefits:For context, the exact code for the read takes care of quite a few details for you.
You could lock the file to fully prevent tearing, but this comes with some overhead too... so it may just not be worth it given how small the window of opportunity is, and that the user should not be adversarial against themselves.
And once you've loaded the content in memory, you can be assured it won't change, and therefore you can aggressively reference it.
1
u/Mighto-360 Apr 12 '26
I don’t know your language, but parse time per token could vary wildly depending on the token type and length. Instead, bench based on file size (GB/s, not tokens/s). That will give you a much better idea of how it would perform on a large codebase
1
u/joonazan Apr 12 '26
As long as your tokens are defined by a regular language, the tokenizer is a DFA, so every byte just moves to a different state.
Finding the next state is the critical path. A token might also be emitted but we can ignore that since the CPU can do that in parallel mostly.
Reading the byte itself will take very little time since it is a very predictable read. At most 4 cycles for accessing L1. This is only necessary every 8th time but let's keep it simple.
The state transitions can most likely fit into L1 but it is a bit unclear what a good representation is like. The absolute worst case with a reasonable implementation would be scanning through 256 different options for every character. Every failed comparison (load, cmp, don't jump) has a latency of 5 on AArch64, but may have better throughput, so it isn't unreasonable to say that the absolute worst case would cost 1k cycles per byte.
Even in that unrealistically bad case, you'd be able to process multiple millions of tokens per second. So yes, your code is slow. The only caveat is that very complex tokenization rules maybe can't fit into L1.
If you want to have fast tokenization, you should just use a regular expression compiler. It is essentially a solved problem.
4
u/[deleted] Apr 12 '26
[removed] — view removed comment