Skip to content

Refactor ranges::minmax and ranges::minmax_element#3384

Merged
StephanTLavavej merged 1 commit into
microsoft:mainfrom
CaseyCarter:minmax
Feb 10, 2023
Merged

Refactor ranges::minmax and ranges::minmax_element#3384
StephanTLavavej merged 1 commit into
microsoft:mainfrom
CaseyCarter:minmax

Conversation

@CaseyCarter
Copy link
Copy Markdown
Contributor

The fix for #2900 in #3366 did not take advantage of the fact that the minimum and maximum elements of a range are always distinct except when the range has only one element. It couldn't easily do so due to the way ranges::minmax and ranges::minmax_element share the common backend ranges::_Minmax_element_unchecked.

This PR introduces a new backend for ranges::minmax (ranges::_Minmax_fn::_Minmax_fwd_unchecked) and makes ranges::_Minmax_element_unchecked the private member ranges::_Minmax_element_fn::_Minmax_element_fwd_unchecked. Since the two are distinct, _Minmax_fwd_unchecked can deal in values instead of iterators, and we can unroll the first loop iteration to detect the single-element case naturally. Since no additional branch is needed, we can enable the fix for #2900 unconditionally.

Drive-by: Fully qualify the names of ugly functions called by ranges::minmax and ranges::minmax_element.

The fix for microsoft#2900 in microsoft#3366 did not take advantage of the fact that the minimum and maximum elements of a range are always distinct except when the range has only one element. It couldn't easily do so due to the way `ranges::minmax` and `ranges::minmax_element` share the common backend `ranges::_Minmax_element_unchecked`.

This PR introduces a new backend for `ranges::minmax` (`ranges::_Minmax_fn::_Minmax_fwd_unchecked`) and makes `ranges::_Minmax_element_unchecked` the private member `ranges::_Minmax_element_fn::_Minmax_element_fwd_unchecked`. Since the two are distinct, `_Minmax_fwd_unchecked` can deal in values instead of iterators, and we can unroll the first loop iteration to detect the single-element case naturally. Since no additional branch is needed, we can enable the fix for microsoft#2900 unconditionally.

Drive-by: Fully qualify the names of ugly functions called by `ranges::minmax` and `ranges::minmax_element`.
@CaseyCarter CaseyCarter added the enhancement Something can be improved label Feb 3, 2023
@CaseyCarter CaseyCarter requested a review from a team as a code owner February 3, 2023 05:13
Comment thread stl/inc/algorithm
Comment thread stl/inc/algorithm
@StephanTLavavej StephanTLavavej self-assigned this Feb 10, 2023
@StephanTLavavej
Copy link
Copy Markdown
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit d1db89a into microsoft:main Feb 10, 2023
@StephanTLavavej
Copy link
Copy Markdown
Member

Thanks for overhauling this code to inherently handle the unusual corner case! 😸 📈 🎉

@CaseyCarter CaseyCarter deleted the minmax branch February 11, 2023 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Something can be improved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants