From 5e816ef7aed2a82f729e0c2f444bf1cedb9e2670 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 15 Sep 2025 18:43:22 -0600 Subject: [PATCH] [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