This is an archive of the discontinued LLVM Phabricator instance.

[libc++] Add an early return for __partial_sort of an empty range.
ClosedPublic

Authored by Quuxplusone on Dec 26 2021, 8:05 PM.

Details

Summary

If __first == __middle, then partial_sort is a no-op; don't bother to iterate all the way from __middle to __end.

Fixes https://github.com/llvm/llvm-project/issues/49431

Diff Detail

Event Timeline

Quuxplusone requested review of this revision.Dec 26 2021, 8:05 PM
Quuxplusone created this revision.
Herald added 1 blocking reviewer(s): Restricted Project. · View Herald TranscriptDec 26 2021, 8:05 PM
philnik added inline comments.
libcxx/include/__algorithm/partial_sort.h
33–34

Nit, but this patch seems small enough to change that.

Quuxplusone edited the summary of this revision. (Show Details)Dec 28 2021, 3:09 PM
jloser added a subscriber: jloser.Dec 28 2021, 7:12 PM

Any appetite for adding a test that counts the numbers of comparisons to bind this behavior? Otherwise, LGTM!

ldionne accepted this revision.Jan 3 2022, 6:38 AM

LGTM, but I agree it would be good to add a test to avoid regressing that if we change our partial_sort implementation (which we might very well do at some point, since there is some work happening on std::sort).

This revision is now accepted and ready to land.Jan 3 2022, 6:38 AM

BTW, that was not binding -- as discussed offline, I think it is reasonable to try to tackle complexity tests separately from this patch, and more generally for all the algorithms.

This revision was landed with ongoing or failed builds.Jan 4 2022, 1:15 PM
This revision was automatically updated to reflect the committed changes.