From e064b9f79de024fc8646323caa5618fbad830f50 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 2 May 2026 11:56:14 -0600 Subject: [PATCH] [algs] Defer insertion sort to single final pass; bump threshold to 32 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Restructure hb_qsort_inline so the recursive partitioning loop stops at subranges of size <= threshold without sorting them, and the entry point runs one insertion sort over the entire array at the end. After the partitioning loop, every element is within the threshold of its sorted position, so the final pass is O(n * threshold) — cache-friendlier than a per-leaf insertion sort, which is what libstdc++'s std::sort does too. Also bump the threshold from 16 to 32, which the deferred scheme prefers (per-leaf insertion sort prefers 16; the deferred form amortizes a larger threshold better). Bench (NotoSansCJK GPU encode, smaller is better): std::sort baseline: 570 ms per-leaf insertion sort, threshold 16 (parent): 613 ms per-leaf insertion sort, threshold 32: 623 ms deferred insertion sort, threshold 16: 620 ms deferred insertion sort, threshold 32 (this): 610 ms deferred insertion sort, threshold 64: 610 ms deferred insertion sort, threshold 96: 620 ms Tested: 240/240 tests pass; nm -D libharfbuzz.so shows zero remaining _ZSt sort weak symbols. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/hb-algs.hh | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 5dfbd5321..1b92ad6f6 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -1305,17 +1305,27 @@ hb_bsearch (const K& key, V* base, } -/* Quicksort with median-of-three pivot, two-way Hoare partition, - * tail-call elimination on the larger side, and insertion-sort - * fall-through for small ranges. Comparator follows C qsort - * convention (negative / zero / positive int). +/* Quicksort partitioning loop: median-of-three pivot, two-way + * Hoare partition, tail-call elimination on the larger side. + * Stops partitioning when subranges shrink below the threshold; + * a single insertion-sort pass over the whole array (run by the + * caller) finishes the job. Same structure libstdc++ uses for + * std::sort. + * + * Not stable; equivalent values may be swapped. */ +/* Quicksort partitioning loop: median-of-three pivot, two-way + * Hoare partition, tail-call elimination on the larger side. + * Stops partitioning when subranges shrink below the threshold; + * a single insertion-sort pass over the whole array (run by + * hb_qsort_inline below) finishes the job. Same shape libstdc++ + * uses for std::sort. * * Not stable; equivalent values may be swapped. */ template static inline void -hb_qsort_inline (T *base, size_t nel, Compar compar) +hb_qsort_loop (T *base, size_t nel, Compar compar) { - while (nel > 16) + while (nel > 32) { T *last = base + nel - 1; T *mid = base + nel / 2; @@ -1330,8 +1340,9 @@ hb_qsort_inline (T *base, size_t nel, Compar compar) hb_swap (*mid, *(last - 1)); T &pivot = *(last - 1); - /* Two-way Hoare partition. base and last are sentinels - * (already <= and >= pivot respectively). */ + /* Two-way Hoare partition. Inner loops are unguarded: + * median-of-three left *base <= pivot and *last >= pivot, + * which act as sentinels. */ T *i = base, *j = last - 1; while (true) { @@ -1348,18 +1359,27 @@ hb_qsort_inline (T *base, size_t nel, Compar compar) size_t right = nel - left - 1; if (left < right) { - hb_qsort_inline (base, left, compar); + hb_qsort_loop (base, left, compar); base = i + 1; nel = right; } else { - hb_qsort_inline (i + 1, right, compar); + hb_qsort_loop (i + 1, right, compar); nel = left; } } +} - /* Insertion sort for small inputs. */ +template +static inline void +hb_qsort_inline (T *base, size_t nel, Compar compar) +{ + hb_qsort_loop (base, nel, compar); + + /* Single final insertion sort over the whole array. After + * the partitioning loop, every element is within the threshold + * of its sorted position, so this pass is O(n * threshold). */ T *end = base + nel; for (T *pi = base + 1; pi < end; pi++) for (T *pj = pi; pj > base && compar (pj[-1], pj[0]) > 0; pj--)