Merge pull request #5563 from harfbuzz/instancer-gvar-consolidate-mallocs

Instancer gvar consolidate mallocs
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2025-09-15 19:30:46 -06:00
commit bac2a67cc9
7 files changed
+139 -73

No files matched your search

+2 -1
View File
@@ -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 ()
+2 -1
View File
@@ -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; }
+56 -30
View File
@@ -222,6 +222,21 @@ struct TupleVariationHeader
DEFINE_SIZE_MIN (4);
};
struct optimize_scratch_t
{
iup_scratch_t iup;
hb_vector_t<bool> opt_indices;
hb_vector_t<int> rounded_x_deltas;
hb_vector_t<int> rounded_y_deltas;
hb_vector_t<float> opt_deltas_x;
hb_vector_t<float> opt_deltas_y;
hb_vector_t<unsigned char> opt_point_data;
hb_vector_t<unsigned char> opt_deltas_data;
hb_vector_t<unsigned char> point_data;
hb_vector_t<unsigned char> deltas_data;
hb_vector_t<int> rounded_deltas;
};
struct tuple_delta_t
{
static constexpr bool realloc_move = true; // Watch out when adding new members!
@@ -371,10 +386,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<F2DOT14> *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<F2DOT14> start_coords, end_coords;
@@ -505,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<int> &rounded_deltas_scratch)
{ return compile_deltas (indices, deltas_x, deltas_y, compiled_deltas, rounded_deltas_scratch); }
static bool compile_deltas (hb_array_t<const bool> point_indices,
hb_array_t<const float> x_deltas,
hb_array_t<const float> y_deltas,
hb_vector_t<unsigned char> &compiled_deltas /* OUT */)
hb_vector_t<unsigned char> &compiled_deltas, /* OUT */
hb_vector_t<int> &rounded_deltas /* scratch */)
{
hb_vector_t<int> 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 */
@@ -569,8 +592,7 @@ struct tuple_delta_t
unsigned ref_count = 0;
hb_vector_t<unsigned> &end_points = scratch;
end_points.reset ();
hb_vector_t<unsigned> &end_points = scratch.reset ();
for (unsigned i = 0; i < point_count; i++)
{
@@ -658,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;
@@ -665,22 +688,21 @@ struct tuple_delta_t
deltas_y.length != count)
return false;
hb_vector_t<bool> opt_indices;
hb_vector_t<int> rounded_x_deltas, rounded_y_deltas;
hb_vector_t<bool> &opt_indices = scratch.opt_indices.reset ();
hb_vector_t<int> &rounded_x_deltas = scratch.rounded_x_deltas;
hb_vector_t<int> &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;
@@ -689,7 +711,8 @@ struct tuple_delta_t
if (ref_count == count) return true;
hb_vector_t<float> opt_deltas_x, opt_deltas_y;
hb_vector_t<float> &opt_deltas_x = scratch.opt_deltas_x.reset ();
hb_vector_t<float> &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)
{
@@ -702,21 +725,22 @@ struct tuple_delta_t
opt_indices.arrayZ[i] = false;
}
hb_vector_t<unsigned char> opt_point_data;
hb_vector_t<unsigned char> &opt_point_data = scratch.opt_point_data.reset ();
if (!compile_point_set (opt_indices, opt_point_data))
return false;
hb_vector_t<unsigned char> opt_deltas_data;
hb_vector_t<unsigned char> &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<unsigned char> point_data;
hb_vector_t<unsigned char> &point_data = scratch.point_data.reset ();
if (!compile_point_set (indices, point_data))
return false;
hb_vector_t<unsigned char> deltas_data;
if (!compile_deltas (indices, deltas_x, deltas_y, deltas_data))
hb_vector_t<unsigned char> &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)
@@ -1194,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;
@@ -1254,6 +1279,7 @@ struct TupleVariationData
if (shared_points_bytes)
compiled_byte_size += shared_points_bytes->length;
}
hb_vector_t<int> rounded_deltas_scratch;
// compile delta and tuple var header for each tuple variation
for (auto& tuple: tuple_vars)
{
@@ -1267,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;
@@ -1819,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<int>& row = delta_rows[start_row + minor];
+10 -1
View File
@@ -66,6 +66,7 @@ struct glyph_variations_t
hb_vector_t<tuple_variations_t> glyph_variations;
hb_vector_t<F2DOT14> peak_coords_storage;
hb_vector_t<F2DOT14> 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<const hb_vector_t<F2DOT14>*, 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<F2DOT14> 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);
+43 -30
View File
@@ -37,7 +37,7 @@ constexpr static unsigned MAX_LOOKBACK = 8;
static void _iup_contour_bound_forced_set (const hb_array_t<const contour_point_t> contour_points,
const hb_array_t<const int> x_deltas,
const hb_array_t<const int> 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<const 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;
@@ -254,9 +254,10 @@ static bool _can_iup_in_between (const hb_array_t<const contour_point_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<double> &interp_x_deltas, /* scratch */
hb_vector_t<double> &interp_y_deltas /* scratch */)
{
hb_vector_t<double> 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))
@@ -264,12 +265,13 @@ static bool _can_iup_in_between (const hb_array_t<const contour_point_t> contour
unsigned num = contour_points.length;
double tolerance_sq = tolerance * tolerance;
for (unsigned i = 0; i < num; i++)
{
double dx = static_cast<double> (x_deltas.arrayZ[i]) - interp_x_deltas.arrayZ[i];
double dy = static_cast<double> (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;
@@ -278,11 +280,13 @@ static bool _can_iup_in_between (const hb_array_t<const contour_point_t> contour
static bool _iup_contour_optimize_dp (const contour_point_vector_t& contour_points,
const hb_vector_t<int>& x_deltas,
const hb_vector_t<int>& y_deltas,
const hb_set_t& forced_set,
const hb_bit_set_t& forced_set,
double tolerance,
unsigned lookback,
hb_vector_t<unsigned>& costs, /* OUT */
hb_vector_t<int>& chain /* OUT */)
hb_vector_t<int>& chain, /* OUT */
hb_vector_t<double> &interp_x_deltas_scratch,
hb_vector_t<double> &interp_y_deltas_scratch)
{
unsigned n = contour_points.length;
if (unlikely (!costs.resize (n, false) ||
@@ -294,7 +298,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);
@@ -315,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;
@@ -333,7 +338,8 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
const hb_array_t<const int> x_deltas,
const hb_array_t<const int> y_deltas,
hb_array_t<bool> opt_indices, /* OUT */
double tolerance = 0.0)
double tolerance,
iup_scratch_t &scratch)
{
unsigned n = contour_points.length;
if (opt_indices.length != n ||
@@ -342,11 +348,12 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_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;
@@ -382,33 +389,36 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
}
/* else, solve the general problem using Dynamic Programming */
hb_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<unsigned> &costs = scratch.costs.reset ();
hb_vector_t<int> &chain = scratch.chain.reset ();
if (!forced_set.is_empty ())
{
int k = n - 1 - forced_set.get_max ();
if (k < 0)
return false;
hb_vector_t<int> rot_x_deltas, rot_y_deltas;
contour_point_vector_t rot_points;
hb_set_t rot_forced_set;
hb_vector_t<int> &rot_x_deltas = scratch.rot_x_deltas.reset ();
hb_vector_t<int> &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) ||
!rotate_array (y_deltas, k, rot_y_deltas) ||
!rotate_set (forced_set, k, n, rot_forced_set))
return false;
hb_vector_t<unsigned> costs;
hb_vector_t<int> chain;
if (!_iup_contour_optimize_dp (rot_points, rot_x_deltas, rot_y_deltas,
rot_forced_set, tolerance, n,
costs, chain))
costs, chain,
scratch.interp_x_deltas, scratch.interp_y_deltas))
return false;
hb_set_t solution;
hb_bit_set_t &solution = scratch.solution.reset ();
int index = n - 1;
while (index != -1)
{
@@ -423,7 +433,7 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
for (unsigned i : solution)
opt_indices.arrayZ[i] = true;
hb_vector_t<bool> rot_indices;
hb_vector_t<bool> &rot_indices = scratch.rot_indices.reset ();
const hb_array_t<const bool> opt_indices_array (opt_indices.arrayZ, opt_indices.length);
rotate_array (opt_indices_array, -k, rot_indices);
@@ -453,19 +463,19 @@ static bool _iup_contour_optimize (const hb_array_t<const contour_point_t> conto
hb_memcpy ((void *) (repeat_points.arrayZ + n), (const void *) contour_points.arrayZ, n * contour_point_size);
}
hb_vector_t<unsigned> costs;
hb_vector_t<int> chain;
if (!_iup_contour_optimize_dp (repeat_points, repeat_x_deltas, repeat_y_deltas,
forced_set, tolerance, n,
costs, chain))
costs, chain,
scratch.interp_x_deltas, scratch.interp_y_deltas))
return false;
unsigned best_cost = n + 1;
int len = costs.length;
hb_set_t best_sol;
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++)
{
hb_set_t solution;
solution.reset ();
int i = start;
int lookback = start - (int) n;
while (i > lookback)
@@ -496,12 +506,14 @@ bool iup_delta_optimize (const contour_point_vector_t& contour_points,
const hb_vector_t<int>& x_deltas,
const hb_vector_t<int>& y_deltas,
hb_vector_t<bool>& opt_indices, /* OUT */
iup_scratch_t &scratch,
double tolerance)
{
if (!opt_indices.resize (contour_points.length))
return false;
hb_vector_t<unsigned> end_points;
hb_vector_t<unsigned> &end_points = scratch.end_points.reset ();
unsigned count = contour_points.length;
if (unlikely (!end_points.alloc (count)))
return false;
@@ -524,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,
scratch))
return false;
start = end + 1;
}
+19
View File
@@ -26,12 +26,31 @@
#define HB_SUBSET_INSTANCER_IUP_HH
#include "hb-subset-plan.hh"
struct iup_scratch_t
{
hb_vector_t<unsigned> end_points;
hb_vector_t<double> interp_x_deltas;
hb_vector_t<double> interp_y_deltas;
hb_vector_t<unsigned> costs;
hb_vector_t<int> chain;
hb_vector_t<bool> rot_indices;
hb_vector_t<int> rot_x_deltas;
hb_vector_t<int> 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;
hb_bit_set_t best_sol;
};
/* 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<int>& x_deltas,
const hb_vector_t<int>& y_deltas,
hb_vector_t<bool>& opt_indices, /* OUT */
iup_scratch_t &scratch,
double tolerance = 0.0);
#endif /* HB_SUBSET_INSTANCER_IUP_HH */
+7 -10
View File
@@ -94,16 +94,13 @@ struct hb_vector_t
}
~hb_vector_t () { fini (); }
template <unsigned n,
typename T = Type,
hb_enable_if (hb_is_trivially_constructible(T) &&
hb_is_trivially_destructible(T))>
template <unsigned n>
void
set_storage (Type (&array)[n])
{
set_storage (array, n);
}
{ set_storage (array, n); }
void
set_storage (hb_array_t<Type> array)
{ set_storage (array.arrayZ, array.length); }
template <typename T = Type,
hb_enable_if (hb_is_trivially_constructible(T) &&
hb_is_trivially_destructible(T))>
@@ -113,7 +110,6 @@ struct hb_vector_t
if (unlikely (in_error ()))
return;
assert (n > 0);
assert (allocated == 0);
assert (length == 0);
@@ -185,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