Rewrite the internal qsort used by hb_array_t::qsort(Compar) as a
typed template hb_qsort_inline<T, Compar>:
* Operates on T* directly, so swap is T-typed (one move per
half-element instead of a per-byte loop). hb_swap is used,
which uses ADL std::swap.
* Two-way Hoare partition instead of three-way Bentley-McIlroy.
The previous three-way scheme paid extra branch overhead in
the inner partition loop without payoff on the typical
HarfBuzz workload (sorting unique curve indices, glyph IDs,
feature tags, etc).
* Median-of-three pivot, parked at last-1.
* Tail-call elimination on the larger partition; recurse only
on the smaller. Bounds stack depth at O(log n).
* Insertion-sort threshold bumped from 10 to 16, matching
libstdc++'s std::sort.
The byte-based sort_r_simple (and helpers SORT_R_SWAP, sort_r_swap,
sort_r_cmpswap, sort_r_swap_blocks) is removed; nothing else used
it. hb_qsort still forwards to system qsort.
Bench (NotoSansCJK GPU encode, smaller is better):
std::sort baseline: 570 ms
byte-based sort_r_simple (step 1): 650 ms (+14%)
three-way typed quicksort: 640 ms
two-way typed quicksort (this): 613 ms (+7.5% vs baseline)
ABI: zero std:: weak symbols leak from libharfbuzz.so, same as
step 1.
Tested: 240/240 tests pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
std::sort instantiates several internal STL helpers
(__introsort_loop, __insertion_sort, __adjust_heap, __heap_select,
__unguarded_linear_insert) as weak symbols in the std:: namespace.
These leaked into libharfbuzz.so for every distinct (T, Compar)
pair, polluting the public ABI surface.
Route hb_array_t::qsort(Compar) through our existing templated
sort_r_simple instead. The comparator is still inlined into the
sort body (sort_r_simple has been comparator-templated since
05762b2fac).
Drop the bool-returning comparator overload in favor of a single
int-sign convention; convert the call sites added during the
std::sort migration back to int-sign comparators.
Step 1 of two: this restores ABI hygiene at the cost of typed-
element swap (sort_r_simple swaps byte-by-byte). A follow-up
will introduce a typed sort to recover that perf.
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) <noreply@anthropic.com>
Restore the historical qsort contract: a C-style int comparator
(negative / zero / positive) is the canonical convention, and
hb_array_t::qsort accepted that for years -- until it was
switched from hb_qsort to std::sort. std::sort wants bool
less-than: a +1 return for a > b looked truthy, broke
strict-weak-ordering, and caused out-of-bounds writes that ASAN
flagged from normalize_stops in hb-gpu-paint.
Wrap the std::sort call with a small overloaded helper
_qsort_lt: bool returns pass through; any other arithmetic
return is treated as the int signed comparator (< 0). Existing
bool-returning callers (hb-cairo-utils, hb-face-builder, _qsort)
keep working; int-returning callers (hb-gpu-paint,
hb-ot-post-table) sort correctly again.
Discovered after extending hb-gpu-fuzzer to also exercise
hb_gpu_paint, which surfaced the latent UB on
noto_handwriting-glyf_colr_1.ttf.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* [subset] Return size_t rather than unsigned from get_size() methods
It's possible some get_size() computations during serialization
may overflow 32-bit arithmetic.
(The result of get_size() is used as size_t when allocating via
hb_serialize_context_t::extend(), but if the return value was
declared as unsigned int, the value may have wrapped resulting
in under-allocation.)
* [subset] Use size_t when accumulating subset_data_size
* [graph] Use size_t (rather than signed int64_t) for vertex_len variables
* Use size_t for sanitize_closure_t::stride to avoid narrowing
hb_array_t::qsort(Compar) now uses std::sort for typed, inlineable
sorting. The no-arg qsort() uses priority dispatch: std::sort for
move-assignable types, hb_qsort fallback for packed OT structs
with deleted move ops.
hb_qsort itself is simplified to just forward to system qsort().
sort_r_simple is retained as fallback when qsort is unavailable.
Convert all direct hb_qsort callers to use typed array qsort with
lambdas. Remove dead comparator functions.
225 tests pass, 0 failures.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Reduce to one templated hb_qsort, one templated qsort() on
hb_array_t/hb_vector_t, plus the no-arg qsort() that casts
Type::cmp to resolve ambiguity.
Convert all external call sites to lambdas so comparators
inline everywhere. Remove the void*-arg overload and the
plain function-pointer overload.
225 tests pass, 0 failures.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Make sort_r_simple and sort_r_cmpswap take the comparator as a
template parameter instead of a function pointer. This allows
lambdas and functors to inline into the sort, avoiding the
indirect-call overhead.
The function-pointer overloads remain for backward compatibility
and for the __OPTIMIZE_SIZE__ / qsort_r paths.
17% speedup on GPU encode benchmark (NotoSansCJK).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add an hb_array_t::qsort overload that takes a comparator with a
void* context argument, forwarding to hb_qsort's existing qsort_r
support. Use it in the GPU encoder to eliminate the last std::
usage from library code.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
../src/hb-algs.hh:240:43: runtime error: reference binding to misaligned address 0x7ffe91a08b0e for type 'const unsigned int', which requires 4 byte alignment
./subprojects/harfbuzz/src/hb-array.hh:71:25: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const]
constexpr hb_array_t& operator = (const hb_array_t<U> &o)
^
const
../subprojects/harfbuzz/src/hb-array.hh:72:5: warning: use of this statement in a constexpr function is a C++14 extension [-Wc++14-extensions]
{ arrayZ = o.arrayZ; length = o.length; backwards_length = o.backwards_length; return *this; }
^
../subprojects/harfbuzz/src/hb-array.hh:329:32: warning: 'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior [-Wconstexpr-not-const]
constexpr hb_sorted_array_t& operator = (const hb_array_t<U> &o)
^
const
../subprojects/harfbuzz/src/hb-array.hh:330:5: warning: use of this statement in a constexpr function is a C++14 extension [-Wc++14-extensions]
{ hb_array_t<Type> (*this) = o; return *this; }
^
4 warnings generated.