New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intern namedtuple field names #70169
Comments
If intern field names in namedtuple, this will speed up the access to them, because names could be compared just by identity in dict lookup. This can also make pickles containing namedtuples more compact. |
Doesn't interning happen already as a byproduct of the exec? |
Indeed, keys of __dict__ are interned. But elements of _fields are not. >>> A = namedtuple('A', 'abc123 def456')
>>> sorted(A.__dict__)[-1] == A._fields[-1]
True
>>> sorted(A.__dict__)[-1] is A._fields[-1]
False |
I don't see how the proposed patch would affect the result. ISTM that the interning would have to happen after the template substitution and exec. See the attached alternate patch. That said, I'm not too concerned with the contents of _fields not being interned. It isn't the performance sensitive part of the code. |
Now, after you pointed out that the keys in the dictionary already interned, I'm not sure anymore that it is needed to intern the _fields items. I'm wondering, why short identifier-like string literals in compiled _fields are not interned? |
The benefits are tiny, but if the one line patch looks good, we might as well intern the _fields and save a few bytes. |
Interesting, short string literals usually are interned, but they are not interned in tuple literal. >>> namespace = {}
>>> exec('a = ["abc123"]\ndef abc123(): pass', namespace)
>>> namespace['abc123'].__name__ is namespace['a'][0]
True
>>> exec('a = ("abc123",)\ndef abc123(): pass', namespace)
>>> namespace['abc123'].__name__ is namespace['a'][0]
False
>>> namespace['abc123'].__name__ == namespace['a'][0]
True I think it would be better to change the compiler to always intern short string literals. And patching namedtuple will be not needed. |
Can we close this entry? If you do patch the compiler, a separate tracker item can be opened. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: