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).
View all comments
Show parent comment
0
u/PitifulTheme411 ... Jul 31 '26
Yeah I'm also going to do that, but I think the purpose for identifier interning is moreso for the parser. ie. when storing the AST, I don't need to store a pointer to the source file. Also it would probably be nice for my alias resolution system as I don't need to do string equality checks but just integer checks (though then again I'm going to be using a stack of hashmaps, so maybe there is no difference).
My current plan is to have an identifier (and operator) interner that gets filled during lexing and used by the parser until name resolution (and macro expansion because macros need to know the actual names of the things), and then it can be discarded in place of the symbol table which stores something like a
SymbolIdand any corresponding information (type, etc). Of course that would respect scopes while the previous would respect actual lexemes.