You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
C Fast path for namedtuple's property/itemgetter pair #76673
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:
assignee=Noneclosed_at=<Date2018-12-31.12:15:44.489>created_at=<Date2018-01-04.18:05:43.360>labels= ['extension-modules', '3.8']
title="C Fast path for namedtuple's property/itemgetter pair"updated_at=<Date2019-02-16.20:30:47.658>user='https://github.com/rhettinger'
Attribute access for named tuples can be made as fast as attribute access of instances of classes with slots. All that is needed is a C subclass of property with it own __get__ that directly look's up the tuple field using PyTuple_GET_ITEM using an index stored in a C struct rather than as an integer object.
Alternatively we could consider the way of optimizing property or itemgetter (or both). This may be more complex solution, but if the added complexity is not too large, this solution may be more preferable because it will speed up not only namedtuple.
Joe Jevnik at already worked through attempts optimize property/itemgetter. There isn't much fruit left there.
What I've outlined here should be several times faster. The code will end-up being very similar to the __get__ for member objects which is currently the fastest form of attribute access.
Ned, I don't have time to finish this before 3.7b1. Will you approve for inclusion in 3.7b2? This isn't a user visible feature. It's goal is to bring named tuple attribute lookup performance up to parity with regular classes using __slots__.
New changeset 3f5fc70 by Raymond Hettinger (Pablo Galindo) in branch 'master': bpo-32492: 1.6x speed up in namedtuple attribute access using C fast-path (bpo-10495) 3f5fc70
Removed the docstrings cache. I have not found significant difference, but this make the code simpler.
Improved tests. Merged test_doc_writable and test_namedtuple_can_mutate_doc_of_descriptors_independently in the single test, added tests for immutability, hashing, field descriptors and help(), and rewriting old checks that used eval().
Unified names of tp_descr_get and tp_descr_set functions. This will help to search implementations of custom descriptors.
New changeset 63fa1cf by Miss Islington (bot) (Raymond Hettinger) in branch 'master': bpo-32492: Add missing whatsnew entries for itemgetter and namedtuple (GH-11897) 63fa1cf
Activity
rhettinger commentedon Jan 4, 2018
Attribute access for named tuples can be made as fast as attribute access of instances of classes with slots. All that is needed is a C subclass of property with it own __get__ that directly look's up the tuple field using PyTuple_GET_ITEM using an index stored in a C struct rather than as an integer object.
serhiy-storchaka commentedon Jan 4, 2018
Alternatively we could consider the way of optimizing property or itemgetter (or both). This may be more complex solution, but if the added complexity is not too large, this solution may be more preferable because it will speed up not only namedtuple.
rhettinger commentedon Jan 4, 2018
Joe Jevnik at already worked through attempts optimize property/itemgetter. There isn't much fruit left there.
What I've outlined here should be several times faster. The code will end-up being very similar to the __get__ for member objects which is currently the fastest form of attribute access.
serhiy-storchaka commentedon Jan 4, 2018
Ah, right. property already is hardy optimized, and specialized version of itemgetter gives only around 35% of maximal speed up.
rhettinger commentedon Jan 29, 2018
Ned, I don't have time to finish this before 3.7b1. Will you approve for inclusion in 3.7b2? This isn't a user visible feature. It's goal is to bring named tuple attribute lookup performance up to parity with regular classes using __slots__.
ned-deily commentedon Jan 29, 2018
OK, it would be nice to get this in and the risk seems relatively low. Good luck!
ned-deily commentedon May 16, 2018
Deferred to 3.8
rhettinger commentedon Dec 30, 2018
New changeset 3f5fc70 by Raymond Hettinger (Pablo Galindo) in branch 'master':
bpo-32492: 1.6x speed up in namedtuple attribute access using C fast-path (bpo-10495)
3f5fc70
serhiy-storchaka commentedon Dec 30, 2018
There are few post-commit tweaks in PR 11367:
serhiy-storchaka commentedon Dec 30, 2018
See also bpo-35619.
serhiy-storchaka commentedon Dec 31, 2018
New changeset 052b2df by Serhiy Storchaka in branch 'master':
bpo-32492: Tweak _collections._tuplegetter. (GH-11367)
052b2df
miss-islington commentedon Feb 16, 2019
New changeset 63fa1cf by Miss Islington (bot) (Raymond Hettinger) in branch 'master':
bpo-32492: Add missing whatsnew entries for itemgetter and namedtuple (GH-11897)
63fa1cf