[depend] Encode common active glyph states

Root and singleton active-glyph sets dominate dependency recursion memo
keys. Avoid copying and interning the root set, and encode singleton glyph
IDs directly in the key.

Add an internal singleton query that can recognize uncached sparse sets in
one pass while preserving the population cache.

On NotoNastaliqUrdu-Regular.ttf this reduces the compile-only dependency
run from about 3.18B to 3.05B instructions and from about 245ms to 238ms.
Dependency graphs match the previous output across the focused font
corpus.

Tested with: meson test -C build

Assisted-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Behdad Esfahbod committed 2026-08-20 03:45:19 -06:00
1 parent 772005ba59
commit 4daafa0767
5 files changed
+132 -16

No files matched your search

+9
View File
@@ -101,6 +101,15 @@ struct hb_bit_set_invertible_t
}
unsigned int get_population () const
{ return inverted ? INVALID - s.get_population () : s.get_population (); }
bool get_singleton (hb_codepoint_t *codepoint) const
{
if (likely (!inverted))
return s.get_singleton (codepoint);
if (s.get_population () != INVALID - 1)
return false;
*codepoint = get_min ();
return *codepoint != INVALID;
}
void add (hb_codepoint_t g) { unlikely (inverted) ? s.del (g) : s.add (g); }
+33
View File
@@ -942,6 +942,39 @@ struct hb_bit_set_t
population = pop;
return pop;
}
bool get_singleton (hb_codepoint_t *codepoint) const
{
if (has_population ())
{
if (population != 1) return false;
*codepoint = get_min ();
return true;
}
hb_codepoint_t singleton = INVALID;
for (const auto &map : page_map)
{
const auto &page = pages.arrayZ[map.index];
for (unsigned i = 0; i < page_t::len (); i++)
{
page_t::elt_t bits = page.v[i];
if (!bits) continue;
if (singleton != INVALID || (bits & (bits - 1)))
return false;
singleton = map.major * page_t::PAGE_BITS +
i * page_t::ELT_BITS + hb_ctz (bits);
}
}
if (singleton == INVALID)
{
population = 0;
return false;
}
population = 1;
*codepoint = singleton;
return true;
}
hb_codepoint_t get_min () const
{
unsigned count = pages.length;
+69 -16
View File
@@ -389,6 +389,8 @@ struct hb_depend_context_t :
GLYPH_SET_SINGLE
};
static constexpr hb_codepoint_t SINGLE_GLYPH_ACTIVE_FLAG = 0x80000000u;
struct glyph_set_cache_key_t
{
glyph_set_cache_key_t () = default;
@@ -504,23 +506,43 @@ struct hb_depend_context_t :
lookups_seen.del (lookup_idx);
}
struct active_glyphs_frame_t
{
hb_set_t glyphs;
hb_codepoint_t active_idx = HB_CODEPOINT_INVALID;
bool use_root = false;
};
const hb_set_t& parent_active_glyphs ()
{
if (!active_glyphs_stack_depth)
return *glyphs;
return active_glyphs_stack[active_glyphs_stack_depth - 1];
const active_glyphs_frame_t &frame = active_glyphs_stack[active_glyphs_stack_depth - 1];
return frame.use_root ? *glyphs : frame.glyphs;
}
hb_set_t* push_cur_active_glyphs ()
hb_set_t* push_cur_active_glyphs (hb_codepoint_t active_idx = HB_CODEPOINT_INVALID)
{
if (active_glyphs_stack_depth == active_glyphs_stack.length &&
unlikely (!active_glyphs_stack.push_or_fail ()))
return nullptr;
hb_set_t *set = &active_glyphs_stack[active_glyphs_stack_depth++];
set->clear ();
return set;
active_glyphs_frame_t &frame = active_glyphs_stack[active_glyphs_stack_depth++];
frame.glyphs.clear ();
frame.active_idx = active_idx;
frame.use_root = false;
return &frame.glyphs;
}
bool push_root_active_glyphs ()
{
if (active_glyphs_stack_depth == active_glyphs_stack.length &&
unlikely (!active_glyphs_stack.push_or_fail ()))
return false;
active_glyphs_stack[active_glyphs_stack_depth++].use_root = true;
return true;
}
bool pop_cur_done_glyphs ()
@@ -537,9 +559,23 @@ struct hb_depend_context_t :
/* The active glyphs and in-progress lookup set are part of the state.
* Omitting the latter would make memoization incorrect for cyclic lookup
* graphs whose available recursive paths depend on their ancestry. */
hb_codepoint_t active_idx;
if (!find_or_create_recurse_set (parent_active_glyphs (), &active_idx))
return false;
bool uses_root = !active_glyphs_stack_depth ||
active_glyphs_stack[active_glyphs_stack_depth - 1].use_root;
hb_codepoint_t active_idx = HB_CODEPOINT_INVALID;
if (!uses_root)
{
const active_glyphs_frame_t &frame = active_glyphs_stack[active_glyphs_stack_depth - 1];
active_idx = frame.active_idx;
if (active_idx == HB_CODEPOINT_INVALID)
{
hb_codepoint_t glyph;
if (frame.glyphs.get_singleton (&glyph) &&
likely (glyph < SINGLE_GLYPH_ACTIVE_FLAG))
active_idx = glyph | SINGLE_GLYPH_ACTIVE_FLAG;
else if (!find_or_create_recurse_set (frame.glyphs, &active_idx))
return false;
}
}
*key = recurse_key_t (lookup_idx,
active_idx,
@@ -581,6 +617,11 @@ struct hb_depend_context_t :
}
*index = recurse_sets.length;
if (unlikely (*index & SINGLE_GLYPH_ACTIVE_FLAG))
{
hb_set_destroy (copy);
return depend_data->fail ();
}
if (unlikely (!recurse_sets.push_or_fail (hb::unique_ptr<hb_set_t> {copy})))
return depend_data->fail ();
if (unlikely (!recurse_set_to_index.set (recurse_sets[*index].get (), *index)))
@@ -652,7 +693,7 @@ struct hb_depend_context_t :
hb_depend_data_builder_t *depend_data;
hb_face_t *face;
hb_set_t *glyphs;
hb_vector_t<hb_set_t> active_glyphs_stack;
hb_vector_t<active_glyphs_frame_t> active_glyphs_stack;
unsigned active_glyphs_stack_depth = 0;
recurse_func_t recurse_func;
hb_codepoint_t lookup_index = HB_CODEPOINT_INVALID;
@@ -2400,18 +2441,30 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c,
}
covered_seq_indices.add (seqIndex);
hb_set_t *cur_active_glyphs = c->push_cur_active_glyphs ();
if (unlikely (!cur_active_glyphs))
if (has_pos_glyphs)
{
hb_codepoint_t active_idx = HB_CODEPOINT_INVALID;
if (context_format == ContextFormat::SimpleContext)
{
hb_codepoint_t glyph = seqIndex ? pos_glyphs.get_min () : value;
if (likely (glyph < hb_depend_context_t::SINGLE_GLYPH_ACTIVE_FLAG))
active_idx = glyph | hb_depend_context_t::SINGLE_GLYPH_ACTIVE_FLAG;
}
hb_set_t *cur_active_glyphs = c->push_cur_active_glyphs (active_idx);
if (unlikely (!cur_active_glyphs))
{
c->depend_data->current_context_set_index = saved_context_set_index;
c->depend_data->current_edge_flags = saved_edge_flags;
return;
}
*cur_active_glyphs = std::move (pos_glyphs);
}
else if (unlikely (!c->push_root_active_glyphs ()))
{
c->depend_data->current_context_set_index = saved_context_set_index;
c->depend_data->current_edge_flags = saved_edge_flags;
return;
}
if (has_pos_glyphs) {
*cur_active_glyphs = std::move (pos_glyphs);
} else {
*cur_active_glyphs = *c->glyphs;
}
unsigned endIndex = inputCount;
if (context_format == ContextFormat::CoverageBasedContext)
+1
View File
@@ -156,6 +156,7 @@ struct hb_sparseset_t
{ return s.next_many (codepoint, out, size); }
unsigned int get_population () const { return s.get_population (); }
bool get_singleton (hb_codepoint_t *codepoint) const { return s.get_singleton (codepoint); }
hb_codepoint_t get_min () const { return s.get_min (); }
hb_codepoint_t get_max () const { return s.get_max (); }
+20
View File
@@ -158,6 +158,26 @@ main (int argc, char **argv)
hb_always_assert (b.intersects (c));
}
/* Test singleton detection. */
{
hb_set_t s;
hb_codepoint_t singleton;
hb_always_assert (!s.get_singleton (&singleton));
s.add (63);
hb_always_assert (s.get_singleton (&singleton));
hb_always_assert (singleton == 63);
s.add (1024);
hb_always_assert (!s.get_singleton (&singleton));
s.del (63);
hb_always_assert (s.get_singleton (&singleton));
hb_always_assert (singleton == 1024);
s.invert ();
hb_always_assert (!s.get_singleton (&singleton));
}
/* Adding HB_SET_VALUE_INVALID */
{
hb_set_t s;