Merge pull request #5539 from harfbuzz/instancer-optimize-more

[instancer] optimize more
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2025-09-12 01:20:50 -06:00
commit c6a15cd680
3 files changed
+159 -114

No files matched your search

+121 -86
View File
@@ -2331,21 +2331,118 @@ struct delta_row_encoding_t
{
/* each byte represents a region, value is one of 0/1/2/4, which means bytes
* needed for this region */
hb_vector_t<uint8_t> chars;
struct chars_t : hb_vector_t<uint8_t>
{
static chars_t get_row_chars (const hb_vector_t<int>& row)
{
chars_t ret;
if (!ret.alloc (row.length)) return ret;
bool long_words = false;
/* 0/1/2 byte encoding */
for (int i = row.length - 1; i >= 0; i--)
{
int v = row.arrayZ[i];
if (v == 0)
ret.push (0);
else if (v > 32767 || v < -32768)
{
long_words = true;
break;
}
else if (v > 127 || v < -128)
ret.push (2);
else
ret.push (1);
}
if (!long_words)
return ret;
/* redo, 0/2/4 bytes encoding */
ret.reset ();
for (int i = row.length - 1; i >= 0; i--)
{
int v = row.arrayZ[i];
if (v == 0)
ret.push (0);
else if (v > 32767 || v < -32768)
ret.push (4);
else
ret.push (2);
}
return ret;
}
inline unsigned get_width ()
{
unsigned ret = + iter ()
| hb_reduce (hb_add, 0u)
;
return ret;
}
hb_bit_set_t get_columns ()
{
hb_bit_set_t cols;
for (auto _ : + hb_enumerate (iter ()))
{
if (_.second)
cols.add (_.first);
}
return cols;
}
int cmp (const chars_t& other) const
{
return as_array ().cmp (other.as_array ());
}
chars_t combine_chars (const chars_t& other) const
{
chars_t combined_chars;
if (!combined_chars.alloc (length))
return combined_chars;
for (unsigned idx = 0; idx < length; idx++)
{
uint8_t v = hb_max (arrayZ[idx], other.arrayZ[idx]);
combined_chars.push (v);
}
return combined_chars;
}
unsigned combine_width (const chars_t& other) const
{
int combined_width = 0;
for (unsigned i = 0; i < length; i++)
combined_width += hb_max (arrayZ[i], other.arrayZ[i]);
return combined_width;
}
};
chars_t combine_chars (const delta_row_encoding_t& other_encoding) const { return chars.combine_chars (other_encoding.chars); }
unsigned combine_width (const delta_row_encoding_t& other_encoding) const { return chars.combine_width (other_encoding.chars); }
static chars_t get_row_chars (const hb_vector_t<int>& row) { return chars_t::get_row_chars (row); }
// Actual data
chars_t chars;
unsigned width = 0;
hb_vector_t<uint8_t> columns;
hb_bit_set_t columns;
unsigned overhead = 0;
hb_vector_t<const hb_vector_t<int>*> items;
delta_row_encoding_t () = default;
delta_row_encoding_t (hb_vector_t<uint8_t>&& chars_,
delta_row_encoding_t (chars_t&& chars_,
const hb_vector_t<int>* row = nullptr) :
delta_row_encoding_t ()
{
chars = std::move (chars_);
width = get_width ();
columns = get_columns ();
width = chars.get_width ();
columns = chars.get_columns ();
overhead = get_chars_overhead (columns);
if (row) items.push (row);
}
@@ -2353,74 +2450,10 @@ struct delta_row_encoding_t
bool is_empty () const
{ return !items; }
static hb_vector_t<uint8_t> get_row_chars (const hb_vector_t<int>& row)
{
hb_vector_t<uint8_t> ret;
if (!ret.alloc (row.length)) return ret;
bool long_words = false;
/* 0/1/2 byte encoding */
for (int i = row.length - 1; i >= 0; i--)
{
int v = row.arrayZ[i];
if (v == 0)
ret.push (0);
else if (v > 32767 || v < -32768)
{
long_words = true;
break;
}
else if (v > 127 || v < -128)
ret.push (2);
else
ret.push (1);
}
if (!long_words)
return ret;
/* redo, 0/2/4 bytes encoding */
ret.reset ();
for (int i = row.length - 1; i >= 0; i--)
{
int v = row.arrayZ[i];
if (v == 0)
ret.push (0);
else if (v > 32767 || v < -32768)
ret.push (4);
else
ret.push (2);
}
return ret;
}
inline unsigned get_width ()
{
unsigned ret = + hb_iter (chars)
| hb_reduce (hb_add, 0u)
;
return ret;
}
hb_vector_t<uint8_t> get_columns ()
{
hb_vector_t<uint8_t> cols;
cols.alloc (chars.length);
for (auto v : chars)
{
uint8_t flag = v ? 1 : 0;
cols.push (flag);
}
return cols;
}
static inline unsigned get_chars_overhead (const hb_vector_t<uint8_t>& cols)
static inline unsigned get_chars_overhead (const hb_bit_set_t& cols)
{
unsigned c = 4 + 6; // 4 bytes for LOffset, 6 bytes for VarData header
unsigned cols_bit_count = 0;
for (auto v : cols)
if (v) cols_bit_count++;
unsigned cols_bit_count = cols.get_population ();
return c + cols_bit_count * 2;
}
@@ -2431,24 +2464,26 @@ struct delta_row_encoding_t
}
int gain_from_merging (const delta_row_encoding_t& other_encoding,
hb_vector_t<uint8_t> &scratch) const
hb_bit_set_t &scratch) const
{
int combined_width = 0;
for (unsigned i = 0; i < chars.length; i++)
combined_width += hb_max (chars.arrayZ[i], other_encoding.chars.arrayZ[i]);
int combined_gain = (int) overhead + (int) other_encoding.overhead;
hb_vector_t<uint8_t> &combined_columns = scratch;
combined_columns.reset ();
int combined_width = combine_width (other_encoding);
if (unlikely (!combined_columns.resize (columns.length, false)))
return 0;
for (unsigned i = 0; i < columns.length; i++)
combined_columns.arrayZ[i] = columns.arrayZ[i] | other_encoding.columns.arrayZ[i];
combined_gain -= (combined_width - (int) width) * items.length;
combined_gain -= (combined_width - (int) other_encoding.width) * other_encoding.items.length;
// If the returned gain is negative, the actual magnitude is unused.
// Short-circuit if that is gonna happen.
if (combined_gain <= 0)
return combined_gain;
hb_bit_set_t &combined_columns = scratch;
combined_columns = columns;
combined_columns.union_ (other_encoding.columns);
int combined_overhead = get_chars_overhead (combined_columns);
int combined_gain = (int) overhead + (int) other_encoding.overhead - combined_overhead
- (combined_width - (int) width) * items.length
- (combined_width - (int) other_encoding.width) * other_encoding.items.length;
combined_gain -= combined_overhead;
return combined_gain;
}
@@ -2464,7 +2499,7 @@ struct delta_row_encoding_t
if (gain_a != gain_b)
return gain_a - gain_b;
return (b->chars).as_array ().cmp ((a->chars).as_array ());
return b->chars.cmp (a->chars);
}
static int cmp_width (const void *pa, const void *pb)
@@ -2475,7 +2510,7 @@ struct delta_row_encoding_t
if (a->width != b->width)
return (int) a->width - (int) b->width;
return (b->chars).as_array ().cmp ((a->chars).as_array ());
return b->chars.cmp (a->chars);
}
bool add_row (const hb_vector_t<int>* row)
+27 -27
View File
@@ -592,7 +592,7 @@ struct tuple_delta_t
return true;
if (unlikely (end_points.in_error ())) return false;
hb_set_t inferred_idxes;
hb_bit_set_t inferred_idxes;
unsigned start_point = 0;
for (unsigned end_point : end_points)
{
@@ -1762,19 +1762,16 @@ struct item_variations_t
bool operator < (const combined_gain_idx_tuple_t& o)
{
if (gain != o.gain)
return gain < o.gain;
if (idx_1 != o.idx_1)
return idx_1 < o.idx_1;
return idx_2 < o.idx_2;
return gain < o.gain ||
(gain == o.gain && (idx_1 < o.idx_1 ||
(idx_1 == o.idx_1 && idx_2 < o.idx_2)));
}
bool operator <= (const combined_gain_idx_tuple_t& o)
{
if (*this < o) return true;
return gain == o.gain && idx_1 == o.idx_1 && idx_2 == o.idx_2;
return gain < o.gain ||
(gain == o.gain && (idx_1 < o.idx_1 ||
(idx_1 == o.idx_1 && idx_2 <= o.idx_2)));
}
};
@@ -1860,7 +1857,7 @@ struct item_variations_t
if (!front_mapping.set ((major<<16) + minor, &row))
return false;
hb_vector_t<uint8_t> chars = delta_row_encoding_t::get_row_chars (row);
auto chars = delta_row_encoding_t::get_row_chars (row);
if (!chars) return false;
if (delta_rows_map.has (&row))
@@ -1893,11 +1890,12 @@ struct item_variations_t
/* sort encoding_objs */
encoding_objs.qsort ();
hb_vector_t<uint8_t> scratch;
hb_bit_set_t scratch;
/* main algorithm: repeatedly pick 2 best encodings to combine, and combine
* them */
hb_priority_queue_t<combined_gain_idx_tuple_t> queue;
using item_t = hb_priority_queue_t<combined_gain_idx_tuple_t>::item_t;
hb_vector_t<item_t> queue_items;
unsigned num_todos = encoding_objs.length;
for (unsigned i = 0; i < num_todos; i++)
{
@@ -1905,11 +1903,16 @@ struct item_variations_t
{
int combining_gain = encoding_objs.arrayZ[i].gain_from_merging (encoding_objs.arrayZ[j], scratch);
if (combining_gain > 0)
queue.insert (combined_gain_idx_tuple_t (-combining_gain, i, j), 0);
{
auto item = item_t (combined_gain_idx_tuple_t (-combining_gain, i, j), 0);
queue_items.push (item);
}
}
}
hb_set_t removed_todo_idxes;
hb_priority_queue_t<combined_gain_idx_tuple_t> queue (std::move (queue_items));
hb_bit_set_t removed_todo_idxes;
while (queue)
{
auto t = queue.pop_minimum ().first;
@@ -1925,17 +1928,7 @@ struct item_variations_t
removed_todo_idxes.add (i);
removed_todo_idxes.add (j);
hb_vector_t<uint8_t> combined_chars;
if (!combined_chars.alloc (encoding.chars.length))
return false;
for (unsigned idx = 0; idx < encoding.chars.length; idx++)
{
uint8_t v = hb_max (encoding.chars.arrayZ[idx], other_encoding.chars.arrayZ[idx]);
combined_chars.push (v);
}
delta_row_encoding_t combined_encoding_obj (std::move (combined_chars));
delta_row_encoding_t combined_encoding_obj (std::move (encoding.combine_chars (other_encoding)));
for (const auto& row : hb_concat (encoding.items, other_encoding.items))
combined_encoding_obj.add_row (row);
@@ -1944,8 +1937,15 @@ struct item_variations_t
if (removed_todo_idxes.has (idx)) continue;
const delta_row_encoding_t& obj = encoding_objs.arrayZ[idx];
if (obj.chars == combined_chars)
// In the unlikely event that the same encoding exists already, combine it.
if (obj.chars == combined_encoding_obj.chars)
{
// This is straight port from fonttools algorithm. I added this branch there
// because I thought it can happen. But looks like we never get in here in
// practice. I'm not confident enough to remove it though; in theory it can
// happen. I think it's just that our tests are not extensive enough to hit
// this path.
for (const auto& row : obj.items)
combined_encoding_obj.add_row (row);
+11 -1
View File
@@ -45,12 +45,22 @@
template <typename K>
struct hb_priority_queue_t
{
private:
public:
typedef hb_pair_t<K, unsigned> item_t;
private:
hb_vector_t<item_t> heap;
public:
hb_priority_queue_t () = default;
hb_priority_queue_t (hb_vector_t<item_t>&& other) : heap (std::move (other))
{
// Heapify the vector.
for (int i = (heap.length / 2) - 1; i >= 0; i--)
bubble_down (i);
}
void reset () { heap.resize (0); }
bool in_error () const { return heap.in_error (); }