From a4a508920154e33fb73df3be06add06c2e39025f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 12 Sep 2025 23:41:40 -0600 Subject: [PATCH] [instancer/varstore] Speed up encoding merging Don't recalculate chars from all row data. --- src/hb-ot-layout-common.hh | 31 +++++++++++++++++++++++-------- src/hb-ot-var-common.hh | 13 +++++++------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 6b4d67383..31a1fefc6 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2371,17 +2371,33 @@ struct delta_row_encoding_t hb_vector_t*> items; delta_row_encoding_t () = default; - template *))> - delta_row_encoding_t (iter_t rows = hb_array_t *> ()) + delta_row_encoding_t (hb_vector_t*> &&rows, unsigned num_cols) { assert (rows); - for (auto row : rows) - add_row (row); + items = std::move (rows); + + if (unlikely (!chars.resize (num_cols))) + return; calculate_chars (); + } + void merge (const delta_row_encoding_t& other) + { + items.alloc (items.length + other.items.length); + for (auto row : other.items) + add_row (row); + + // Merge chars + assert (chars.length == other.chars.length); + for (unsigned i = 0; i < chars.length; i++) + chars.arrayZ[i] = hb_max (chars.arrayZ[i], other.chars.arrayZ[i]); + chars_changed (); + } + + void chars_changed () + { auto _ = chars.get_width (); width = _.first; overhead = get_chars_overhead (_.second); @@ -2391,9 +2407,6 @@ struct delta_row_encoding_t { assert (items); - if (unlikely (!chars.resize (items[0]->length))) - return; - bool long_words = false; for (auto row : items) @@ -2425,6 +2438,8 @@ struct delta_row_encoding_t if (v == 1) v = 2; } + + chars_changed (); } bool is_empty () const diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index 8cfdf5d1f..aed317bee 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1858,7 +1858,7 @@ struct item_variations_t } if (major_rows) - encoding_objs.push (delta_row_encoding_t (major_rows)); + encoding_objs.push (delta_row_encoding_t (std::move (major_rows), num_cols)); start_row += num_rows; } @@ -1910,7 +1910,7 @@ struct item_variations_t removed_todo_idxes.add (i); removed_todo_idxes.add (j); - delta_row_encoding_t combined_encoding_obj (hb_concat (encoding.items, other_encoding.items)); + encoding.merge (other_encoding); for (unsigned idx = 0; idx < encoding_objs.length; idx++) { @@ -1918,7 +1918,7 @@ struct item_variations_t const delta_row_encoding_t& obj = encoding_objs.arrayZ[idx]; // In the unlikely event that the same encoding exists already, combine it. - if (obj.width == combined_encoding_obj.width && obj.chars == combined_encoding_obj.chars) + if (obj.width == encoding.width && obj.chars == encoding.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 @@ -1927,18 +1927,19 @@ struct item_variations_t // this path. for (const auto& row : obj.items) - combined_encoding_obj.add_row (row); + encoding.add_row (row); removed_todo_idxes.add (idx); continue; } - int combined_gain = combined_encoding_obj.gain_from_merging (obj); + int combined_gain = encoding.gain_from_merging (obj); if (combined_gain > 0) queue.insert (combined_gain_idx_tuple_t (combined_gain, idx, encoding_objs.length), 0); } - encoding_objs.push (std::move (combined_encoding_obj)); + auto moved_encoding = std::move (encoding); + encoding_objs.push (moved_encoding); } int num_final_encodings = (int) encoding_objs.length - (int) removed_todo_idxes.get_population ();