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
Some optimizations based on LWG-4490's new wording lenience.
Scope: tackle niche ranges that:
[-] are not common_ranges
[-] its end sentinel does not model sized_sentinel_for for the iterator type (sometimes required implicitly by exclusion)
[+] are themselves sized
[+] are random access
Steps:
compute offsetted iterator based on the size of the range in O(1) time
pass the new iterator pair further down the pipeline, making some ranges now eligible for existing optimized iterator-pair paths
profit
Identified optimization opportunities in following algorithms:
ranges::copy
ranges::move
ranges::count
ranges::remove_copy
ranges::fill
ranges::replace
ranges::replace_copy
ranges::remove
ranges::unique
ranges::unique_copy
ranges::inmax_element (+ extra test coverage)
ranges::minmax (+ extra test coverage)
ranges::is_sorted_until
ranges::swap_ranges (+ extra test coverage)
ranges::uninitialized_copy (+ extra test coverage)
ranges::uninitialized_move (+ extra test coverage)
ranges::uninitialized_fill (+ extra test coverage)
ranges::uninitialized_value_construct
ranges::uninitialized_default_construct
ranges::destory (kinda, still possible to find the end iterator faster)
Actually implemented in this PR:
ranges::copy
ranges::move
Benchmark results for std::ranges::copy (median CPU time per call across five repetitions). It shows what I think is the expected cost distribution of memmove:
Input length
Old
New
Change
0 B
2.30 ns
4.19 ns
82% slower (+1.89 ns)
1 B
2.44 ns
3.84 ns
57% slower (+1.40 ns)
8 B
3.84 ns
4.36 ns
14% slower (+0.52 ns)
64 B
15.17 ns
4.36 ns
3.48x speedup
512 B
105.79 ns
5.23 ns
20.23x speedup
4 KiB
784.74 ns
25.69 ns
30.55x speedup
32 KiB
6,510.42 ns
395.28 ns
16.47x speedup
64 KiB
13,020.83 ns
784.74 ns
16.59x speedup
Benchmark code included in PR for now, can remove or add more if requested.
AI used for identifying algorithms that could benefit from this optimization, overall code quality checks and benchmark design. Impacted tests were ran locally and are passing.
This PR is large (300+ lines) and touching our most template-complicated algorithms. I would prefer first-time contributors to start with smaller changes. (I have not yet had a chance to even review your <print>: Optimize no-argument print() brace handling #6465; making progress on the backlog is hard when people don't ask for advice about what changes would be helpful and what form they should take.) I apologize in advance for the bluntness but I am especially wary when new contributors are using AI to make large-scale changes, as they inherently lack the familiarity to judge whether changes are good or bad. I don't object to this PR's intent, but I want to see one algorithm updated, not all of them.
Your table is calculating speedups incorrectly. Please read https://randomascii.wordpress.com/2018/02/04/what-we-talk-about-when-we-talk-about-performance/ which explains that the proper method is OldTime/NewTime expressed as a multiplicative factor; e.g. if something previously took 22 seconds and now takes 7 seconds, that's a 22/7 = 3.14x speedup. Describing that as "214% faster" is technically correct but extremely confusing (calculation: (OldTime/NewTime - 1) * 100 expressed as percent; describing a 1.23x speedup as "23% faster" is defensible but for speedups nearing 2x it is confusing and beyond 2x extremely confusing). Describing it as "68.2% faster" is simply wrong (bogus calculation: (1 - NewTime/OldTime) * 100 expressed as percent). Going from 13,020.83 ns to 784.74 ns is not "94.0% faster". That's an (impressive) 16.6x speedup.
This PR is large (300+ lines) and touching our most template-complicated algorithms. I would prefer first-time contributors to start with smaller changes. (I have not yet had a chance to even review your <print>: Optimize no-argument print() brace handling #6465; making progress on the backlog is hard when people don't ask for advice about what changes would be helpful and what form they should take.) I apologize in advance for the bluntness but I am especially wary when new contributors are using AI to make large-scale changes, as they inherently lack the familiarity to judge whether changes are good or bad. I don't object to this PR's intent, but I want to see one algorithm updated, not all of them.
Your table is calculating speedups incorrectly. Please read https://randomascii.wordpress.com/2018/02/04/what-we-talk-about-when-we-talk-about-performance/ which explains that the proper method is OldTime/NewTime expressed as a multiplicative factor; e.g. if something previously took 22 seconds and now takes 7 seconds, that's a 22/7 = 3.14x speedup. Describing that as "214% faster" is technically correct but extremely confusing (calculation: (OldTime/NewTime - 1) * 100 expressed as percent; describing a 1.23x speedup as "23% faster" is defensible but for speedups nearing 2x it is confusing and beyond 2x extremely confusing). Describing it as "68.2% faster" is simply wrong (bogus calculation: (1 - NewTime/OldTime) * 100 expressed as percent). Going from 13,020.83 ns to 784.74 ns is not "94.0% faster". That's an (impressive) 16.6x speedup.
No, no, that's perfectly valid. I was just particularly free these days and I wanted to do some contributions on parts of the STL that I'm more familiar with. I don't blindly open PRs, these two iessues were something that I wanted to work on for a long time now, and I don't plan on opening others any time soon.
Is it fine if I reduce the scope of the PR to only copy/move for now?
The reason will be displayed to describe this comment to others. Learn more.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Yes, I was thinking the same too. I had an alternative desing ready using a helper that returns an unwrapped begin and its best end (which may or may not remain the sentinel type). It would look something like this:
I initially dropped it while adopting even more algorithms from the list because I wanted to assert the constraints of each algorithm individually and I felt that the helper was hindering visiblity. I will audit all of them and see if a generic helper can still cover all of them.
Edit: I think "_Get_final_range_enpoints_unwrapped" would be a descriptive name for the helper
Add end-to-end move test for unsized sentinel ranges
stl/inc/algorithm:2123
The new ranges::move path is not exercised end-to-end. The added tests invoke only the internal endpoint helper, and the move test matrix's random-access ranges use sized sentinels, so this branch is skipped. Add a range-overload test with a sized random-access range and unsized sentinel, checking moved values and the returned/rewrapped iterators.
Add end-to-end copy test for unsized sentinel ranges
stl/inc/xutility:5357
The new ranges::copy path is not exercised end-to-end. The added machinery tests call the helper directly, while the copy test matrix's random-access ranges use sized sentinels, so this !sized_sentinel_for branch is skipped. Add a range-overload test using a sized random-access range with an unsized sentinel and verify copied values plus both returned iterators; a wrapped iterator would also cover rewrapping.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
An ambiguous ranges::next call introduces a compilation regression in ranges::copy and ranges::move for valid sized ranges whose iterator difference type also models a sentinel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some optimizations based on LWG-4490's new wording lenience.
Scope: tackle niche ranges that:
[-] are not common_ranges
[-] its end sentinel does not model sized_sentinel_for for the iterator type (sometimes required implicitly by exclusion)
[+] are themselves sized
[+] are random access
Steps:
Identified optimization opportunities in following algorithms:
Actually implemented in this PR:
Benchmark results for std::ranges::copy (median CPU time per call across five repetitions). It shows what I think is the expected cost distribution of memmove:
Benchmark code included in PR for now, can remove or add more if requested.
AI used for identifying algorithms that could benefit from this optimization, overall code quality checks and benchmark design. Impacted tests were ran locally and are passing.