Merge pull request #5555 from harfbuzz/instancer-gvar-micro-opts

Instancer gvar micro opts
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2025-09-15 01:11:30 -06:00
commit b33f6fd213
3 files changed
+28 -42

No files matched your search

+7 -1
View File
@@ -338,7 +338,13 @@ struct hb_hashmap_t
bool has (const K &key, VV **vp = nullptr) const
{
if (!items) return false;
auto *item = fetch_item (key, hb_hash (key));
return has_with_hash (key, hb_hash (key), vp);
}
template <typename VV=V>
bool has_with_hash (const K &key, uint32_t hash, VV **vp = nullptr) const
{
if (!items) return false;
auto *item = fetch_item (key, hash);
if (item)
{
if (vp) *vp = std::addressof (item->value);
+1 -1
View File
@@ -82,7 +82,7 @@ struct NumType
NumType operator ++ (int) { NumType c (*this); ++*this; return c; }
NumType operator -- (int) { NumType c (*this); --*this; return c; }
unsigned hash () const { return hb_array ((const char *) &v, sizeof (v)).hash (); }
uint32_t hash () const { return hb_array ((const char *) &v, sizeof (v)).hash (); }
HB_INTERNAL static int cmp (const NumType *a, const NumType *b)
{ return b->cmp (*a); }
HB_INTERNAL static int cmp (const void *a, const void *b)
+20 -40
View File
@@ -181,11 +181,12 @@ struct glyph_variations_t
{
if (!var.compile_coords (axes_index_map, axes_old_index_tag_map))
return false;
unsigned* count;
if (coords_count_map.has (&(var.compiled_peak_coords), &count))
coords_count_map.set (&(var.compiled_peak_coords), *count + 1);
unsigned *count;
unsigned hash = hb_hash (&var.compiled_peak_coords);
if (coords_count_map.has_with_hash (&(var.compiled_peak_coords), hash, &count))
(*count)++;
else
coords_count_map.set (&(var.compiled_peak_coords), 1);
coords_count_map.set_with_hash (&(var.compiled_peak_coords), hash, 1);
}
}
@@ -193,66 +194,45 @@ struct glyph_variations_t
return false;
/* add only those coords that are used more than once into the vector and sort */
hb_vector_t<const hb_vector_t<F2DOT14>*> shared_coords;
if (unlikely (!shared_coords.alloc (coords_count_map.get_population ())))
return false;
for (const auto _ : coords_count_map.iter ())
{
if (_.second == 1) continue;
shared_coords.push (_.first);
}
hb_vector_t<hb_pair_t<const hb_vector_t<F2DOT14>*, unsigned>> shared_coords {
+ hb_iter (coords_count_map)
| hb_filter ([] (const hb_pair_t<const hb_vector_t<F2DOT14>*, unsigned>& p) { return p.second > 1; })
};
if (unlikely (shared_coords.in_error ())) return false;
/* no shared tuples: no coords are used more than once */
if (!shared_coords) return true;
/* sorting based on the coords frequency first (high to low), then compare
* the coords bytes */
hb_qsort (shared_coords.arrayZ, shared_coords.length, sizeof (hb_vector_t<F2DOT14>*), _cmp_coords, (void *) (&coords_count_map));
shared_coords.qsort (_cmp_coords);
/* build shared_coords->idx map and shared tuples byte array */
shared_tuples_count = hb_min (0xFFFu + 1, shared_coords.length);
unsigned len = shared_tuples_count * (shared_coords[0]->length);
unsigned len = shared_tuples_count * (shared_coords[0].first->length);
if (unlikely (!compiled_shared_tuples.alloc (len)))
return false;
for (unsigned i = 0; i < shared_tuples_count; i++)
{
shared_tuples_idx_map.set (shared_coords[i], i);
shared_tuples_idx_map.set (shared_coords[i].first, i);
/* add a concat() in hb_vector_t? */
for (auto c : shared_coords[i]->iter ())
for (auto c : shared_coords[i].first->iter ())
compiled_shared_tuples.push (c);
}
return true;
}
static int _cmp_coords (const void *pa, const void *pb, void *arg)
static int _cmp_coords (const void *pa, const void *pb)
{
const hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned>* coords_count_map =
reinterpret_cast<const hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned>*> (arg);
const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *a = (const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *) pa;
const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *b = (const hb_pair_t<hb_vector_t<F2DOT14> *, unsigned> *) pb;
/* shared_coords is hb_vector_t<const hb_vector_t<F2DOT14>*> so casting pa/pb
* to be a pointer to a pointer */
const hb_vector_t<F2DOT14>** a = reinterpret_cast<const hb_vector_t<F2DOT14>**> (const_cast<void*>(pa));
const hb_vector_t<F2DOT14>** b = reinterpret_cast<const hb_vector_t<F2DOT14>**> (const_cast<void*>(pb));
if (a->second != b->second)
return b->second - a->second; // high to low
bool has_a = coords_count_map->has (*a);
bool has_b = coords_count_map->has (*b);
if (has_a && has_b)
{
unsigned a_num = coords_count_map->get (*a);
unsigned b_num = coords_count_map->get (*b);
if (a_num != b_num)
return b_num - a_num;
return (*b)->as_array().cmp ((*a)->as_array ());
}
else if (has_a) return -1;
else if (has_b) return 1;
else return 0;
return b->first->as_array().cmp (a->first->as_array ());
}
template<typename Iterator,