Skip to content

Stop quick argsort from also running a stable sort - #2954

Open
gouarin wants to merge 1 commit into
xtensor-stack:masterfrom
gouarin:fix-argsort-quick
Open

gouarin wants to merge 1 commit into
xtensor-stack:masterfrom
gouarin:fix-argsort-quick

Conversation

@gouarin

@gouarin gouarin commented Oct 29, 2026

Copy link
Copy Markdown
Member

Checklist

  • The title and commit message(s) are descriptive.
  • Small commits made to fix your PR have been squashed to avoid history pollution.
  • Tests have been added for new features or bug fixes.
  • API of new functions and classes are documented (no public API changes).

Description

argsort with sorting_method::quick, the default, ran std::sort and then fell through into the stable case, so every call also ran std::stable_sort on the already sorted indices. The result was still valid, but the work was done twice. GCC reports it with -Wimplicit-fallthrough.

Both cases of the switch in detail::argsort_iter now end with break.

xsort.argsort_quick_sorts_once counts the comparisons made by argsort_iter with the quick method and checks they match a single std::sort on the same data: 108 on master against 70.

The quick case in detail::argsort_iter had no break, so it fell through
to the stable case. Every argsort with sorting_method::quick, the
default, ran std::sort and then std::stable_sort on the same indices.
The result was still a valid ordering, but the work roughly doubled.

Add the missing break statements. The new test counts the comparisons
made by the quick path and checks them against a plain std::sort on the
same data. On master it fails with 108 comparisons against 70.
@codspeed

codspeed Bot commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ Unable to measure the execution of 1 benchmark

This benchmark measured no execution time, so its result is not comparable and counts as unchanged. This usually happens when the compiler removes the code under test.

Prevent compiler optimizations

See affected benchmarks

✅ 255 untouched benchmarks


Comparing gouarin:fix-argsort-quick (f5dc73f) with master (57f3d07)

Open in CodSpeed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant