Repository navigation
Optimize builtin functions min() and max() #90350
Description
Activity
colorfulappl commented
on Dec 29, 2021 colorfulapplmannequinMannequinAuthorMore actions- added3.11only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagePerformance or resource usage
on Dec 29, 2021 Repeating my comment on #74472: If we are touching
min()andmax(), it would make sense, IMO, to port them to Argument Clinic. FTR, Argument Clinic got*argssupport in #62809 / bpo-20291.@colorfulappl made a "competing" branch using AC on his local fork1. However, that branch contained a bug with the key function; I made an amended version2 for benchmarking. Here's some micro-benchmarks from optimised builds on macOS 12.1 w/Clang 13:
+---------------------------------------------------------+---------+-----------------------+----------------------+ | Benchmark | main | python/cpython#74472 | python/cpython#74472-ac | +=========================================================+=========+=======================+======================+ | max(a, b) | 193 ns | 74.1 ns: 2.60x faster | 179 ns: 1.08x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+ | max(a, b, c, d, e) | 273 ns | 126 ns: 2.17x faster | 260 ns: 1.05x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+ | max([a, b]) | 267 ns | 185 ns: 1.44x faster | 239 ns: 1.12x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+ | max([a, b, c, d, e]) | 345 ns | 259 ns: 1.33x faster | 312 ns: 1.10x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+ | max((a,), (b,), key=lambda x: x[0]) | 707 ns | 444 ns: 1.59x faster | 513 ns: 1.38x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+ | max((a,), (b,), (c,), (d,), (e,), key=lambda x: x[0]) | 1.12 us | 831 ns: 1.35x faster | 930 ns: 1.20x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+ | max([(a,), (b,)], key=lambda x: x[0]) | 786 ns | 561 ns: 1.40x faster | 570 ns: 1.38x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+ | max([(a,), (b,), (c,), (d,), (e,)], key=lambda x: x[0]) | 1.19 us | 981 ns: 1.22x faster | 981 ns: 1.22x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+ | max([], default=-1) | 484 ns | 177 ns: 2.73x faster | 188 ns: 2.57x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+ | Geometric mean | (ref) | 1.68x faster | 1.29x faster | +---------------------------------------------------------+---------+-----------------------+----------------------+Footnotes
Kind of like what's mentioned at faster-cpython/ideas#131 , it may be interesting to explore implementing min()/max() in Pure python, considering recent specializations to COMPARE_OP, and potential future specializations of FOR_ITER.
- added3.12only security fixesonly security fixesand removed3.11only security fixesonly security fixes
on Sep 7, 2022 @erlend-aasland Would you mind to publish https://github.com/erlend-aasland/cpython/tree/gh-30286-ac you mentioned as a PR, please? It will be a great opportunity to close gh-30286 and resolve this issue for good.
Thank you for your contribution @colorfulappl.
Reacted by Erlend E. Aasland- added3.13only security fixesonly security fixesand removed3.12only security fixesonly security fixes
on Dec 11, 2023 - added a commit that references this issue
on Jan 16, 2024
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: