From f22943a2a2f5bb8d3f81c6efe3af837645ded8bb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 5 Feb 2025 18:51:11 +0000 Subject: [PATCH] [PairPos] Cache coverage as well Another 3% down in Roboto-Regular. --- src/OT/Layout/Common/Coverage.hh | 22 ++++++++++++++++++++++ src/OT/Layout/Common/CoverageFormat1.hh | 2 ++ src/OT/Layout/Common/CoverageFormat2.hh | 2 ++ src/OT/Layout/GPOS/PairPosFormat2.hh | 21 +++++++++++++++++---- src/OT/Layout/types.hh | 3 +++ src/hb-ot-layout-common.hh | 5 +---- 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/OT/Layout/Common/Coverage.hh b/src/OT/Layout/Common/Coverage.hh index 344e87afb..daa197954 100644 --- a/src/OT/Layout/Common/Coverage.hh +++ b/src/OT/Layout/Common/Coverage.hh @@ -96,6 +96,15 @@ struct Coverage default:return NOT_COVERED; } } + unsigned int get_coverage (hb_codepoint_t glyph_id, + hb_ot_lookup_cache_t *cache) const + { + unsigned coverage; + if (cache && cache->get (glyph_id, &coverage)) return coverage; + coverage = get_coverage (glyph_id); + if (cache) cache->set (glyph_id, coverage); + return coverage; + } unsigned get_population () const { @@ -201,6 +210,19 @@ struct Coverage } } + unsigned cost () const + { + switch (u.format) { + case 1: hb_barrier (); return u.format1.cost (); + case 2: hb_barrier (); return u.format2.cost (); +#ifndef HB_NO_BEYOND_64K + case 3: hb_barrier (); return u.format3.cost (); + case 4: hb_barrier (); return u.format4.cost (); +#endif + default:return 0u; + } + } + /* Might return false if array looks unsorted. * Used for faster rejection of corrupt data. */ template diff --git a/src/OT/Layout/Common/CoverageFormat1.hh b/src/OT/Layout/Common/CoverageFormat1.hh index 3f598d40e..dd418b0bc 100644 --- a/src/OT/Layout/Common/CoverageFormat1.hh +++ b/src/OT/Layout/Common/CoverageFormat1.hh @@ -103,6 +103,8 @@ struct CoverageFormat1_3 intersect_glyphs << glyphArray[i]; } + unsigned cost () const { return 1; } + template bool collect_coverage (set_t *glyphs) const { return glyphs->add_sorted_array (glyphArray.as_array ()); } diff --git a/src/OT/Layout/Common/CoverageFormat2.hh b/src/OT/Layout/Common/CoverageFormat2.hh index 9c8754235..dd577fd90 100644 --- a/src/OT/Layout/Common/CoverageFormat2.hh +++ b/src/OT/Layout/Common/CoverageFormat2.hh @@ -157,6 +157,8 @@ struct CoverageFormat2_4 } } + unsigned cost () const { return hb_bit_storage ((unsigned) rangeRecord.len); /* bsearch cost */ } + template bool collect_coverage (set_t *glyphs) const { diff --git a/src/OT/Layout/GPOS/PairPosFormat2.hh b/src/OT/Layout/GPOS/PairPosFormat2.hh index 85acbcf54..8855a2a91 100644 --- a/src/OT/Layout/GPOS/PairPosFormat2.hh +++ b/src/OT/Layout/GPOS/PairPosFormat2.hh @@ -123,12 +123,16 @@ struct PairPosFormat2_4 : ValueBase const Coverage &get_coverage () const { return this+coverage; } - using pair_pos_cache_t = hb_pair_t; + struct pair_pos_cache_t + { + hb_ot_lookup_cache_t coverage; + hb_ot_lookup_cache_t first; + hb_ot_lookup_cache_t second; + }; unsigned cache_cost () const { - unsigned c = (this+classDef1).cost () + (this+classDef2).cost (); - return c >= 4 ? c : 0; + return (this+coverage).cost () + (this+classDef1).cost () + (this+classDef2).cost (); } static void * cache_func (void *p, hb_ot_lookup_cache_op_t op) { @@ -139,6 +143,7 @@ struct PairPosFormat2_4 : ValueBase pair_pos_cache_t *cache = (pair_pos_cache_t *) hb_malloc (sizeof (pair_pos_cache_t)); if (likely (cache)) { + cache->coverage.clear (); cache->first.clear (); cache->second.clear (); } @@ -162,8 +167,17 @@ struct PairPosFormat2_4 : ValueBase bool _apply (hb_ot_apply_context_t *c, bool cached) const { TRACE_APPLY (this); + +#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE + pair_pos_cache_t *cache = cached ? (pair_pos_cache_t *) c->lookup_accel->cache : nullptr; +#endif + hb_buffer_t *buffer = c->buffer; +#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE + unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint, cache ? &cache->coverage : nullptr); +#else unsigned int index = (this+coverage).get_coverage (buffer->cur().codepoint); +#endif if (likely (index == NOT_COVERED)) return_trace (false); hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input; @@ -176,7 +190,6 @@ struct PairPosFormat2_4 : ValueBase } #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE - pair_pos_cache_t *cache = cached ? (pair_pos_cache_t *) c->lookup_accel->cache : nullptr; unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint, cache ? &cache->first : nullptr); unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint, cache ? &cache->second : nullptr); #else diff --git a/src/OT/Layout/types.hh b/src/OT/Layout/types.hh index 3840db059..527f64114 100644 --- a/src/OT/Layout/types.hh +++ b/src/OT/Layout/types.hh @@ -29,6 +29,9 @@ #ifndef OT_LAYOUT_TYPES_HH #define OT_LAYOUT_TYPES_HH +using hb_ot_lookup_cache_t = hb_cache_t<15, 8, 7>; +static_assert (sizeof (hb_ot_lookup_cache_t) == 256, ""); + namespace OT { namespace Layout { diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 54c1f82dd..593b51b9f 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -48,9 +48,6 @@ using OT::Layout::MediumTypes; namespace OT { -using hb_ot_class_cache_t = hb_cache_t<15, 8, 7>; -static_assert (sizeof (hb_ot_class_cache_t) == 256, ""); - template static inline bool ClassDef_serialize (hb_serialize_context_t *c, Iterator it); @@ -2081,7 +2078,7 @@ struct ClassDef } } unsigned int get_class (hb_codepoint_t glyph_id, - hb_ot_class_cache_t *cache) const + hb_ot_lookup_cache_t *cache) const { unsigned klass; if (cache && cache->get (glyph_id, &klass)) return klass;