From d15bc8d4e9b30edeeba2c3414d09366e94ad1273 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 20 Dec 2025 17:07:02 -0700 Subject: [PATCH] [contextual] Use a binary coverage cache (in format2) Reduces cache size by half. Since we discard the actual coverage number anyway. --- src/OT/Layout/Common/Coverage.hh | 15 +++++++++++++++ src/OT/Layout/types.hh | 3 +++ src/hb-cache.hh | 16 ++++++++++------ src/hb-ot-layout-gsubgpos.hh | 8 ++++---- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/OT/Layout/Common/Coverage.hh b/src/OT/Layout/Common/Coverage.hh index ae0635ba9..93ca1b2f7 100644 --- a/src/OT/Layout/Common/Coverage.hh +++ b/src/OT/Layout/Common/Coverage.hh @@ -111,6 +111,21 @@ struct Coverage return coverage; } + unsigned int get_coverage_binary (hb_codepoint_t glyph_id, + hb_ot_layout_binary_cache_t *cache) const + { + unsigned coverage; + if (cache && cache->get (glyph_id, &coverage)) return coverage < cache->MAX_VALUE ? coverage : NOT_COVERED; + coverage = get_coverage (glyph_id); + if (cache) { + if (coverage == NOT_COVERED) + cache->set_unchecked (glyph_id, cache->MAX_VALUE); + else + cache->set_unchecked (glyph_id, 0); + } + return coverage; + } + unsigned get_population () const { switch (u.format.v) { diff --git a/src/OT/Layout/types.hh b/src/OT/Layout/types.hh index 6e412e5fe..65d0fc8e6 100644 --- a/src/OT/Layout/types.hh +++ b/src/OT/Layout/types.hh @@ -32,6 +32,9 @@ using hb_ot_layout_mapping_cache_t = hb_cache_t<15, 8, 7>; static_assert (sizeof (hb_ot_layout_mapping_cache_t) == 256, ""); +using hb_ot_layout_binary_cache_t = hb_cache_t<14, 1, 7>; +static_assert (sizeof (hb_ot_layout_binary_cache_t) == 128, ""); + namespace OT { namespace Layout { diff --git a/src/hb-cache.hh b/src/hb-cache.hh index cb89e1f40..7600f134f 100644 --- a/src/hb-cache.hh +++ b/src/hb-cache.hh @@ -64,12 +64,16 @@ template , - hb_atomic_t>::type, - typename std::conditional::type + typename std::conditional, + typename std::conditional, + hb_atomic_t>::type>::type, + typename std::conditional::type>::type >::type; static_assert ((key_bits >= cache_bits), ""); diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index e5fdd0153..1928917b5 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -2710,7 +2710,7 @@ struct ContextFormat2_5 struct external_cache_t { - hb_ot_layout_mapping_cache_t coverage; + hb_ot_layout_binary_cache_t coverage; }; void *external_cache_create () const { @@ -2728,7 +2728,7 @@ struct ContextFormat2_5 TRACE_APPLY (this); #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE external_cache_t *cache = (external_cache_t *) external_cache; - unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint, cache ? &cache->coverage : nullptr); + unsigned int index = (this+coverage).get_coverage_binary (c->buffer->cur().codepoint, cache ? &cache->coverage : nullptr); #else unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); #endif @@ -3951,7 +3951,7 @@ struct ChainContextFormat2_5 struct external_cache_t { - hb_ot_layout_mapping_cache_t coverage; + hb_ot_layout_binary_cache_t coverage; }; void *external_cache_create () const { @@ -3969,7 +3969,7 @@ struct ChainContextFormat2_5 TRACE_APPLY (this); #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE external_cache_t *cache = (external_cache_t *) external_cache; - unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint, cache ? &cache->coverage : nullptr); + unsigned int index = (this+coverage).get_coverage_binary (c->buffer->cur().codepoint, cache ? &cache->coverage : nullptr); #else unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint); #endif