r/ProgrammingLanguages • • 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.

https://aikoschurmann.com/blog/string-interning-compilers

11 Upvotes

11 comments sorted by

View all comments

Show parent comment

2

u/Creative-Cup-6326 Apr 01 '26

Well, you have memory reduction. Later symbol lookup becomes O(1) which stays O(L) with a hashmap. You can intern types too, then you don’t need to do a recursive hash on it since types can get very complex etc.

2

u/Valuable_Leopard_799 Apr 01 '26

I did say the HashMap is used once for lookup, as do you.

The type interning is neat.

I had a thought, you wanted dense IDs to do fast lookups in scopes for example, but without some hashtable functionality around that lookup you'd have to allocate a large part of the symbol space for every place in the compiler you want to associate things with a symbol.

How would you handle having 100s of symbols and 4 to 5 levels of nested scope? Maybe the Array of Stacks you mentioned could help, hm.