r/ProgrammingLanguages • u/PitifulTheme411 ... • Jul 31 '26
Discussion Is it Good Practice to Intern Identifiers?
I'm working on my language and am working on the alias system, which isn't relevant to this discussion other than the fact that it operates on identifiers and other lexemes (like operators). As I was implementing it and was reading some prior stuff, I found somebody saying that it is good practice to intern all the identifiers as the lexer comes across them. Then in the AST you can just use the corresponding LexemeId or whatever.
Apparently this is used pretty extensively in many popular languages' compilers, so I was wondering if this is good practice or necessary? I am thinking that if it is a good idea, that I can implement it in my language as well, because I think it may simplify a few things (including my alias stuff).
14
u/Athas Futhark Jul 31 '26
I think it is a bad idea to do it by interning strings. I do it by associating each identifier with a unique integer tag during name resolution, meaning the same identifier in different scopes will have different tags. That means I also don't have to worry about unintended name capture when moving code around (although I have to do renaming when duplicating code when e.g. inlining).
26
u/alphaglosined Jul 31 '26
Yes, it is a necessary performance optimisation that doesn't cost much.
A single pointer comparison vs array comparisons all over the place? Yeah, one is clearly the winner.
As a nice consequence, you can do stuff like
tok == Id.while_so keywords only need to be added to the table, and then it's just a simple global read.15
u/qurious-crow Jul 31 '26
My under-caffeinated brain read that as "Is it good practice to identify interns". After due consideration, I think yes, you probably should have some idea about who your interns are :)
1
u/QubitEncoder Jul 31 '26
What language are you writing,m
1
u/eightrx Jul 31 '26
You could just push tokens in a struct of arrays pattern, and then use indexes to refer to tokens attributes. This doesn't give you uniqueness but is much simpler and can be faster
1
u/superstar64 https://github.com/Superstar64/Hazy Jul 31 '26
Since I'm writing my compiler in Haskell, doing the interning in the lexer isn't an option for me. Instead what I do is I have a symbol resolution pass where I just assign each variable to the index of where it's defined. This symbol resolution is the main place string comparisons occur, as once it's done you can just use the indexes.
Consider this example:
After renaming it becomes something like:
1
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Jul 31 '26
The "intern" term comes from the Java String class. In compilers, you'll find the term "hash consing" used much more widely. Same thing with a different name.
Remember to bucket things into three categories: (i) easiest way to get something to work, (ii) a nice to have optimization, and (iii) something that takes effort but pays significant dividends in terms of capability or optimization. As you're learning, spend most of your budget on #1, because otherwise all your time will disappear. But don't be afraid to then allocate some portion of time to the other two categories, as long as they connect well with the #1 category. Hash consing is definitely in category #2 and sometimes #3, but it's not required to get things working.
-1
u/Recycled5000 Jul 31 '26
Not a good idea. Limited interning would be ok, limited to a load of code. But don’t make the runtime make any guarantees of interning beyond the load unit.
0
u/arthurno1 Jul 31 '26 edited Jul 31 '26
Ttpically we use perfect hashing for keywords, since keywords are a static set. Lookup gperf for that one. For the identifiers in the language, like aliases are, use a hash map, since they change dynamically during the program. That is a standard solution to the problem.
Yes, interning is a standard technique, you don't want to compare strings all the time, because it is slow. It is a subject of basic compiler course, so if you ask that, you have probably not done any research and reading.
I suggest to invest in the Dragon book, and find some online free books, tutorials and courses on compiler and language design, if you can't take a course at some university.
-2
u/umlcat Jul 31 '26
What do you mean "inter Identifiers" ? Could you provide, please, a source code example ???