[PATCH] libstdc++: Place pivot in final position in __introsort_loop [PR57916]

Efe Deniz Bağlar efedenizbaglar45@gmail.com
Sat Dec 5 20:11:02 GMT 2026


__unguarded_partition_pivot leaves the pivot at *__first and partitions
[__first + 1, __last), so __introsort_loop previously kept the pivot in
the left subrange [__first, __cut), where it was partitioned again on
later iterations.  Following the idea proposed by Alexey Tourbin in
PR 57916 and described by Marc Glisse in PR 58437 comment 14, swap
*__first with *(__cut - 1) to place the pivot in its final sorted
position and exclude it from both partitions by narrowing the left
subrange to [__first, __cut - 1).  This reduces the average comparison
count's n ln n coefficient from 30/17 to 12/7 (about 5% fewer
comparisons measured at n = 10^6).  Apply this to both
std::__introsort_loop and ranges::__detail::__introsort_loop, leaving
__unguarded_partition_pivot unchanged so its contract stays intact for
__introselect and when linking objects built with mixed header versions.

	PR libstdc++/57916

libstdc++-v3/ChangeLog:

	* include/bits/ranges_algo.h (__detail::__introsort_loop): Swap
	pivot into its final position at __cut - 1 and exclude it from
	both partitions.
	* include/bits/stl_algo.h (__introsort_loop): Likewise.

Signed-off-by: Efe Deniz Bağlar <efedenizbaglar45@gmail.com>
---
Measurements
Random inputs (comparisons/n, original / patched, ratio = orig/patched):
  n = 10^4 (40,000 inputs): 15.9798(18) / 15.1014(17) / 1.0582(1)
  n = 10^5  (6,000 inputs): 20.0447(46) / 19.0530(44) / 1.0521(2)
  n = 10^6 (50,000 inputs): 24.1071(16) / 22.9932(15) / 1.0484(1)
  n = 10^7 (20,000 inputs): 28.1668(26) / 26.9392(24) / 1.0456(1)
At n = 10^6: 0.19 extra moves/key for 1.112 fewer comparisons/key.
Ascending inputs use 33% fewer comparisons; equal keys save 1.2-3.8%.
Time (g++ 13.3, -O2, n <= 10^6; std::string n = 10^4 not measured):
faster in 21 of 24 configurations (2.5-5.2% on random inputs), slower by
1-8% on descending uint64 across all three sizes.  Timings were measured
with an earlier form of the patch doing the same comparisons and swaps
(with an extra never-taken branch); the comparison counts above were
reproduced identically with this patch.

Credit and history
Alexey Tourbin proposed this in PR 57916 (2013-07-17; the patch was
never applied), and Marc Glisse described the same swap in PR 58437
comment 14 (2013-09-19); r203035-r203037 changed only the sample
positions.

In PR 57916 comment 5, Chris Jefferson warned that changing shared
helper behaviour could break mixed-version linking.  Doing the swap
locally in __introsort_loop leaves __unguarded_partition_pivot's
contract unchanged, and old and new __introsort_loop instantiations are
interchangeable, so sorting remains correct whichever the linker picks.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57916
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58437#c14

Testing
Tested on x86_64-pc-linux-gnu; no new failures:
target: x86_64-pc-linux-gnu, master 4f76e6b8a241, configure --disable-bootstrap --enable-languages=c,c++ --disable-multilib
$ make -j12 check-target-libstdc++-v3
  before: 20141 expected passes, 0 unexpected failures, 832 unsupported
  after:  20141 expected passes, 0 unexpected failures, 832 unsupported
$ GLIBCXX_TESTSUITE_STDS=98,11,17,20 make -j12 check-target-libstdc++-v3 RUNTESTFLAGS="conformance.exp=25_algorithms/* --target_board=unix\{,-D_GLIBCXX_DEBUG\}"
  before: 6401 expected passes, 0 unexpected failures, 2444 unsupported
  after:  6401 expected passes, 0 unexpected failures, 2444 unsupported
$ contrib/compare_tests before after: no new failures

Paper
Full derivation, equal-keys data, and timing details:
https://doi.org/10.5281/zenodo.24848056
https://github.com/kanemoda/pivot-that-stays-behind

Stage
GCC 17 is in Stage 3; since PR 57916 is an enhancement, this can wait
for GCC 18 Stage 1 unless maintainers want it sooner.

Tools used
AI tools were used for the analysis, experiments, and measurement and
testing infrastructure.

 libstdc++-v3/include/bits/ranges_algo.h | 6 +++++-
 libstdc++-v3/include/bits/stl_algo.h    | 7 ++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index f4c70dc70..8607994ff 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -2495,8 +2495,12 @@ namespace ranges
 	      }
 	    --__depth_limit;
 	    _Iter __cut = __detail::__unguarded_partition_pivot(__first, __last, __comp);
+	    // Place the pivot in its final position at __cut - 1.  Exclude it
+	    // from both partitions.
+	    _Iter __pivot = ranges::prev(__cut);
+	    ranges::iter_swap(__first, __pivot);
 	    __detail::__introsort_loop(__cut, __last, __depth_limit, __comp);
-	    __last = __cut;
+	    __last = __pivot;
 	  }
       }
   } // namespace __detail
diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h
index f7c4053ea..b39b02196 100644
--- a/libstdc++-v3/include/bits/stl_algo.h
+++ b/libstdc++-v3/include/bits/stl_algo.h
@@ -1946,8 +1946,13 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
 	  --__depth_limit;
 	  _RandomAccessIterator __cut =
 	    std::__unguarded_partition_pivot(__first, __last, __comp);
+	  // Place the pivot in its final position at __cut - 1.  Exclude it
+	  // from both partitions.
+	  _RandomAccessIterator __pivot = __cut;
+	  --__pivot;
+	  std::iter_swap(__first, __pivot);
 	  std::__introsort_loop(__cut, __last, __depth_limit, __comp);
-	  __last = __cut;
+	  __last = __pivot;
 	}
     }
 
-- 
2.43.0


More information about the Libstdc++ mailing list