Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 10, 2023

Conversation

CaseyCarter
Copy link
Member

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
@StephanTLavavej StephanTLavavej self-assigned this Feb 10, 2023
@StephanTLavavej
Copy link
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
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