A Faster Lexer in Go
eli.thegreenplace.net
eli.thegreenplace.net
Unless you're parsing genuinely enormous amounts of code (or latency bound) I find that most compilers are far too slow in other areas to justify optimizing the lexer versus fixing architectural issues in (say) semantic analysis.
A lot of parts of compilers are non-local, and involve lots of pointer chasing, which means avoiding repeated work is very important. In the D compiler there a bunch of checks where we stop you shooting yourself in the foot, I think generating all of these tree-walkers from a specification (so you can walk it all in one go) would probably be a decent speedup.
(That being said, I have no idea if LLVM would merge a TableGen lexer written in Go.)
According to a cloc definition I just did:
LLVM Table Gen
files: 1041
blank: 75684
comment: 69272
code: 480047
Probably enough to warrant a faster lexer.I can highly recommend this video for anyone wanting to build something satisfying in Go as a fairly simple project.
[1] https://github.com/eigenhombre/l1 [2] https://github.com/eigenhombre/lexutil
Not really important revelations or anything, but as a person who does not do development professionally, it is nice to feel like I may be developing decent instincts for the craft.
Print sends a newline by default but i don’t think it flushes explicitly (hence the flush keyword parameter).
The general problem seems to be about dynamic dispatch of methods, in go using fat pointers, so the author avoid the dynamic dispatch by modifing the code.
It's the original input buffer. It's less fragmented than creating small copies for every token. The backing store stays alive as long as there's a single token pointing to it. It's a bit inefficient if there's a lot of whitespace, comment, etc. in it, or if you're only interested in a very small portion of the tokens, but it doesn't have the overhead of a memory block per string (which is at least 16 bytes). So it depends, I guess.
I don't think dynamic dispatch comes into play in go. The strings are represented in the same way.