From f454a490347bff2804f6ee735d080e11cf0c88f2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 20 Aug 2026 01:06:37 -0600 Subject: [PATCH] [depend] Keep covered context positions inline Contextual dependency traversal uses a set to track sequence positions already covered by earlier lookup records. Those positions are bounded by HB_MAX_CONTEXT_LENGTH, but the general sparse set allocates its first bit page on every contextual rule. Use one inline hb_bit_page_t for dependency traversal. Its 512 bits are well above the configured 64-position context limit. This reduces quiet Noto Nastaliq graph construction from 385ms to 363ms. Tested with the full Meson test suite (272/272 passed). Assisted-by: OpenAI Codex --- src/OT/Layout/GSUB/SubstLookup.hh | 4 ++-- src/hb-ot-layout-gsub-table.hh | 4 ++-- src/hb-ot-layout-gsubgpos.hh | 15 ++++++++------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/OT/Layout/GSUB/SubstLookup.hh b/src/OT/Layout/GSUB/SubstLookup.hh index d3b1d1081..ffa5e20bb 100644 --- a/src/OT/Layout/GSUB/SubstLookup.hh +++ b/src/OT/Layout/GSUB/SubstLookup.hh @@ -211,9 +211,9 @@ struct SubstLookup : Lookup return ret; } - static inline hb_depend_context_t::return_t depend_glyphs_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indices, unsigned seq_index, unsigned end_index); + static inline hb_depend_context_t::return_t depend_glyphs_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index); - static inline hb_depend_context_t::return_t dispatch_depend_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indices, unsigned seq_index, unsigned end_index) + static inline hb_depend_context_t::return_t dispatch_depend_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index) { return depend_glyphs_recurse_func (c, lookup_index, covered_seq_indices, seq_index, end_index); } diff --git a/src/hb-ot-layout-gsub-table.hh b/src/hb-ot-layout-gsub-table.hh index 240bc6163..2c7faec88 100644 --- a/src/hb-ot-layout-gsub-table.hh +++ b/src/hb-ot-layout-gsub-table.hh @@ -58,14 +58,14 @@ template return l.dispatch (c); } -/*static*/ hb_depend_context_t::return_t SubstLookup::depend_glyphs_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indices, unsigned seq_index, unsigned end_index) +/*static*/ hb_depend_context_t::return_t SubstLookup::depend_glyphs_recurse_func (hb_depend_context_t *c, unsigned lookup_index, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index) { const SubstLookup &l = c->face->table.GSUB.get_relaxed ()->table->get_lookup (lookup_index); /* After a non-1-to-1 lookup (expansion/contraction), subsequent lookups in the same * contextual rule see all glyphs, not position-specific ones. This matches closure behavior. * Mark sequence positions as covered so later lookups use the full glyph set. */ if (l.may_have_non_1to1 ()) - hb_set_add_range (covered_seq_indices, seq_index, end_index); + covered_seq_indices->add_range (seq_index, end_index); hb_depend_context_t::recurse_key_t key; if (!c->get_recurse_key (lookup_index, &key)) diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 5450ebb0b..ddc960e3f 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -451,13 +451,13 @@ struct hb_depend_context_t : hb_subset_depend_edge_flags_t flags = HB_SUBSET_DEPEND_EDGE_FLAG_NONE; }; - typedef return_t (*recurse_func_t) (hb_depend_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indicies, unsigned seq_index, unsigned end_index); + typedef return_t (*recurse_func_t) (hb_depend_context_t *c, unsigned lookup_index, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index); /* return_t is hb_empty_t — dispatch is purely for side-effects, like hb_closure_context_t. */ template return_t dispatch (const T &obj) { obj.depend (this); return hb_empty_t (); } static return_t default_return_value () { return hb_empty_t (); } - void recurse (unsigned lookup_idx, hb_set_t *covered_seq_indicies, unsigned seq_index, unsigned end_index) + void recurse (unsigned lookup_idx, hb_bit_page_t *covered_seq_indices, unsigned seq_index, unsigned end_index) { /* Cycle detection using call-stack semantics: block re-entry of a * lookup that is currently on the execution stack, but allow it again @@ -478,7 +478,7 @@ struct hb_depend_context_t : * that indirect cycles of the form A→B→A are also caught here. */ if (lookups_seen.has (lookup_idx)) return; lookups_seen.add (lookup_idx); - recurse_func (this, lookup_idx, covered_seq_indicies, seq_index, end_index); + recurse_func (this, lookup_idx, covered_seq_indices, seq_index, end_index); lookups_seen.del (lookup_idx); } @@ -2148,7 +2148,8 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, * - Sequential accumulation: Not done. We want ALL possible edges, so we don't * limit later lookups based on outputs from earlier lookups in the same rule. */ - hb_set_t covered_seq_indicies; + /* HB_MAX_CONTEXT_LENGTH positions fit in one allocation-free bit page. */ + hb_bit_page_t covered_seq_indices; hb_set_t pos_glyphs; for (unsigned int i = 0; i < lookupCount; i++) { @@ -2267,7 +2268,7 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, bool has_pos_glyphs = false; - if (!covered_seq_indicies.has (seqIndex)) + if (!covered_seq_indices.has (seqIndex)) { has_pos_glyphs = true; pos_glyphs.clear (); @@ -2299,7 +2300,7 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, } } - covered_seq_indicies.add (seqIndex); + covered_seq_indices.add (seqIndex); hb_set_t *cur_active_glyphs = c->push_cur_active_glyphs (); if (unlikely (!cur_active_glyphs)) { @@ -2317,7 +2318,7 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c, if (context_format == ContextFormat::CoverageBasedContext) endIndex += 1; - c->recurse (lookupRecord[i].lookupListIndex, &covered_seq_indicies, seqIndex, endIndex); + c->recurse (lookupRecord[i].lookupListIndex, &covered_seq_indices, seqIndex, endIndex); c->pop_cur_done_glyphs (); /* Restore outer context after this lookup, so subsequent lookups in this