From a34f8b3141c9bb1cfa6212467069d39744bfadcb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 11 Sep 2025 19:10:05 -0600 Subject: [PATCH 01/10] [instancer] Use bitset instead of vector for VarStore optimizer columns ~30% faster on GoogleSansFlex. --- src/hb-ot-layout-common.hh | 31 ++++++++++++------------------- src/hb-ot-var-common.hh | 2 +- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index cfd5ee5ca..11341b1b3 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2333,7 +2333,7 @@ struct delta_row_encoding_t * needed for this region */ hb_vector_t chars; unsigned width = 0; - hb_vector_t columns; + hb_bit_set_t columns; unsigned overhead = 0; hb_vector_t*> items; @@ -2403,24 +2403,21 @@ struct delta_row_encoding_t return ret; } - hb_vector_t get_columns () + hb_bit_set_t get_columns () { - hb_vector_t cols; - cols.alloc (chars.length); - for (auto v : chars) + hb_bit_set_t cols; + for (auto _ : + hb_enumerate (chars)) { - uint8_t flag = v ? 1 : 0; - cols.push (flag); + if (_.second) + cols.add (_.first); } return cols; } - static inline unsigned get_chars_overhead (const hb_vector_t& cols) + static inline unsigned get_chars_overhead (const hb_bit_set_t& cols) { unsigned c = 4 + 6; // 4 bytes for LOffset, 6 bytes for VarData header - unsigned cols_bit_count = 0; - for (auto v : cols) - if (v) cols_bit_count++; + unsigned cols_bit_count = cols.get_population (); return c + cols_bit_count * 2; } @@ -2431,19 +2428,15 @@ struct delta_row_encoding_t } int gain_from_merging (const delta_row_encoding_t& other_encoding, - hb_vector_t &scratch) const + hb_bit_set_t &scratch) const { int combined_width = 0; for (unsigned i = 0; i < chars.length; i++) combined_width += hb_max (chars.arrayZ[i], other_encoding.chars.arrayZ[i]); - hb_vector_t &combined_columns = scratch; - combined_columns.reset (); - - if (unlikely (!combined_columns.resize (columns.length, false))) - return 0; - for (unsigned i = 0; i < columns.length; i++) - combined_columns.arrayZ[i] = columns.arrayZ[i] | other_encoding.columns.arrayZ[i]; + hb_bit_set_t &combined_columns = scratch; + combined_columns = columns; + combined_columns.union_ (other_encoding.columns); int combined_overhead = get_chars_overhead (combined_columns); int combined_gain = (int) overhead + (int) other_encoding.overhead - combined_overhead diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index e1a872819..28c58e049 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1893,7 +1893,7 @@ struct item_variations_t /* sort encoding_objs */ encoding_objs.qsort (); - hb_vector_t scratch; + hb_bit_set_t scratch; /* main algorithm: repeatedly pick 2 best encodings to combine, and combine * them */ From 266e1c2481510c39f014fc4fb90f003b0722bccc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 11 Sep 2025 20:12:39 -0600 Subject: [PATCH 02/10] [instancer] Short-circuit some work in gain_from_merging --- src/hb-ot-layout-common.hh | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 11341b1b3..38d1a847f 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2430,18 +2430,26 @@ struct delta_row_encoding_t int gain_from_merging (const delta_row_encoding_t& other_encoding, hb_bit_set_t &scratch) const { + int combined_gain = (int) overhead + (int) other_encoding.overhead; + int combined_width = 0; for (unsigned i = 0; i < chars.length; i++) combined_width += hb_max (chars.arrayZ[i], other_encoding.chars.arrayZ[i]); + combined_gain -= (combined_width - (int) width) * items.length; + combined_gain -= (combined_width - (int) other_encoding.width) * other_encoding.items.length; + + // If the returned gain is negative, the actual magnitude is unused. + // Short-circuit if that is gonna happen. + if (combined_gain <= 0) + return combined_gain; + hb_bit_set_t &combined_columns = scratch; combined_columns = columns; combined_columns.union_ (other_encoding.columns); int combined_overhead = get_chars_overhead (combined_columns); - int combined_gain = (int) overhead + (int) other_encoding.overhead - combined_overhead - - (combined_width - (int) width) * items.length - - (combined_width - (int) other_encoding.width) * other_encoding.items.length; + combined_gain -= combined_overhead; return combined_gain; } From c938e8ce881e82103d397a0c567bfa83e9cc989c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 11 Sep 2025 21:57:14 -0600 Subject: [PATCH 03/10] [instancer] Micro-optimize by using bit-set directly --- src/hb-ot-var-common.hh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 28c58e049..6423e9cad 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -592,7 +592,7 @@ struct tuple_delta_t return true; if (unlikely (end_points.in_error ())) return false; - hb_set_t inferred_idxes; + hb_bit_set_t inferred_idxes; unsigned start_point = 0; for (unsigned end_point : end_points) { @@ -1909,7 +1909,7 @@ struct item_variations_t } } - hb_set_t removed_todo_idxes; + hb_bit_set_t removed_todo_idxes; while (queue) { auto t = queue.pop_minimum ().first; From 1a6c6d7865292ffda2afad2afd0b20bae176afda Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 11 Sep 2025 21:59:41 -0600 Subject: [PATCH 04/10] [instancer] Micro-optimize a couple of hot comparisons --- src/hb-ot-var-common.hh | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 6423e9cad..599589c67 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1762,19 +1762,16 @@ struct item_variations_t bool operator < (const combined_gain_idx_tuple_t& o) { - if (gain != o.gain) - return gain < o.gain; - - if (idx_1 != o.idx_1) - return idx_1 < o.idx_1; - - return idx_2 < o.idx_2; + return gain < o.gain || + (gain == o.gain && (idx_1 < o.idx_1 || + (idx_1 == o.idx_1 && idx_2 < o.idx_2))); } bool operator <= (const combined_gain_idx_tuple_t& o) { - if (*this < o) return true; - return gain == o.gain && idx_1 == o.idx_1 && idx_2 == o.idx_2; + return gain < o.gain || + (gain == o.gain && (idx_1 < o.idx_1 || + (idx_1 == o.idx_1 && idx_2 <= o.idx_2))); } }; From 9d9711698b6b4cd79ea48b23bf57e14e06ac9add Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 11 Sep 2025 23:10:50 -0600 Subject: [PATCH 05/10] [instancer/varstore] Add a chars_t type --- src/hb-ot-layout-common.hh | 12 ++++++++---- src/hb-ot-var-common.hh | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 38d1a847f..7ac61703a 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2331,14 +2331,18 @@ struct delta_row_encoding_t { /* each byte represents a region, value is one of 0/1/2/4, which means bytes * needed for this region */ - hb_vector_t chars; + struct chars_t : hb_vector_t + { + }; + + chars_t chars; unsigned width = 0; hb_bit_set_t columns; unsigned overhead = 0; hb_vector_t*> items; delta_row_encoding_t () = default; - delta_row_encoding_t (hb_vector_t&& chars_, + delta_row_encoding_t (chars_t&& chars_, const hb_vector_t* row = nullptr) : delta_row_encoding_t () @@ -2353,9 +2357,9 @@ struct delta_row_encoding_t bool is_empty () const { return !items; } - static hb_vector_t get_row_chars (const hb_vector_t& row) + static chars_t get_row_chars (const hb_vector_t& row) { - hb_vector_t ret; + chars_t ret; if (!ret.alloc (row.length)) return ret; bool long_words = false; diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 599589c67..dd2b49b73 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1857,7 +1857,7 @@ struct item_variations_t if (!front_mapping.set ((major<<16) + minor, &row)) return false; - hb_vector_t chars = delta_row_encoding_t::get_row_chars (row); + auto chars = delta_row_encoding_t::get_row_chars (row); if (!chars) return false; if (delta_rows_map.has (&row)) @@ -1922,7 +1922,7 @@ struct item_variations_t removed_todo_idxes.add (i); removed_todo_idxes.add (j); - hb_vector_t combined_chars; + delta_row_encoding_t::chars_t combined_chars; if (!combined_chars.alloc (encoding.chars.length)) return false; From fe6c9a0f7305bd9d1d196bc29b9204a7a1a6a4fc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 11 Sep 2025 23:23:41 -0600 Subject: [PATCH 06/10] [instancer/varstore] Fix a logic bug combined_chars was moved already. Also, see comment. --- src/hb-ot-var-common.hh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index dd2b49b73..726eba446 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1941,8 +1941,15 @@ struct item_variations_t if (removed_todo_idxes.has (idx)) continue; const delta_row_encoding_t& obj = encoding_objs.arrayZ[idx]; - if (obj.chars == combined_chars) + // In the unlikely event that the same encoding exists already, combine it. + if (obj.chars == combined_encoding_obj.chars) { + // This is straight port from fonttools algorithm. I added this branch there + // because I thought it can happen. But looks like we never get in here in + // practice. I'm not confident enough to remove it though; in theory it can + // happen. I think it's just that our tests are not extensive enough to hit + // this path. + for (const auto& row : obj.items) combined_encoding_obj.add_row (row); From 3dc5e3d689c45663a01caf90120f4bc75a55cdfb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 11 Sep 2025 23:27:51 -0600 Subject: [PATCH 07/10] [instancer/varstore] Consolidate chars_t logic --- src/hb-ot-layout-common.hh | 166 ++++++++++++++++++++++--------------- src/hb-ot-var-common.hh | 12 +-- 2 files changed, 99 insertions(+), 79 deletions(-) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 7ac61703a..404a352ce 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2333,8 +2333,101 @@ struct delta_row_encoding_t * needed for this region */ struct chars_t : hb_vector_t { + static chars_t get_row_chars (const hb_vector_t& row) + { + chars_t ret; + if (!ret.alloc (row.length)) return ret; + + bool long_words = false; + + /* 0/1/2 byte encoding */ + for (int i = row.length - 1; i >= 0; i--) + { + int v = row.arrayZ[i]; + if (v == 0) + ret.push (0); + else if (v > 32767 || v < -32768) + { + long_words = true; + break; + } + else if (v > 127 || v < -128) + ret.push (2); + else + ret.push (1); + } + + if (!long_words) + return ret; + + /* redo, 0/2/4 bytes encoding */ + ret.reset (); + for (int i = row.length - 1; i >= 0; i--) + { + int v = row.arrayZ[i]; + if (v == 0) + ret.push (0); + else if (v > 32767 || v < -32768) + ret.push (4); + else + ret.push (2); + } + return ret; + } + + inline unsigned get_width () + { + unsigned ret = + iter () + | hb_reduce (hb_add, 0u) + ; + return ret; + } + + hb_bit_set_t get_columns () + { + hb_bit_set_t cols; + for (auto _ : + hb_enumerate (iter ())) + { + if (_.second) + cols.add (_.first); + } + return cols; + } + + int cmp (const chars_t& other) const + { + return as_array ().cmp (other.as_array ()); + } + + chars_t combine_chars (const chars_t& other) const + { + chars_t combined_chars; + if (!combined_chars.alloc (length)) + return combined_chars; + + for (unsigned idx = 0; idx < length; idx++) + { + uint8_t v = hb_max (arrayZ[idx], other.arrayZ[idx]); + combined_chars.push (v); + } + return combined_chars; + } + + unsigned combine_width (const chars_t& other) const + { + int combined_width = 0; + for (unsigned i = 0; i < length; i++) + combined_width += hb_max (arrayZ[i], other.arrayZ[i]); + return combined_width; + } }; + chars_t combine_chars (const delta_row_encoding_t& other_encoding) const { return chars.combine_chars (other_encoding.chars); } + unsigned combine_width (const delta_row_encoding_t& other_encoding) const { return chars.combine_width (other_encoding.chars); } + static chars_t get_row_chars (const hb_vector_t& row) { return chars_t::get_row_chars (row); } + + // Actual data + chars_t chars; unsigned width = 0; hb_bit_set_t columns; @@ -2348,8 +2441,8 @@ struct delta_row_encoding_t { chars = std::move (chars_); - width = get_width (); - columns = get_columns (); + width = chars.get_width (); + columns = chars.get_columns (); overhead = get_chars_overhead (columns); if (row) items.push (row); } @@ -2357,67 +2450,6 @@ struct delta_row_encoding_t bool is_empty () const { return !items; } - static chars_t get_row_chars (const hb_vector_t& row) - { - chars_t ret; - if (!ret.alloc (row.length)) return ret; - - bool long_words = false; - - /* 0/1/2 byte encoding */ - for (int i = row.length - 1; i >= 0; i--) - { - int v = row.arrayZ[i]; - if (v == 0) - ret.push (0); - else if (v > 32767 || v < -32768) - { - long_words = true; - break; - } - else if (v > 127 || v < -128) - ret.push (2); - else - ret.push (1); - } - - if (!long_words) - return ret; - - /* redo, 0/2/4 bytes encoding */ - ret.reset (); - for (int i = row.length - 1; i >= 0; i--) - { - int v = row.arrayZ[i]; - if (v == 0) - ret.push (0); - else if (v > 32767 || v < -32768) - ret.push (4); - else - ret.push (2); - } - return ret; - } - - inline unsigned get_width () - { - unsigned ret = + hb_iter (chars) - | hb_reduce (hb_add, 0u) - ; - return ret; - } - - hb_bit_set_t get_columns () - { - hb_bit_set_t cols; - for (auto _ : + hb_enumerate (chars)) - { - if (_.second) - cols.add (_.first); - } - return cols; - } - static inline unsigned get_chars_overhead (const hb_bit_set_t& cols) { unsigned c = 4 + 6; // 4 bytes for LOffset, 6 bytes for VarData header @@ -2436,9 +2468,7 @@ struct delta_row_encoding_t { int combined_gain = (int) overhead + (int) other_encoding.overhead; - int combined_width = 0; - for (unsigned i = 0; i < chars.length; i++) - combined_width += hb_max (chars.arrayZ[i], other_encoding.chars.arrayZ[i]); + int combined_width = combine_width (other_encoding); combined_gain -= (combined_width - (int) width) * items.length; combined_gain -= (combined_width - (int) other_encoding.width) * other_encoding.items.length; @@ -2469,7 +2499,7 @@ struct delta_row_encoding_t if (gain_a != gain_b) return gain_a - gain_b; - return (b->chars).as_array ().cmp ((a->chars).as_array ()); + return b->chars.cmp (a->chars); } static int cmp_width (const void *pa, const void *pb) @@ -2480,7 +2510,7 @@ struct delta_row_encoding_t if (a->width != b->width) return (int) a->width - (int) b->width; - return (b->chars).as_array ().cmp ((a->chars).as_array ()); + return b->chars.cmp (a->chars); } bool add_row (const hb_vector_t* row) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 726eba446..00a77ba88 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1922,17 +1922,7 @@ struct item_variations_t removed_todo_idxes.add (i); removed_todo_idxes.add (j); - delta_row_encoding_t::chars_t combined_chars; - if (!combined_chars.alloc (encoding.chars.length)) - return false; - - for (unsigned idx = 0; idx < encoding.chars.length; idx++) - { - uint8_t v = hb_max (encoding.chars.arrayZ[idx], other_encoding.chars.arrayZ[idx]); - combined_chars.push (v); - } - - delta_row_encoding_t combined_encoding_obj (std::move (combined_chars)); + delta_row_encoding_t combined_encoding_obj (std::move (encoding.combine_chars (other_encoding))); for (const auto& row : hb_concat (encoding.items, other_encoding.items)) combined_encoding_obj.add_row (row); From aaeea5729b97813eb71b1d809212534a77436ca2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 11 Sep 2025 23:52:44 -0600 Subject: [PATCH 08/10] [instancer/varstore] Change chars_t storage scheme Slows down. Gonna revert. Apparently builtin popcount is not fast unless built for archs that support it. --- src/hb-ot-layout-common.hh | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 404a352ce..3b6e3c0c2 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2329,8 +2329,8 @@ static inline bool ClassDef_serialize (hb_serialize_context_t *c, /* ported from fonttools (class _Encoding) */ struct delta_row_encoding_t { - /* each byte represents a region, value is one of 0/1/2/4, which means bytes - * needed for this region */ + /* each byte represents a region, value is one of 0/1/3/15, which means + * 0/1/2/4 bytes needed for this region respectively (one bit for each) */ struct chars_t : hb_vector_t { static chars_t get_row_chars (const hb_vector_t& row) @@ -2352,7 +2352,7 @@ struct delta_row_encoding_t break; } else if (v > 127 || v < -128) - ret.push (2); + ret.push (3); else ret.push (1); } @@ -2368,18 +2368,23 @@ struct delta_row_encoding_t if (v == 0) ret.push (0); else if (v > 32767 || v < -32768) - ret.push (4); + ret.push (15); else - ret.push (2); + ret.push (3); } return ret; } inline unsigned get_width () { - unsigned ret = + iter () - | hb_reduce (hb_add, 0u) - ; + unsigned ret = 0; + unsigned i = 0; + for (; i + 8 <= length; i += 8) + ret += hb_popcount (* (uint64_t *) &arrayZ[i]); + for (; i + 4 <= length; i += 4) + ret += hb_popcount (* (uint32_t *) &arrayZ[i]); + for (; i < length; i++) + ret += hb_popcount (arrayZ[i]); return ret; } @@ -2407,7 +2412,7 @@ struct delta_row_encoding_t for (unsigned idx = 0; idx < length; idx++) { - uint8_t v = hb_max (arrayZ[idx], other.arrayZ[idx]); + uint8_t v = arrayZ[idx] | other.arrayZ[idx]; combined_chars.push (v); } return combined_chars; @@ -2415,10 +2420,15 @@ struct delta_row_encoding_t unsigned combine_width (const chars_t& other) const { - int combined_width = 0; - for (unsigned i = 0; i < length; i++) - combined_width += hb_max (arrayZ[i], other.arrayZ[i]); - return combined_width; + unsigned ret = 0; + unsigned i = 0; + for (; i + 8 <= length; i += 8) + ret += hb_popcount (* (uint64_t *) &arrayZ[i] | * (uint64_t *) &other.arrayZ[i]); + for (; i + 4 <= length; i += 4) + ret += hb_popcount (* (uint32_t *) &arrayZ[i] | * (uint32_t *) &other.arrayZ[i]); + for (; i < length; i++) + ret += hb_popcount (arrayZ[i] | other.arrayZ[i]); + return ret; } }; From 12c4623757b8bd22c734b1b6d583062153f81e97 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 12 Sep 2025 00:05:37 -0600 Subject: [PATCH 09/10] Revert "[instancer/varstore] Change chars_t storage scheme" This reverts commit aaeea5729b97813eb71b1d809212534a77436ca2. --- src/hb-ot-layout-common.hh | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 3b6e3c0c2..404a352ce 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2329,8 +2329,8 @@ static inline bool ClassDef_serialize (hb_serialize_context_t *c, /* ported from fonttools (class _Encoding) */ struct delta_row_encoding_t { - /* each byte represents a region, value is one of 0/1/3/15, which means - * 0/1/2/4 bytes needed for this region respectively (one bit for each) */ + /* each byte represents a region, value is one of 0/1/2/4, which means bytes + * needed for this region */ struct chars_t : hb_vector_t { static chars_t get_row_chars (const hb_vector_t& row) @@ -2352,7 +2352,7 @@ struct delta_row_encoding_t break; } else if (v > 127 || v < -128) - ret.push (3); + ret.push (2); else ret.push (1); } @@ -2368,23 +2368,18 @@ struct delta_row_encoding_t if (v == 0) ret.push (0); else if (v > 32767 || v < -32768) - ret.push (15); + ret.push (4); else - ret.push (3); + ret.push (2); } return ret; } inline unsigned get_width () { - unsigned ret = 0; - unsigned i = 0; - for (; i + 8 <= length; i += 8) - ret += hb_popcount (* (uint64_t *) &arrayZ[i]); - for (; i + 4 <= length; i += 4) - ret += hb_popcount (* (uint32_t *) &arrayZ[i]); - for (; i < length; i++) - ret += hb_popcount (arrayZ[i]); + unsigned ret = + iter () + | hb_reduce (hb_add, 0u) + ; return ret; } @@ -2412,7 +2407,7 @@ struct delta_row_encoding_t for (unsigned idx = 0; idx < length; idx++) { - uint8_t v = arrayZ[idx] | other.arrayZ[idx]; + uint8_t v = hb_max (arrayZ[idx], other.arrayZ[idx]); combined_chars.push (v); } return combined_chars; @@ -2420,15 +2415,10 @@ struct delta_row_encoding_t unsigned combine_width (const chars_t& other) const { - unsigned ret = 0; - unsigned i = 0; - for (; i + 8 <= length; i += 8) - ret += hb_popcount (* (uint64_t *) &arrayZ[i] | * (uint64_t *) &other.arrayZ[i]); - for (; i + 4 <= length; i += 4) - ret += hb_popcount (* (uint32_t *) &arrayZ[i] | * (uint32_t *) &other.arrayZ[i]); - for (; i < length; i++) - ret += hb_popcount (arrayZ[i] | other.arrayZ[i]); - return ret; + int combined_width = 0; + for (unsigned i = 0; i < length; i++) + combined_width += hb_max (arrayZ[i], other.arrayZ[i]); + return combined_width; } }; From aba798ddab246faea91f069d31dabaf33a90fc53 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 12 Sep 2025 00:52:40 -0600 Subject: [PATCH 10/10] [instancer/varstore] Use heapify() instead of inserts() --- src/hb-ot-var-common.hh | 10 ++++++++-- src/hb-priority-queue.hh | 12 +++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 00a77ba88..7b100c322 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1894,7 +1894,8 @@ struct item_variations_t /* main algorithm: repeatedly pick 2 best encodings to combine, and combine * them */ - hb_priority_queue_t queue; + using item_t = hb_priority_queue_t::item_t; + hb_vector_t queue_items; unsigned num_todos = encoding_objs.length; for (unsigned i = 0; i < num_todos; i++) { @@ -1902,10 +1903,15 @@ struct item_variations_t { int combining_gain = encoding_objs.arrayZ[i].gain_from_merging (encoding_objs.arrayZ[j], scratch); if (combining_gain > 0) - queue.insert (combined_gain_idx_tuple_t (-combining_gain, i, j), 0); + { + auto item = item_t (combined_gain_idx_tuple_t (-combining_gain, i, j), 0); + queue_items.push (item); + } } } + hb_priority_queue_t queue (std::move (queue_items)); + hb_bit_set_t removed_todo_idxes; while (queue) { diff --git a/src/hb-priority-queue.hh b/src/hb-priority-queue.hh index 274d5df4c..753e86b8c 100644 --- a/src/hb-priority-queue.hh +++ b/src/hb-priority-queue.hh @@ -45,12 +45,22 @@ template struct hb_priority_queue_t { - private: + public: typedef hb_pair_t item_t; + + private: hb_vector_t heap; public: + hb_priority_queue_t () = default; + hb_priority_queue_t (hb_vector_t&& other) : heap (std::move (other)) + { + // Heapify the vector. + for (int i = (heap.length / 2) - 1; i >= 0; i--) + bubble_down (i); + } + void reset () { heap.resize (0); } bool in_error () const { return heap.in_error (); }