From e42994749c548bb6736e565ec91db59dcbd38560 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 3 Sep 2026 23:54:33 -0600 Subject: [PATCH] OT: Cache lookup subtable indices --- src/OT/Layout/types.hh | 3 ++ src/hb-ot-layout-gsubgpos.hh | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/OT/Layout/types.hh b/src/OT/Layout/types.hh index 5cf9eb368..81489f3fa 100644 --- a/src/OT/Layout/types.hh +++ b/src/OT/Layout/types.hh @@ -35,6 +35,9 @@ static_assert (sizeof (hb_ot_layout_mapping_cache_t) == 512, ""); using hb_ot_layout_binary_cache_t = hb_cache_t<14, 1, 8>; static_assert (sizeof (hb_ot_layout_binary_cache_t) == 256, ""); +using hb_ot_layout_subtable_index_cache_t = hb_cache_t<12, 3, 7>; +static_assert (sizeof (hb_ot_layout_subtable_index_cache_t) == 128, ""); + namespace OT { namespace Layout { diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index abe0c1db4..453d5d92f 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -5849,6 +5849,14 @@ struct hb_ot_layout_lookup_accelerator_t thiz->count = count; #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE + if (count > 4) + { + thiz->subtable_index_cache = (hb_ot_layout_subtable_index_cache_t *) + hb_malloc (sizeof (hb_ot_layout_subtable_index_cache_t)); + if (likely (thiz->subtable_index_cache)) + new (thiz->subtable_index_cache) hb_ot_layout_subtable_index_cache_t (); + } + thiz->subtable_cache_user_idx = c_accelerate_subtables.subtable_cache_user_idx; for (unsigned i = 0; i < count; i++) @@ -5862,6 +5870,7 @@ struct hb_ot_layout_lookup_accelerator_t void fini () { #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE + hb_free (subtable_index_cache); for (unsigned i = 0; i < count; i++) hb_free (subtables[i].external_cache); #endif @@ -5888,6 +5897,49 @@ struct hb_ot_layout_lookup_accelerator_t #endif return subtables[0].apply_no_digest (c); } +#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE + if (subtable_index_cache) + { + hb_codepoint_t glyph = c->buffer->cur().codepoint; + unsigned cached_index; + if (subtable_index_cache->get (glyph, &cached_index) && + cached_index < count) + { + if (use_cache ? subtables[cached_index].apply_cached_no_digest (c) : + subtables[cached_index].apply_no_digest (c)) + return true; + + /* The cached subtable was the first whose digest admitted this glyph, + * so no earlier subtable can apply. */ + for (unsigned i = cached_index + 1; i < count; i++) + if (use_cache ? subtables[i].apply_cached (c) : subtables[i].apply (c)) + return true; + return false; + } + + /* Only cache a successful subtable if all earlier subtable digests + * reject the glyph. This preserves lookup order when contextual + * subtables have overlapping first-glyph coverages. */ + unsigned i = 0; + while (i < count && !subtables[i].digest.may_have (glyph)) + i++; + if (i < count) + { + if (use_cache ? subtables[i].apply_cached_no_digest (c) : + subtables[i].apply_no_digest (c)) + { + subtable_index_cache->set (glyph, i); + return true; + } + i++; + } + for (; i < count; i++) + if (use_cache ? subtables[i].apply_cached (c) : subtables[i].apply (c)) + return true; + return false; + } +#endif + #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE if (use_cache) { @@ -5930,6 +5982,7 @@ struct hb_ot_layout_lookup_accelerator_t private: unsigned count = 0; /* Number of subtables in the array. */ #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE + hb_ot_layout_subtable_index_cache_t *subtable_index_cache = nullptr; unsigned subtable_cache_user_idx = (unsigned) -1; #endif hb_accelerate_subtables_context_t::hb_applicable_t subtables[HB_VAR_ARRAY];