-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Description
Feature or enhancement
Proposal:
Interning string literals can speed up dictionary lookup. Ref: sys.intern
sys.intern(string)
Enter string in the table of “interned” strings and return the interned string – which is string itself or a copy. Interning strings is useful to gain a little performance on dictionary lookup – if the keys in a dictionary are interned, and the lookup key is interned, the key comparisons (after hashing) can be done by a pointer compare instead of a string compare. Normally, the names used in Python programs are automatically interned, and the dictionaries used to hold module, class or instance attributes have interned keys.
We already intern the typename and field names for namedtuple.
cpython/Lib/collections/__init__.py
Line 384 in a971574
| typename = _sys.intern(str(typename)) |
cpython/Lib/collections/__init__.py
Line 424 in a971574
| field_names = tuple(map(_sys.intern, field_names)) |
We can make similar improvements for dataclass.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response