From db86e546abc8076097aa1f8af25789c3e2d599ac Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 16:41:31 -0600 Subject: [PATCH 1/9] [instancer/gvar] Combine peak_coords mallocs into one chunk --- src/hb-ot-var-common.hh | 11 +++++++++-- src/hb-ot-var-gvar-table.hh | 11 ++++++++++- src/hb-vector.hh | 14 +++++--------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index b39be6593..e9fa38928 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -371,10 +371,17 @@ struct tuple_delta_t } bool compile_coords (const hb_map_t& axes_index_map, - const hb_map_t& axes_old_index_tag_map) + const hb_map_t& axes_old_index_tag_map, + hb_array_t *storage = nullptr) { unsigned cur_axis_count = axes_index_map.get_population (); - if (unlikely (!compiled_peak_coords.resize (cur_axis_count))) + if (storage) + { + assert (storage->length >= cur_axis_count); + compiled_peak_coords.set_storage (storage->sub_array (0, cur_axis_count)); + *storage += cur_axis_count; + } + else if (unlikely (!compiled_peak_coords.resize (cur_axis_count))) return false; hb_array_t start_coords, end_coords; diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index 46301a479..730ac7fa8 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -66,6 +66,7 @@ struct glyph_variations_t hb_vector_t glyph_variations; + hb_vector_t peak_coords_storage; hb_vector_t compiled_shared_tuples; private: unsigned shared_tuples_count = 0; @@ -174,12 +175,20 @@ struct glyph_variations_t * function will always deref pointers first */ hb_hashmap_t*, unsigned> coords_count_map; + unsigned tuple_vars_count = + hb_iter (glyph_variations) + | hb_map ([] (const tuple_variations_t& vars) { return vars.tuple_vars.length; }) + | hb_reduce (hb_add, 0) + ; + if (unlikely (!peak_coords_storage.resize (tuple_vars_count * axes_index_map.get_population()))) + return false; + hb_array_t peak_coords_array = peak_coords_storage.as_array (); + /* count the num of shared coords */ for (tuple_variations_t& vars: glyph_variations) { for (tuple_delta_t& var : vars.tuple_vars) { - if (!var.compile_coords (axes_index_map, axes_old_index_tag_map)) + if (!var.compile_coords (axes_index_map, axes_old_index_tag_map, std::addressof (peak_coords_array))) return false; unsigned *count; unsigned hash = hb_hash (&var.compiled_peak_coords); diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 34694e38b..590e7c05b 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -94,16 +94,13 @@ struct hb_vector_t } ~hb_vector_t () { fini (); } - template + template void set_storage (Type (&array)[n]) - { - set_storage (array, n); - } - + { set_storage (array, n); } + void + set_storage (hb_array_t array) + { set_storage (array.arrayZ, array.length); } template @@ -113,7 +110,6 @@ struct hb_vector_t if (unlikely (in_error ())) return; - assert (n > 0); assert (allocated == 0); assert (length == 0); From df18518f62ef378c8fd40bcd4c0d67c39ba9cb20 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 17:14:31 -0600 Subject: [PATCH 2/9] [iup] Don't sqrt --- src/hb-subset-instancer-iup.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hb-subset-instancer-iup.cc b/src/hb-subset-instancer-iup.cc index 074657aca..61f30194d 100644 --- a/src/hb-subset-instancer-iup.cc +++ b/src/hb-subset-instancer-iup.cc @@ -264,12 +264,13 @@ static bool _can_iup_in_between (const hb_array_t contour unsigned num = contour_points.length; + double tolerance_sq = tolerance * tolerance; for (unsigned i = 0; i < num; i++) { double dx = static_cast (x_deltas.arrayZ[i]) - interp_x_deltas.arrayZ[i]; double dy = static_cast (y_deltas.arrayZ[i]) - interp_y_deltas.arrayZ[i]; - - if (sqrt (dx * dx + dy * dy) > tolerance) + + if (dx * dx + dy * dy > tolerance_sq) return false; } return true; @@ -294,7 +295,7 @@ static bool _iup_contour_optimize_dp (const contour_point_vector_t& contour_poin for (unsigned i = 0; i < n; i++) { unsigned best_cost = (i == 0 ? 1 : costs.arrayZ[i-1] + 1); - + costs.arrayZ[i] = best_cost; chain.arrayZ[i] = (i == 0 ? -1 : i - 1); @@ -342,11 +343,12 @@ static bool _iup_contour_optimize (const hb_array_t conto return false; bool all_within_tolerance = true; + double tolerance_sq = tolerance * tolerance; for (unsigned i = 0; i < n; i++) { int dx = x_deltas.arrayZ[i]; int dy = y_deltas.arrayZ[i]; - if (sqrt ((double) dx * dx + (double) dy * dy) > tolerance) + if ((double) dx * dx + (double) dy * dy > tolerance_sq) { all_within_tolerance = false; break; From 5928a3c7385fe07471d18e745aa9f9e5280e0d15 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 17:17:15 -0600 Subject: [PATCH 3/9] [iup] Reduce vector allocs --- src/hb-subset-instancer-iup.cc | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/hb-subset-instancer-iup.cc b/src/hb-subset-instancer-iup.cc index 61f30194d..dfce115d9 100644 --- a/src/hb-subset-instancer-iup.cc +++ b/src/hb-subset-instancer-iup.cc @@ -254,9 +254,10 @@ static bool _can_iup_in_between (const hb_array_t contour const contour_point_t& p1, const contour_point_t& p2, int p1_dx, int p2_dx, int p1_dy, int p2_dy, - double tolerance) + double tolerance, + hb_vector_t &interp_x_deltas, /* scratch */ + hb_vector_t &interp_y_deltas /* scratch */) { - hb_vector_t interp_x_deltas, interp_y_deltas; if (!_iup_segment (contour_points, x_deltas, y_deltas, p1, p2, p1_dx, p2_dx, p1_dy, p2_dy, interp_x_deltas, interp_y_deltas)) @@ -283,7 +284,9 @@ static bool _iup_contour_optimize_dp (const contour_point_vector_t& contour_poin double tolerance, unsigned lookback, hb_vector_t& costs, /* OUT */ - hb_vector_t& chain /* OUT */) + hb_vector_t& chain, /* OUT */ + hb_vector_t &interp_x_deltas_scratch, + hb_vector_t &interp_y_deltas_scratch) { unsigned n = contour_points.length; if (unlikely (!costs.resize (n, false) || @@ -316,7 +319,8 @@ static bool _iup_contour_optimize_dp (const contour_point_vector_t& contour_poin contour_points.arrayZ[p1], contour_points.arrayZ[i], x_deltas.arrayZ[p1], x_deltas.arrayZ[i], y_deltas.arrayZ[p1], y_deltas.arrayZ[i], - tolerance)) + tolerance, + interp_x_deltas_scratch, interp_y_deltas_scratch)) { best_cost = cost; costs.arrayZ[i] = best_cost; @@ -334,7 +338,9 @@ static bool _iup_contour_optimize (const hb_array_t conto const hb_array_t x_deltas, const hb_array_t y_deltas, hb_array_t opt_indices, /* OUT */ - double tolerance = 0.0) + double tolerance, + hb_vector_t &interp_x_deltas_scratch, + hb_vector_t &interp_y_deltas_scratch) { unsigned n = contour_points.length; if (opt_indices.length != n || @@ -407,7 +413,8 @@ static bool _iup_contour_optimize (const hb_array_t conto if (!_iup_contour_optimize_dp (rot_points, rot_x_deltas, rot_y_deltas, rot_forced_set, tolerance, n, - costs, chain)) + costs, chain, + interp_x_deltas_scratch, interp_y_deltas_scratch)) return false; hb_set_t solution; @@ -459,7 +466,8 @@ static bool _iup_contour_optimize (const hb_array_t conto hb_vector_t chain; if (!_iup_contour_optimize_dp (repeat_points, repeat_x_deltas, repeat_y_deltas, forced_set, tolerance, n, - costs, chain)) + costs, chain, + interp_x_deltas_scratch, interp_y_deltas_scratch)) return false; unsigned best_cost = n + 1; @@ -518,6 +526,8 @@ bool iup_delta_optimize (const contour_point_vector_t& contour_points, if (end_points.in_error ()) return false; + hb_vector_t interp_x_deltas_scratch, interp_y_deltas_scratch; + unsigned start = 0; for (unsigned end : end_points) { @@ -526,7 +536,8 @@ bool iup_delta_optimize (const contour_point_vector_t& contour_points, x_deltas.as_array ().sub_array (start, len), y_deltas.as_array ().sub_array (start, len), opt_indices.as_array ().sub_array (start, len), - tolerance)) + tolerance, + interp_x_deltas_scratch, interp_y_deltas_scratch)) return false; start = end + 1; } From a64ab6c6e567aae27a59174f468ef3e277dca78f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 17:47:07 -0600 Subject: [PATCH 4/9] [iup] Reduce hb_set_t allocs --- src/hb-subset-instancer-iup.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hb-subset-instancer-iup.cc b/src/hb-subset-instancer-iup.cc index dfce115d9..eb1dbead9 100644 --- a/src/hb-subset-instancer-iup.cc +++ b/src/hb-subset-instancer-iup.cc @@ -473,9 +473,10 @@ static bool _iup_contour_optimize (const hb_array_t conto unsigned best_cost = n + 1; int len = costs.length; hb_set_t best_sol; + hb_set_t solution; for (int start = n - 1; start < len; start++) { - hb_set_t solution; + solution.clear (); int i = start; int lookback = start - (int) n; while (i > lookback) From 5e816ef7aed2a82f729e0c2f444bf1cedb9e2670 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 18:43:22 -0600 Subject: [PATCH 5/9] [iup] Reuse vectors --- src/hb-ot-var-common.hh | 75 +++++++++++++++++++++------------- src/hb-subset-instancer-iup.cc | 10 ++--- src/hb-subset-instancer-iup.hh | 9 ++++ src/hb-vector.hh | 3 +- 4 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index e9fa38928..f53d81450 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -222,6 +222,21 @@ struct TupleVariationHeader DEFINE_SIZE_MIN (4); }; +struct optimize_scratch_t +{ + iup_scratch_t iup; + hb_vector_t opt_indices; + hb_vector_t rounded_x_deltas; + hb_vector_t rounded_y_deltas; + hb_vector_t opt_deltas_x; + hb_vector_t opt_deltas_y; + hb_vector_t opt_point_data; + hb_vector_t opt_deltas_data; + hb_vector_t point_data; + hb_vector_t deltas_data; + hb_vector_t rounded_deltas; +}; + struct tuple_delta_t { static constexpr bool realloc_move = true; // Watch out when adding new members! @@ -512,24 +527,25 @@ struct tuple_delta_t return compiled_interm_coords.length; } - bool compile_deltas () - { return compile_deltas (indices, deltas_x, deltas_y, compiled_deltas); } + bool compile_deltas (hb_vector_t &rounded_deltas_scratch) + { return compile_deltas (indices, deltas_x, deltas_y, compiled_deltas, rounded_deltas_scratch); } static bool compile_deltas (hb_array_t point_indices, hb_array_t x_deltas, hb_array_t y_deltas, - hb_vector_t &compiled_deltas /* OUT */) + hb_vector_t &compiled_deltas, /* OUT */ + hb_vector_t &rounded_deltas /* scratch */) { - hb_vector_t rounded_deltas; - if (unlikely (!rounded_deltas.alloc (point_indices.length))) + if (unlikely (!rounded_deltas.resize (point_indices.length, false))) return false; + unsigned j = 0; for (unsigned i = 0; i < point_indices.length; i++) { if (!point_indices[i]) continue; - int rounded_delta = (int) roundf (x_deltas.arrayZ[i]); - rounded_deltas.push (rounded_delta); + rounded_deltas.arrayZ[j++] = (int) roundf (x_deltas.arrayZ[i]); } + rounded_deltas.resize (j); if (!rounded_deltas) return true; /* allocate enough memories 5 * num_deltas */ @@ -576,8 +592,7 @@ struct tuple_delta_t unsigned ref_count = 0; - hb_vector_t &end_points = scratch; - end_points.reset (); + hb_vector_t &end_points = scratch.reset (); for (unsigned i = 0; i < point_count; i++) { @@ -665,6 +680,7 @@ struct tuple_delta_t bool optimize (const contour_point_vector_t& contour_points, bool is_composite, + optimize_scratch_t &scratch, double tolerance = 0.5 + 1e-10) { unsigned count = contour_points.length; @@ -672,22 +688,21 @@ struct tuple_delta_t deltas_y.length != count) return false; - hb_vector_t opt_indices; - hb_vector_t rounded_x_deltas, rounded_y_deltas; + hb_vector_t &opt_indices = scratch.opt_indices.reset (); + hb_vector_t &rounded_x_deltas = scratch.rounded_x_deltas; + hb_vector_t &rounded_y_deltas = scratch.rounded_y_deltas; - if (unlikely (!rounded_x_deltas.alloc (count) || - !rounded_y_deltas.alloc (count))) + if (unlikely (!rounded_x_deltas.resize (count, false) || + !rounded_y_deltas.resize (count, false))) return false; for (unsigned i = 0; i < count; i++) { - int rounded_x_delta = (int) roundf (deltas_x.arrayZ[i]); - int rounded_y_delta = (int) roundf (deltas_y.arrayZ[i]); - rounded_x_deltas.push (rounded_x_delta); - rounded_y_deltas.push (rounded_y_delta); + rounded_x_deltas.arrayZ[i] = (int) roundf (deltas_x.arrayZ[i]); + rounded_y_deltas.arrayZ[i] = (int) roundf (deltas_y.arrayZ[i]); } - if (!iup_delta_optimize (contour_points, rounded_x_deltas, rounded_y_deltas, opt_indices, tolerance)) + if (!iup_delta_optimize (contour_points, rounded_x_deltas, rounded_y_deltas, opt_indices, scratch.iup, tolerance)) return false; unsigned ref_count = 0; @@ -696,7 +711,8 @@ struct tuple_delta_t if (ref_count == count) return true; - hb_vector_t opt_deltas_x, opt_deltas_y; + hb_vector_t &opt_deltas_x = scratch.opt_deltas_x.reset (); + hb_vector_t &opt_deltas_y = scratch.opt_deltas_y.reset (); bool is_comp_glyph_wo_deltas = (is_composite && ref_count == 0); if (is_comp_glyph_wo_deltas) { @@ -709,21 +725,22 @@ struct tuple_delta_t opt_indices.arrayZ[i] = false; } - hb_vector_t opt_point_data; + hb_vector_t &opt_point_data = scratch.opt_point_data.reset (); if (!compile_point_set (opt_indices, opt_point_data)) return false; - hb_vector_t opt_deltas_data; + hb_vector_t &opt_deltas_data = scratch.opt_deltas_data.reset (); if (!compile_deltas (opt_indices, is_comp_glyph_wo_deltas ? opt_deltas_x : deltas_x, is_comp_glyph_wo_deltas ? opt_deltas_y : deltas_y, - opt_deltas_data)) + opt_deltas_data, + scratch.rounded_deltas)) return false; - hb_vector_t point_data; + hb_vector_t &point_data = scratch.point_data.reset (); if (!compile_point_set (indices, point_data)) return false; - hb_vector_t deltas_data; - if (!compile_deltas (indices, deltas_x, deltas_y, deltas_data)) + hb_vector_t &deltas_data = scratch.deltas_data.reset (); + if (!compile_deltas (indices, deltas_x, deltas_y, deltas_data, scratch.rounded_deltas)) return false; if (opt_point_data.length + opt_deltas_data.length < point_data.length + deltas_data.length) @@ -1201,9 +1218,10 @@ struct TupleVariationData bool iup_optimize (const contour_point_vector_t& contour_points) { + optimize_scratch_t scratch; for (tuple_delta_t& var : tuple_vars) { - if (!var.optimize (contour_points, is_composite)) + if (!var.optimize (contour_points, is_composite, scratch)) return false; } return true; @@ -1261,6 +1279,7 @@ struct TupleVariationData if (shared_points_bytes) compiled_byte_size += shared_points_bytes->length; } + hb_vector_t rounded_deltas_scratch; // compile delta and tuple var header for each tuple variation for (auto& tuple: tuple_vars) { @@ -1274,7 +1293,7 @@ struct TupleVariationData * this tuple */ if (!points_data->length) continue; - if (!tuple.compile_deltas ()) + if (!tuple.compile_deltas (rounded_deltas_scratch)) return false; unsigned points_data_length = (points_data != shared_points_bytes) ? points_data->length : 0; @@ -1826,7 +1845,7 @@ struct item_variations_t } } - major_rows.clear (); + major_rows.reset (); for (unsigned minor = 0; minor < num_rows; minor++) { const hb_vector_t& row = delta_rows[start_row + minor]; diff --git a/src/hb-subset-instancer-iup.cc b/src/hb-subset-instancer-iup.cc index eb1dbead9..a01d260b3 100644 --- a/src/hb-subset-instancer-iup.cc +++ b/src/hb-subset-instancer-iup.cc @@ -476,7 +476,7 @@ static bool _iup_contour_optimize (const hb_array_t conto hb_set_t solution; for (int start = n - 1; start < len; start++) { - solution.clear (); + solution.reset (); int i = start; int lookback = start - (int) n; while (i > lookback) @@ -507,12 +507,14 @@ bool iup_delta_optimize (const contour_point_vector_t& contour_points, const hb_vector_t& x_deltas, const hb_vector_t& y_deltas, hb_vector_t& opt_indices, /* OUT */ + iup_scratch_t &scratch, double tolerance) { if (!opt_indices.resize (contour_points.length)) return false; - hb_vector_t end_points; + hb_vector_t &end_points = scratch.end_points.reset (); + unsigned count = contour_points.length; if (unlikely (!end_points.alloc (count))) return false; @@ -527,8 +529,6 @@ bool iup_delta_optimize (const contour_point_vector_t& contour_points, if (end_points.in_error ()) return false; - hb_vector_t interp_x_deltas_scratch, interp_y_deltas_scratch; - unsigned start = 0; for (unsigned end : end_points) { @@ -538,7 +538,7 @@ bool iup_delta_optimize (const contour_point_vector_t& contour_points, y_deltas.as_array ().sub_array (start, len), opt_indices.as_array ().sub_array (start, len), tolerance, - interp_x_deltas_scratch, interp_y_deltas_scratch)) + scratch.interp_x_deltas, scratch.interp_y_deltas)) return false; start = end + 1; } diff --git a/src/hb-subset-instancer-iup.hh b/src/hb-subset-instancer-iup.hh index 01987bd25..5bfc4275d 100644 --- a/src/hb-subset-instancer-iup.hh +++ b/src/hb-subset-instancer-iup.hh @@ -26,12 +26,21 @@ #define HB_SUBSET_INSTANCER_IUP_HH #include "hb-subset-plan.hh" + +struct iup_scratch_t +{ + hb_vector_t end_points; + hb_vector_t interp_x_deltas; + hb_vector_t interp_y_deltas; +}; + /* given contour points and deltas, optimize a set of referenced points within error * tolerance. Returns optimized referenced point indices */ HB_INTERNAL bool iup_delta_optimize (const contour_point_vector_t& contour_points, const hb_vector_t& x_deltas, const hb_vector_t& y_deltas, hb_vector_t& opt_indices, /* OUT */ + iup_scratch_t &scratch, double tolerance = 0.0); #endif /* HB_SUBSET_INSTANCER_IUP_HH */ diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 590e7c05b..9a6b29b80 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -181,11 +181,12 @@ struct hb_vector_t } HB_ALWAYS_INLINE_VECTOR_ALLOCS - void reset () + hb_vector_t &reset () { if (unlikely (in_error ())) reset_error (); resize (0); + return *this; } friend void swap (hb_vector_t& a, hb_vector_t& b) noexcept From 045bd691036d92faeb7c067668598c634a8bde01 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 18:56:55 -0600 Subject: [PATCH 6/9] [iup] Use bit-set instead of set --- src/hb-subset-instancer-iup.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/hb-subset-instancer-iup.cc b/src/hb-subset-instancer-iup.cc index a01d260b3..0fe79e157 100644 --- a/src/hb-subset-instancer-iup.cc +++ b/src/hb-subset-instancer-iup.cc @@ -37,7 +37,7 @@ constexpr static unsigned MAX_LOOKBACK = 8; static void _iup_contour_bound_forced_set (const hb_array_t contour_points, const hb_array_t x_deltas, const hb_array_t y_deltas, - hb_set_t& forced_set, /* OUT */ + hb_bit_set_t& forced_set, /* OUT */ double tolerance = 0.0) { unsigned len = contour_points.length; @@ -150,10 +150,10 @@ static bool rotate_array (const hb_array_t& org_array, return true; } -static bool rotate_set (const hb_set_t& org_set, +static bool rotate_set (const hb_bit_set_t& org_set, int k, unsigned n, - hb_set_t& out) + hb_bit_set_t& out) { if (!n) return false; k %= n; @@ -280,7 +280,7 @@ static bool _can_iup_in_between (const hb_array_t contour static bool _iup_contour_optimize_dp (const contour_point_vector_t& contour_points, const hb_vector_t& x_deltas, const hb_vector_t& y_deltas, - const hb_set_t& forced_set, + const hb_bit_set_t& forced_set, double tolerance, unsigned lookback, hb_vector_t& costs, /* OUT */ @@ -390,7 +390,7 @@ static bool _iup_contour_optimize (const hb_array_t conto } /* else, solve the general problem using Dynamic Programming */ - hb_set_t forced_set; + hb_bit_set_t forced_set; _iup_contour_bound_forced_set (contour_points, x_deltas, y_deltas, forced_set, tolerance); if (!forced_set.is_empty ()) @@ -401,7 +401,7 @@ static bool _iup_contour_optimize (const hb_array_t conto hb_vector_t rot_x_deltas, rot_y_deltas; contour_point_vector_t rot_points; - hb_set_t rot_forced_set; + hb_bit_set_t rot_forced_set; if (!rotate_array (contour_points, k, rot_points) || !rotate_array (x_deltas, k, rot_x_deltas) || !rotate_array (y_deltas, k, rot_y_deltas) || @@ -417,7 +417,7 @@ static bool _iup_contour_optimize (const hb_array_t conto interp_x_deltas_scratch, interp_y_deltas_scratch)) return false; - hb_set_t solution; + hb_bit_set_t solution; int index = n - 1; while (index != -1) { @@ -472,8 +472,8 @@ static bool _iup_contour_optimize (const hb_array_t conto unsigned best_cost = n + 1; int len = costs.length; - hb_set_t best_sol; - hb_set_t solution; + hb_bit_set_t best_sol; + hb_bit_set_t solution; for (int start = n - 1; start < len; start++) { solution.reset (); From 54ac33cce1bf256a219802df32327bc20d8932f4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 19:00:34 -0600 Subject: [PATCH 7/9] [iup] Reuse more mallocs --- src/hb-subset-instancer-iup.cc | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/hb-subset-instancer-iup.cc b/src/hb-subset-instancer-iup.cc index 0fe79e157..e5354da5d 100644 --- a/src/hb-subset-instancer-iup.cc +++ b/src/hb-subset-instancer-iup.cc @@ -339,8 +339,7 @@ static bool _iup_contour_optimize (const hb_array_t conto const hb_array_t y_deltas, hb_array_t opt_indices, /* OUT */ double tolerance, - hb_vector_t &interp_x_deltas_scratch, - hb_vector_t &interp_y_deltas_scratch) + iup_scratch_t &scratch) { unsigned n = contour_points.length; if (opt_indices.length != n || @@ -393,6 +392,9 @@ static bool _iup_contour_optimize (const hb_array_t conto hb_bit_set_t forced_set; _iup_contour_bound_forced_set (contour_points, x_deltas, y_deltas, forced_set, tolerance); + hb_vector_t &costs = scratch.costs.reset (); + hb_vector_t &chain = scratch.chain.reset (); + if (!forced_set.is_empty ()) { int k = n - 1 - forced_set.get_max (); @@ -408,13 +410,10 @@ static bool _iup_contour_optimize (const hb_array_t conto !rotate_set (forced_set, k, n, rot_forced_set)) return false; - hb_vector_t costs; - hb_vector_t chain; - if (!_iup_contour_optimize_dp (rot_points, rot_x_deltas, rot_y_deltas, rot_forced_set, tolerance, n, costs, chain, - interp_x_deltas_scratch, interp_y_deltas_scratch)) + scratch.interp_x_deltas, scratch.interp_y_deltas)) return false; hb_bit_set_t solution; @@ -462,12 +461,10 @@ static bool _iup_contour_optimize (const hb_array_t conto hb_memcpy ((void *) (repeat_points.arrayZ + n), (const void *) contour_points.arrayZ, n * contour_point_size); } - hb_vector_t costs; - hb_vector_t chain; if (!_iup_contour_optimize_dp (repeat_points, repeat_x_deltas, repeat_y_deltas, forced_set, tolerance, n, costs, chain, - interp_x_deltas_scratch, interp_y_deltas_scratch)) + scratch.interp_x_deltas, scratch.interp_y_deltas)) return false; unsigned best_cost = n + 1; @@ -538,7 +535,7 @@ bool iup_delta_optimize (const contour_point_vector_t& contour_points, y_deltas.as_array ().sub_array (start, len), opt_indices.as_array ().sub_array (start, len), tolerance, - scratch.interp_x_deltas, scratch.interp_y_deltas)) + scratch)) return false; start = end + 1; } From e42714cfff7f1f98a2ad6c7b95ccf484a2232d77 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 19:05:38 -0600 Subject: [PATCH 8/9] [iup] Reuse bit-set allocs --- src/hb-bit-set.hh | 3 ++- src/hb-map.hh | 3 ++- src/hb-subset-instancer-iup.cc | 10 +++++----- src/hb-subset-instancer-iup.hh | 6 ++++++ 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/hb-bit-set.hh b/src/hb-bit-set.hh index ec462c7d4..9134f7ba6 100644 --- a/src/hb-bit-set.hh +++ b/src/hb-bit-set.hh @@ -108,10 +108,11 @@ struct hb_bit_set_t page_map.alloc (sz); } - void reset () + hb_bit_set_t& reset () { successful = true; clear (); + return *this; } void clear () diff --git a/src/hb-map.hh b/src/hb-map.hh index efcd6716c..e4fc74077 100644 --- a/src/hb-map.hh +++ b/src/hb-map.hh @@ -187,10 +187,11 @@ struct hb_hashmap_t population = occupancy = 0; } - void reset () + hb_hashmap_t& reset () { successful = true; clear (); + return *this; } bool in_error () const { return !successful; } diff --git a/src/hb-subset-instancer-iup.cc b/src/hb-subset-instancer-iup.cc index e5354da5d..17fbd1672 100644 --- a/src/hb-subset-instancer-iup.cc +++ b/src/hb-subset-instancer-iup.cc @@ -389,7 +389,7 @@ static bool _iup_contour_optimize (const hb_array_t conto } /* else, solve the general problem using Dynamic Programming */ - hb_bit_set_t forced_set; + hb_bit_set_t &forced_set = scratch.forced_set.reset (); _iup_contour_bound_forced_set (contour_points, x_deltas, y_deltas, forced_set, tolerance); hb_vector_t &costs = scratch.costs.reset (); @@ -403,7 +403,7 @@ static bool _iup_contour_optimize (const hb_array_t conto hb_vector_t rot_x_deltas, rot_y_deltas; contour_point_vector_t rot_points; - hb_bit_set_t rot_forced_set; + hb_bit_set_t &rot_forced_set = scratch.rot_forced_set.reset (); if (!rotate_array (contour_points, k, rot_points) || !rotate_array (x_deltas, k, rot_x_deltas) || !rotate_array (y_deltas, k, rot_y_deltas) || @@ -416,7 +416,7 @@ static bool _iup_contour_optimize (const hb_array_t conto scratch.interp_x_deltas, scratch.interp_y_deltas)) return false; - hb_bit_set_t solution; + hb_bit_set_t &solution = scratch.solution.reset (); int index = n - 1; while (index != -1) { @@ -469,8 +469,8 @@ static bool _iup_contour_optimize (const hb_array_t conto unsigned best_cost = n + 1; int len = costs.length; - hb_bit_set_t best_sol; - hb_bit_set_t solution; + hb_bit_set_t &solution = scratch.solution.reset (); + hb_bit_set_t &best_sol = scratch.best_sol.reset (); for (int start = n - 1; start < len; start++) { solution.reset (); diff --git a/src/hb-subset-instancer-iup.hh b/src/hb-subset-instancer-iup.hh index 5bfc4275d..c2aaab68e 100644 --- a/src/hb-subset-instancer-iup.hh +++ b/src/hb-subset-instancer-iup.hh @@ -32,6 +32,12 @@ struct iup_scratch_t hb_vector_t end_points; hb_vector_t interp_x_deltas; hb_vector_t interp_y_deltas; + hb_vector_t costs; + hb_vector_t chain; + hb_bit_set_t forced_set; + hb_bit_set_t rot_forced_set; + hb_bit_set_t solution; + hb_bit_set_t best_sol; }; /* given contour points and deltas, optimize a set of referenced points within error From 82ca73a2c34dc416d90cc3735d43877eafa0bb35 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 19:12:40 -0600 Subject: [PATCH 9/9] [iup] Reuse more vector allocs --- src/hb-subset-instancer-iup.cc | 8 +++++--- src/hb-subset-instancer-iup.hh | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/hb-subset-instancer-iup.cc b/src/hb-subset-instancer-iup.cc index 17fbd1672..393cfedf8 100644 --- a/src/hb-subset-instancer-iup.cc +++ b/src/hb-subset-instancer-iup.cc @@ -401,8 +401,10 @@ static bool _iup_contour_optimize (const hb_array_t conto if (k < 0) return false; - hb_vector_t rot_x_deltas, rot_y_deltas; - contour_point_vector_t rot_points; + hb_vector_t &rot_x_deltas = scratch.rot_x_deltas.reset (); + hb_vector_t &rot_y_deltas = scratch.rot_y_deltas.reset (); + contour_point_vector_t &rot_points = scratch.rot_points; + rot_points.reset (); hb_bit_set_t &rot_forced_set = scratch.rot_forced_set.reset (); if (!rotate_array (contour_points, k, rot_points) || !rotate_array (x_deltas, k, rot_x_deltas) || @@ -431,7 +433,7 @@ static bool _iup_contour_optimize (const hb_array_t conto for (unsigned i : solution) opt_indices.arrayZ[i] = true; - hb_vector_t rot_indices; + hb_vector_t &rot_indices = scratch.rot_indices.reset (); const hb_array_t opt_indices_array (opt_indices.arrayZ, opt_indices.length); rotate_array (opt_indices_array, -k, rot_indices); diff --git a/src/hb-subset-instancer-iup.hh b/src/hb-subset-instancer-iup.hh index c2aaab68e..981c88d24 100644 --- a/src/hb-subset-instancer-iup.hh +++ b/src/hb-subset-instancer-iup.hh @@ -34,6 +34,10 @@ struct iup_scratch_t hb_vector_t interp_y_deltas; hb_vector_t costs; hb_vector_t chain; + hb_vector_t rot_indices; + hb_vector_t rot_x_deltas; + hb_vector_t rot_y_deltas; + contour_point_vector_t rot_points; hb_bit_set_t forced_set; hb_bit_set_t rot_forced_set; hb_bit_set_t solution;