From f2c4ab4545227513b2cbeb97510169597c5be770 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 13 Sep 2025 16:14:55 -0600 Subject: [PATCH 1/3] [instancer/varstore] Micro-optimize --- src/hb-ot-var-common.hh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index bd9c8941d..ef4f782c1 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1822,8 +1822,7 @@ struct item_variations_t { int rounded_delta = roundf (tuple.deltas_x[i]); delta_rows[start_row + i][*col_idx] += rounded_delta; - if ((!has_long) && (rounded_delta < -65536 || rounded_delta > 65535)) - has_long = true; + has_long |= rounded_delta < -65536 || rounded_delta > 65535; } } From 2319d763aa97b92fd451de67c0a96abffe3b2b9d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 13 Sep 2025 16:21:34 -0600 Subject: [PATCH 2/3] [TupleValues] Micro-optimize encoding Also, a check for 32bit values in the word-encoding function was missing. Ouch. --- src/hb-open-type.hh | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index 08b0fe86c..358ba3e7b 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -1509,9 +1509,9 @@ struct TupleValues int val = values.arrayZ[i]; if (val == 0) encoded_len += encode_value_run_as_zeroes (i, encoded_bytes.sub_array (encoded_len), values); - else if (val >= -128 && val <= 127) + else if ((int8_t) val == val) encoded_len += encode_value_run_as_bytes (i, encoded_bytes.sub_array (encoded_len), values); - else if (val >= -32768 && val <= 32767) + else if ((int16_t) val == val) encoded_len += encode_value_run_as_words (i, encoded_bytes.sub_array (encoded_len), values); else encoded_len += encode_value_run_as_longs (i, encoded_bytes.sub_array (encoded_len), values); @@ -1557,7 +1557,7 @@ struct TupleValues while (i < num_values) { int val = values.arrayZ[i]; - if (val > 127 || val < -128) + if ((int8_t) val != val) break; /* from fonttools: if there're 2 or more zeros in a sequence, @@ -1612,15 +1612,18 @@ struct TupleValues { int val = values.arrayZ[i]; - /* start a new run for a single zero value*/ + if ((int16_t) val != val) + break; + + /* start a new run for a single zero value. */ if (val == 0) break; - /* from fonttools: continue word-encoded run if there's only one + /* From fonttools: continue word-encoded run if there's only one * single value in the range [-128, 127] because it is more compact. * Only start a new run when there're 2 continuous such values. */ - if (val >= -128 && val <= 127 && + if ((int8_t) val == val && i + 1 < num_values && - values.arrayZ[i+1] >= -128 && values.arrayZ[i+1] <= 127) + (int8_t) values.arrayZ[i+1] == values.arrayZ[i+1]) break; i++; @@ -1673,7 +1676,7 @@ struct TupleValues { int val = values.arrayZ[i]; - if (val >= -32768 && val <= 32767) + if ((int16_t) val == val) break; i++; From 1f936e236615cbd284603ea4ea3e13a366edd3ee Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 13 Sep 2025 16:58:45 -0600 Subject: [PATCH 3/3] [instancer] Try to move instead of copy some tuples --- src/hb-ot-var-common.hh | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index ef4f782c1..f2e1e31f2 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -247,6 +247,7 @@ struct tuple_delta_t tuple_delta_t () = default; tuple_delta_t (const tuple_delta_t& o) = default; + tuple_delta_t& operator = (const tuple_delta_t& o) = default; friend void swap (tuple_delta_t& a, tuple_delta_t& b) noexcept { @@ -325,13 +326,15 @@ struct tuple_delta_t void change_tuple_var_axis_limit (hb_tag_t axis_tag, Triple axis_limit, TripleDistances axis_triple_distances, hb_vector_t& out, - rebase_tent_result_scratch_t &scratch) const + rebase_tent_result_scratch_t &scratch) { + // May move *this out. + out.reset (); Triple *tent; if (!axis_tuples.has (axis_tag, &tent)) { - out.push (*this); + out.push (std::move (*this)); return; } @@ -341,15 +344,22 @@ struct tuple_delta_t if (tent->middle == 0.0) { - out.push (*this); + out.push (std::move (*this)); return; } rebase_tent_result_t &solutions = scratch.first; rebase_tent (*tent, axis_limit, axis_triple_distances, solutions, scratch.second); - for (auto &t : solutions) + for (unsigned i = 0; i < solutions.length; i++) { - tuple_delta_t new_var = *this; + auto &t = solutions.arrayZ[i]; + + tuple_delta_t new_var; + if (i < solutions.length - 1) + new_var = *this; + else + new_var = std::move (*this); + if (t.second == Triple ()) new_var.remove_axis (axis_tag); else @@ -1070,8 +1080,9 @@ struct TupleVariationData axis_triple_distances = axes_triple_distances.get (axis_tag); hb_vector_t new_vars; - for (const tuple_delta_t& var : tuple_vars) + for (tuple_delta_t& var : tuple_vars) { + // This may move var out. var.change_tuple_var_axis_limit (axis_tag, *axis_limit, axis_triple_distances, out, scratch); if (!out) continue;