r/ProgrammingLanguages • u/Creative-Cup-6326 • Mar 31 '26
Using string interning to optimize symbol resolution in compilers
Hey everyone, I'm building a custom compiler from scratch and wanted to talk about how string interning can massively optimize it.
I wrote a short post on my approach using a Dense Arena Interner to turn slow string comparisons into instant O(1) integer checks across the parsing and typechecking pipeline. Would love to hear how you all handle this.
11
Upvotes
View all comments
2
u/eightrx Apr 01 '26
I use zig's std.HashMapUnmanaged with custom 'Context' and 'Adapter' structs and the getOrPutContextAdapted method
Strings are stored contiguously in a single buffer, null terminated. The map hashes the string without copying it as a key, and returns a u32 index into the buffer of strings, which the Context and Adapter structs convey to the hashmap
For keywords, I do this during lexing with std.meta.stringToEnum