Reputation: 10903
I've found countless arguments for when you should use sys.intern()
to explicitly internalize your strings. I've found it much harder to find good arguments for when not to intern them. Key to my confusion is that the documentation states "Interned strings are not immortal; you must keep a reference to the return value of intern() around to benefit from it." This removes one of the typical issues for interned strings, which is that they consume memory long after the usage of the string is done. This leaves very few remaining possible disadvantages. Few enough that I would expect them to have been enumerated.
The only disadvantage I have found to intern that seems to be self-consistent is that intern must interact with a globally shared resource, and thus may even have to do multithread synchronization. But is that the only disadvantage? Is the need to globally synchronize during the intern()
call the only disadvantage?
Upvotes: 1
Views: 376
Reputation: 281683
Interning isn't free. Even ignoring the global synchronization (that's just handled by the GIL), you're spending time on interning, and at most possible call sites, you gain absolutely nothing for that time.
Interning only pays off in very specific cases. You should only do it if you've profiled your code with and without interning and you know, or are very confident, that it actually pays off in your use case.
Upvotes: 2