Title: Use PEP 590 vectorcall to speed up calls to filter()
Type: Stage: patch review
Components: Interpreter Core Versions: Python 3.10
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: corona10, rhettinger, vstinner
Priority: normal Keywords: patch

Created on 2021-02-21 17:59 by corona10, last changed 2021-02-22 03:35 by rhettinger.

File name Uploaded Description Edit corona10, 2021-02-21 17:59
Pull Requests
URL Status Linked Edit
PR 24611 open corona10, 2021-02-21 18:02
Messages (5)
msg387469 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-02-21 17:59
| Benchmark    | master | vectorcall           |
| bench filter | 191 ns | 151 ns: 1.26x faster |

Like reversed(, it looks okay to update filter() to use PEP 590.
msg387470 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-02-21 18:40
+0 I don't see any downside.

Note, the benchmark only times instantiation of the filter object.  It doesn't actually run the iterator which is where most of the runtime cost is spent.  So in actual code there is almost zero benefit.  For example, add "list" to the statement:   stmt="b = list(filter(lambda x: x % 2 == 0, a))" and the improvement disappears.
msg387482 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-02-22 00:18
> +0 I don't see any downside.

Thank you Raymond :)
I agree with your view, At first, I thought that instantiation time reducing is also meaningful and we already applied PEP 590 for some of the types to reduce instantiation. (e.g range(), list(), dict(), bool(), reversed(), type()..)

And also I thought that filter is one of the well-used features in Python so it was one of the candidates to apply from my sight. ;)

But if this PR only consume maintenance cost, I am okay with not to apply it doesn't matter :)

And happy new year Raymond(Sorry I am late ;) But the lunar new year was only 2weeks ago so not too late lol)
msg387490 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-02-22 03:12
Happy new year to you as well :-)
msg387493 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-02-22 03:35
FWIW, I don't think there is much of a maintenance burden; hence, the +0.  You've already done the work.
Date User Action Args
2021-02-22 03:35:33rhettingersetmessages: + msg387493
2021-02-22 03:12:13rhettingersetmessages: + msg387490
2021-02-22 00:18:12corona10setmessages: + msg387482
2021-02-21 18:40:56rhettingersetnosy: + rhettinger
messages: + msg387470
2021-02-21 18:02:22corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request23390
2021-02-21 17:59:22corona10create