Commit Graph
149 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 4.7 d3a1de25e3 [array] Replace std::sort with internal sort_r_simple
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>
2026-05-02 11:32:17 -06:00
Behdad Esfahbod 5fac9a0560 [array] Adapt qsort comparator return type to std::sort's bool
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>
2026-04-14 16:16:22 -06:00
jfkthame 603ab95602 Return size_t rather than unsigned int from get_size() (#5915)
* [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
2026-04-13 11:01:19 -06:00
Behdad EsfahbodandClaude Opus 4.6 52f0115c0f [algs] Use std::sort in hb_array_t::qsort; simplify hb_qsort
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>
2026-03-27 13:14:56 -06:00
Behdad EsfahbodandClaude Opus 4.6 df433afe6c [algs] Simplify hb_qsort: single template, inline all callers
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>
2026-03-27 12:50:30 -06:00
Behdad EsfahbodandClaude Opus 4.6 05762b2fac [algs] Template hb_qsort comparator for inlining
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>
2026-03-27 12:36:59 -06:00
Behdad EsfahbodandClaude Opus 4.6 33ad6419e3 [gpu] Remove std::sort; add qsort with context to hb_array_t
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>
2026-03-26 19:48:10 -06:00
Behdad Esfahbod f632633900 [array] Fix build 2025-11-21 17:47:14 -07:00
Behdad Esfahbod 072299a25c [gvar] More speedup of drawing mega-var-fonts
Another 4% in GoogleSansFlex.
2025-11-21 17:37:00 -07:00
Behdad Esfahbod 1a2780735c [array] Micro-optimize 2025-02-05 00:35:40 +00:00
Behdad Esfahbod 29d49eced8 [vector] Introduce realloc_moves tag
Some types (even aggregate) can be moved using realloc().
Extend the fast path to hb_hashmap and tuple_delta_t.
2023-11-01 15:55:47 -06:00
Behdad Esfahbod 6a17622a75 [array] Speed up iteration
These are faster than relying on the random-access methods
(forward, rewind, item_it).
2023-06-22 18:35:10 -06:00
Behdad Esfahbod 0364c69e6d [iter] Add has_fast_len
Set iterators are not random_access, but have fast len().
2023-06-07 15:52:18 -06:00
Behdad Esfahbod 826fe2c9f7 [hash] Wrap specialization in HB_OPTIMIZE_SIZE_MORE 2023-05-09 13:48:38 -06:00
Behdad Esfahbod b2b15fa30a [hash] Links 2023-05-09 13:39:52 -06:00
Behdad Esfahbod fe0f7dc57b [bytes] Use fasthash as hash algorithm
Part of https://github.com/harfbuzz/harfbuzz/pull/4228
2023-05-08 22:19:02 -06:00
Behdad Esfahbod c005e3a2e3 [bytes] Simplify hash function
Part of https://github.com/harfbuzz/harfbuzz/issues/4227
2023-05-08 16:33:31 -06:00
Behdad Esfahbod 45afbdff2c [array] Add a const to a cast 2023-05-08 11:19:55 -06:00
Behdad Esfahbod 6d7de2f8dd [array] Implement FNV-1a hash function 2023-05-07 15:23:05 -06:00
Behdad Esfahbod ebdeab8baa [array] Improve hash function
Previously all arrays of 0 bytes were getting same hash.
2023-05-07 14:54:02 -06:00
Behdad Esfahbod ef2a8f722f [VarComposite] Adjust for RESET_UNSPECIFIED_AXES semantic change
https://github.com/harfbuzz/boring-expansion-spec/issues/81
2023-02-06 12:04:16 -07:00
Behdad Esfahbod a24d4e9261 [array] Oops. Fix memcpy copy()! 2022-12-02 12:01:35 -07:00
Behdad Esfahbod 5e41766bb9 [array] Fix hb_bytes_t memcpy copy
Wasn't being used!
2022-12-02 12:01:35 -07:00
Behdad Esfahbod 3b68c7146f [array] Don't clear serializer buffer when copying out
Not needed.
2022-12-01 14:19:27 -07:00
Behdad Esfahbod d8d0e06694 [array] Comment 2022-11-29 21:35:54 -07:00
Behdad Esfahbod 3131aecf9f [array/hash] Fix asan issue
../src/hb-algs.hh:240:43: runtime error: reference binding to misaligned address 0x7ffe91a08b0e for type 'const unsigned int', which requires 4 byte alignment
2022-11-28 14:12:55 -07:00
Behdad Esfahbod 9d18180c3c [array] Use hb_hash instead of handrolling 2022-11-26 15:38:21 -07:00
Behdad Esfahbod e333223f26 [array] Optimize serializing copy() 2022-11-25 14:26:02 -07:00
Behdad Esfahbod dce3502e10 [array] Add commented-out static asserts
They don't work.
2022-11-23 18:34:54 -07:00
Behdad Esfahbod ae578705c2 [array] Write hash as range for loop again
Now that our range loop is faster than our own iter.
2022-11-22 12:23:17 -07:00
Behdad Esfahbod bca569ae53 [array] Speed up hash() for byte arrays 2022-11-21 23:19:42 -07:00
Behdad Esfahbod d7b492e3f5 Revert "[array] Remove hash specializations for bytes"
This reverts commit 213117317c.
2022-11-21 23:08:51 -07:00
Behdad Esfahbod b00a911fa7 [sorted-array] Add faster iterator implementation here as well 2022-11-21 12:11:30 -07:00
Behdad Esfahbod e82a3c69dd [array/vector] Optimize range-based for-loop
Avoid bounds-checking.
2022-11-21 12:00:10 -07:00
Behdad Esfahbod 921f45f46d [array] Rewrite hash() as dagger 2022-11-18 17:26:58 -07:00
Behdad Esfahbod 213117317c [array] Remove hash specializations for bytes
Not needed.
2022-11-18 17:24:44 -07:00
Behdad Esfahbod c7d57dcf26 [vector/array] Simplify qsort() 2022-11-16 21:21:31 -07:00
Behdad Esfahbod a87843be3d [array] Another try at sizeof sorted_array 2022-11-14 13:43:58 -07:00
Behdad Esfahbod df040de9b4 [array] Add a std::forward 2022-08-18 16:15:00 -06:00
Behdad Esfahbod c606978ee0 [array] Remove unused type pack 2022-08-18 16:13:56 -06:00
Behdad Esfahbod b475a2ab29 [array] Adjust operator != 2022-07-17 18:44:11 -06:00
Behdad Esfahbod 4b3afafb00 [array] Use hb_swap() in reverse() 2022-07-13 15:13:07 -06:00
Behdad Esfahbod 97ea10a63a Remove old nullptr_t hacks
Were used for hashmap before.
2022-06-02 11:23:38 -06:00
Behdad Esfahbod 975a5f9194 [array] Use hb_memcmp instead of memcmp
Fixes ubsan error.
2022-05-20 12:34:49 -06:00
Behdad Esfahbod 4266cf3be2 [array] Specialize operator== for bytes_t and ubytes_t 2022-05-19 18:16:54 -06:00
Behdad Esfahbod 1c8226ed14 Fix compiler warning
On Mac compiler:

FAILED: src/libharfbuzz.0.dylib.p/hb-ot-tag.cc.o
c++ -Isrc/libharfbuzz.0.dylib.p -Isrc -I../src -I. -I.. -I/usr/local/opt/freetype/include/freetype2 -I/usr/local/Cellar/graphite2/1.3.14/include -I/usr/local/Cellar/glib/2.72.1/include/glib-2.0 -I/usr/local/Cellar/glib/2.72.1/lib/glib-2.0/include -I/usr/local/opt/gettext/include -I/usr/local/Cellar/pcre/8.45/include -Xclang -fcolor-diagnostics --coverage -pipe -Wall -Winvalid-pch -Wnon-virtual-dtor -std=c++11 -fno-rtti -O2 -g -fno-exceptions -fno-rtti -fno-threadsafe-statics -fvisibility-inlines-hidden -DHAVE_CONFIG_H -Wno-non-virtual-dtor -MD -MQ src/libharfbuzz.0.dylib.p/hb-ot-tag.cc.o -MF src/libharfbuzz.0.dylib.p/hb-ot-tag.cc.o.d -o src/libharfbuzz.0.dylib.p/hb-ot-tag.cc.o -c ../src/hb-ot-tag.cc
In file included from ../src/hb-ot-tag.cc:29:
In file included from ../src/hb.hh:481:
../src/hb-array.hh:359:14: error: missing default argument on parameter 'ds'
              Ts... ds) const
                    ^
../src/hb-ot-tag.cc:292:58: note: in instantiation of function template specialization 'hb_sorted_array_t<const LangTag>::bfind<const char *, unsigned int>' requested here
    if (hb_sorted_array (ot_languages, ot_languages_len).bfind (lang_str, &tag_idx,
                                                         ^
1 error generated.
2022-05-17 15:28:50 -06:00
Behdad Esfahbod c1f4b57c06 [ot-tags] Optimize language comparison
Now that we know both strings are of equal len of 2 or 3, optimize.

Part of https://github.com/harfbuzz/harfbuzz/issues/3591

Before:
------------------------------------------------------------------------------------------------
Benchmark                                                      Time             CPU   Iterations
------------------------------------------------------------------------------------------------
BM_hb_ot_tags_from_script_and_language/COMMON abcd_XY       8.50 ns         8.47 ns     81221549
BM_hb_ot_tags_from_script_and_language/COMMON zh_CN         79.6 ns         79.3 ns      8785804
BM_hb_ot_tags_from_script_and_language/COMMON en_US         40.0 ns         39.9 ns     17462768
BM_hb_ot_tags_from_script_and_language/LATIN en_US          39.2 ns         39.1 ns     17886793
BM_hb_ot_tags_from_script_and_language/COMMON none          4.31 ns         4.30 ns    162805417
BM_hb_ot_tags_from_script_and_language/LATIN none           4.32 ns         4.31 ns    162656688

After:
------------------------------------------------------------------------------------------------
Benchmark                                                      Time             CPU   Iterations
------------------------------------------------------------------------------------------------
BM_hb_ot_tags_from_script_and_language/COMMON abcd_XY       8.27 ns         8.24 ns     81868701
BM_hb_ot_tags_from_script_and_language/COMMON zh_CN         56.1 ns         56.0 ns     12353284
BM_hb_ot_tags_from_script_and_language/COMMON en_US         24.3 ns         24.2 ns     28955030
BM_hb_ot_tags_from_script_and_language/LATIN en_US          24.5 ns         24.4 ns     28664868
BM_hb_ot_tags_from_script_and_language/COMMON none          4.35 ns         4.34 ns    161190014
BM_hb_ot_tags_from_script_and_language/LATIN none           4.36 ns         4.34 ns    161319000
2022-05-17 15:19:40 -06:00
luz paz e2e305066a Fix various typos
Found via `codespell -q 3 -S ./perf/texts -L actualy,ba,beng,fo,gir,inout,nd,ot,pres,ro,te,teh,timne`
2022-01-16 05:39:03 -08:00
Behdad Esfahbod 325f26290e [array] Fix a couple constexpr warnings
./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.
2021-11-19 17:19:09 -07:00
Behdad Esfahbod 4731b10736 [array] Mark constructors constexpr
../src/hb-map.hh:44:38: note: non-constexpr constructor 'hb_array_t' cannot be used in a constant expression
  static constexpr K INVALID_KEY   = kINVALID;
                                     ^
2021-11-19 12:33:37 -07:00