Repository navigation
Closed
Labels
3.11only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedperformancePerformance or resource usage
Description
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 = None
closed_at = None
created_at = <Date 2021-08-27.03:03:33.269>
labels = ['interpreter-core', '3.11', 'performance']
title = 'More compact range iterator'
updated_at = <Date 2021-09-27.14:46:46.491>
user = 'https://github.com/serhiy-storchaka'bugs.python.org fields:
activity = <Date 2021-09-27.14:46:46.491>
actor = 'lukasz.langa'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Interpreter Core']
creation = <Date 2021-08-27.03:03:33.269>
creator = 'serhiy.storchaka'
dependencies = []
files = ['50306']
hgrepos = []
issue_num = 45026
keywords = ['patch']
message_count = 22.0
messages = ['400390', '400392', '400396', '400399', '400528', '401085', '401411', '401419', '402139', '402335', '402347', '402349', '402452', '402477', '402534', '402543', '402555', '402639', '402649', '402708', '402712', '402724']
nosy_count = 5.0
nosy_names = ['gvanrossum', 'rhettinger', 'lukasz.langa', 'serhiy.storchaka', 'Dennis Sweeney']
pr_nums = ['27986', '28176']
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'resource usage'
url = 'https://bugs.python.org/issue45026'
versions = ['Python 3.11']Metadata
Metadata
Assignees
Labels
3.11only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)pendingThe issue will be closed if no feedback is providedperformancePerformance or resource usage
Activity
ambv commentedon Sep 21, 2021
Looks like #72363 is faster across the board. One missing benchmark here is comparing
len()speed as this was where the results will be reversed. It would be interesting to see to what extent.serhiy-storchaka commentedon Sep 21, 2021
length_hint(), not len(). Its cost is included in microbenchmark for list(), where it is followed by iterating 2000 items.
Calling operator.length_hint() in Python:
Indirect call from C (it includes overhead for calling list() and iter() in Python):
With few experiments I have found that PR 28176 is faster than PR 27986 for list(iter(range(...))) if a range is larger than 40-100 items.
gvanrossum commentedon Sep 21, 2021
May the best PR win. :-)
ambv commentedon Sep 22, 2021
Since len timings for ranges of 100 items are negligible anyway, I personally still favor #72363 which is clearly faster during iteration.
sweeneyde commentedon Sep 23, 2021
I benchmarked #72173 and #72363 on "for i in range(10000): pass" and found that #72173 was faster for this (likely most common) case of relatively small integers.
Mean +- std dev: [main] 204 us +- 5 us -> [GH-27986] 194 us +- 4 us: 1.05x faster
Mean +- std dev: [main] 204 us +- 5 us -> [GH-28176] 223 us +- 6 us: 1.09x slower
It's possible to have different implementations for small/large integers, but IMO it's probably best to keep consistency and go with #72173.
ambv commentedon Sep 23, 2021
Good point benchmarking small iterations too, Dennis. I missed that.
Agreed then, #72173 looks like a winner.
serhiy-storchaka commentedon Sep 24, 2021
I do not see any difference in iterating small integers:
ambv commentedon Sep 24, 2021
Serhiy, right: looks like the difference stems from recreating the range objects, not from iteration. But Dennis' observation still stands: using `for i in range(...):` is a very popular idiom. If that becomes slower than the status quo, we will be making existing Python programs slower as well.
serhiy-storchaka commentedon Sep 25, 2021
There is nothing in code that could explain a measureable difference in creating the range objects or the range object iterators. And indeed, it is in the range of the standard deviation, so it is non-existent.
sweeneyde commentedon Sep 25, 2021
I did more benchmarks on my Windows laptop, and it seems the difference goes away after using PGO.
The benchmarking program:
#################################
###################################
The results (without PGO):
PS C:\Users\sween\Source\Repos\cpython2\cpython> .\python.bat -m pyperf compare_to .\20e3149c175a24466c7d1c352f8ff2c11effc489.json .\cffa90a8b0057d7e7456571045f2fb7b9ceb426f.json -G
Running Release|x64 interpreter...
Slower (15):
Faster (2):
Benchmark hidden because not significant (3): for i in it_100: pass, for i in it_10000: pass, for i in it_100000: pass
Geometric mean: 1.03x slower
###################################
The results (with PGO):
PS C:\Users\sween\Source\Repos\cpython2\cpython> .\python.bat -m pyperf compare_to .\PGO-20e3149c175a24466c7d1c352f8ff2c11effc489.json .\PGO-cffa90a8b0057d7e7456571045f2fb7b9ceb426f.json -G
Running PGUpdate|x64 interpreter...
Slower (7):
Faster (7):
Benchmark hidden because not significant (6): deque(it_10), list(iter(range(10))), for i in range(100): pass, deque(it_100), deque(it_1000), deque(it_10000)
Geometric mean: 1.01x faster
ambv commentedon Sep 27, 2021
Dennis, run your benchmarks with --rigorous to avoid "Benchmark hidden because not significant".
I note that the second and third benchmarks aren't useful as written because the iterators are exhausted after first repetition. I could see this in my results, note how the values don't rise with the iterator size:
deque(it_10): Mean +- std dev: 334 ns +- 8 ns
deque(it_100): Mean +- std dev: 338 ns +- 9 ns
deque(it_1000): Mean +- std dev: 335 ns +- 9 ns
deque(it_10000): Mean +- std dev: 336 ns +- 10 ns
deque(it_100000): Mean +- std dev: 338 ns +- 11 ns
When I modified those to recreate the iterator on every run, the story was much different.
ambv commentedon Sep 27, 2021
Benchmarks for PGO builds on macOS 10.15 Catalina, Intel MBP 2018.
Like in Dennis' case, 20e3149 is #72173 and cffa90a is #72363. The difference is that
it_benchmarks create the iterator on each execution. In this case the explicit iterator versions of the for-loop are indistinguishable from the ones usingrange()directly.################
❯ python -m pyperf compare_to /tmp/20e3149c175a24466c7d1c352f8ff2c11effc489-2.json /tmp/cffa90a8b0057d7e7456571045f2fb7b9ceb426f-2.json -G
Slower (11):
Faster (8):
Benchmark hidden because not significant (1): for i in range(10): pass
Geometric mean: 1.00x slower
################
The results look like a wash here. Let me compare both to
main.ambv commentedon Sep 27, 2021
Well, this is kind of disappointing on my end. I attach the full result of a three-way comparison between
mainat the time of Serhiy's last merge (3f8b23f) with #72173 (20e3149) and #72363 (cffa90a). The gist is this:Geometric mean
==============
20e3149-2: 1.01x slower
cffa90a-2: 1.01x slower
At least on my Macbook Pro, all PGO builds, looks like the status quo is on average faster than any of the candidate PRs.
iritkatriel commentedon Aug 18, 2022
Given the benchmark results, are we abandoning this?
serhiy-storchaka commentedon Nov 27, 2022
The difference of 1% is not significant. You can get larger difference from run to run with the same binary. But for large integers the difference is ~2x. And the difference in the memory and the pickle sizes is not insignificant.
The original code was changed, in particular there is a special path in the eval loop for range iteration, so all benchmarks should be rerun.
It sounds funny, but the small difference in iterating small ranges in #27986 disappeared after applying a simple manual optimization:
I thought that the compiler is smart enough for this.
serhiy-storchaka commentedon Nov 29, 2022
#27986 is now the same as the baseline in iterating small integers range. #28176 is slightly slower.
But it is the fastest in iterating large integers range.
gvanrossum commentedon Nov 30, 2022
I would vote for the faster PR when the values are small -- perf for large ranges is not a priority (these occur too rare and everything else is slower with them as well).
serhiy-storchaka commentedon Nov 30, 2022
I agree. Thank you for review.
gh-89189: More compact range iterator (GH-27986)
Merge branch 'main' into fixinspect