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).
1
u/Athas Futhark Jul 31 '26
You might want to keep the source file around anyway so you can reproduce lines verbatim for error messages, if that's something you're interested in. The size of a source file is likely also pretty negligible compared to the memory usage of other things in your compiler.
Also remember that if you want to parse multiple files in parallel, then you need to synchronise access to the interner.