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/gasche Apr 01 '26
I wish this kind of blog posts came with a benchmark to see the actual performance impact. Does it matter in practice?
Some parts of the post are also a bit weird.
Putting everything in the program in a single big table does not necessarily improve locality. (Maybe the author refers to the fact that one can use int32 instead of a full word and hope to improve locality by reducing overall space consumption?) If I use a hashtable or balanced tree that contains metadata for only the variables of the function I am compiling, I may have better locality than with a huge table containing metadata for all variables in the program. (How does that even work when I want temporary metadata for a given analysis on a given function, do I allocate an array whose size is the total numbers of variables in the program? That sounds wasteful. I suppose I rather use a sparse map over the variables in the function, but then the fact that variable IDs are dense/contiguous does not play a role anymore.)
When does it ever happen that you need to iterate over all symbols?