Title: C Fast path for namedtuple's property/itemgetter pair
Type: Stage: patch review
Components: Extension Modules Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ned.deily, rhettinger, serhiy.storchaka
Priority: Keywords: patch

Created on 2018-01-04 18:05 by rhettinger, last changed 2018-05-16 15:43 by ned.deily.

Pull Requests
URL Status Linked Edit
PR 5100 open rhettinger, 2018-01-04 18:07
Messages (7)
msg309477 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-01-04 18:05
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.
msg309478 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-01-04 18:39
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.
msg309481 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-01-04 20:28
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.
msg309486 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-01-04 22:31
Ah, right. property already is hardy optimized, and specialized version of itemgetter gives only around 35% of maximal speed up.
msg311168 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-01-29 20:42
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__.
msg311171 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-01-29 21:02
OK, it would be nice to get this in and the risk seems relatively low.  Good luck!
msg316822 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2018-05-16 15:43
Deferred to 3.8
Date User Action Args
2018-05-16 15:43:16ned.deilysetpriority: deferred blocker ->

messages: + msg316822
versions: - Python 3.7
2018-01-29 21:02:17ned.deilysetpriority: normal -> deferred blocker

messages: + msg311171
versions: + Python 3.8
2018-01-29 20:42:29rhettingersetnosy: + ned.deily
messages: + msg311168
2018-01-04 22:31:04serhiy.storchakasetmessages: + msg309486
2018-01-04 20:28:55rhettingersetmessages: + msg309481
2018-01-04 18:39:47serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg309478
2018-01-04 18:07:23rhettingersetkeywords: + patch
stage: patch review
pull_requests: + pull_request4968
2018-01-04 18:05:43rhettingercreate