From 3f40edbfa09735243b9b5cb62492b04a9c450357 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 5 Feb 2025 17:25:27 +0000 Subject: [PATCH 1/5] [ClassDef] Add ability to use a cache --- src/hb-ot-layout-common.hh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 757b05031..54c1f82dd 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -34,6 +34,7 @@ #include "hb-open-type.hh" #include "hb-set.hh" #include "hb-bimap.hh" +#include "hb-cache.hh" #include "OT/Layout/Common/Coverage.hh" #include "OT/Layout/types.hh" @@ -47,6 +48,9 @@ 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); @@ -2076,6 +2080,15 @@ struct ClassDef default:return 0; } } + unsigned int get_class (hb_codepoint_t glyph_id, + hb_ot_class_cache_t *cache) const + { + unsigned klass; + if (cache && cache->get (glyph_id, &klass)) return klass; + klass = get_class (glyph_id); + if (cache) cache->set (glyph_id, klass); + return klass; + } unsigned get_population () const { From acd122d030c83bb7f37495adf82140be0321f341 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 5 Feb 2025 17:38:39 +0000 Subject: [PATCH 2/5] [gsubgpos] Prepare for per-lookup allocated caches --- src/hb-ot-layout-gsubgpos.hh | 107 ++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 39 deletions(-) diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 966fa06c1..e5257a0a4 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -899,6 +899,13 @@ struct hb_ot_apply_context_t : } }; +enum class hb_ot_lookup_cache_op_t +{ + CREATE, + ENTER, + LEAVE, + DESTROY, +}; struct hb_accelerate_subtables_context_t : hb_dispatch_context_t @@ -923,19 +930,27 @@ struct hb_accelerate_subtables_context_t : } template - static inline auto cache_func_ (const T *obj, hb_ot_apply_context_t *c, bool enter, hb_priority<1>) HB_RETURN (bool, obj->cache_func (c, enter) ) + static inline auto cache_func_ (const T *obj, + hb_ot_apply_context_t *c, + hb_ot_lookup_cache_op_t op, + hb_priority<1>) HB_RETURN (void *, obj->cache_func (c, op) ) template - static inline bool cache_func_ (const T *obj, hb_ot_apply_context_t *c, bool enter, hb_priority<0>) { return false; } + static inline void * cache_func_ (const T *obj, + hb_ot_apply_context_t *c, + hb_ot_lookup_cache_op_t op HB_UNUSED, + hb_priority<0>) { return (void *) false; } template - static inline bool cache_func_to (const void *obj, hb_ot_apply_context_t *c, bool enter) + static inline void * cache_func_to (const void *obj, + hb_ot_apply_context_t *c, + hb_ot_lookup_cache_op_t op) { const Type *typed_obj = (const Type *) obj; - return cache_func_ (typed_obj, c, enter, hb_prioritize); + return cache_func_ (typed_obj, c, op, hb_prioritize); } #endif typedef bool (*hb_apply_func_t) (const void *obj, hb_ot_apply_context_t *c); - typedef bool (*hb_cache_func_t) (const void *obj, hb_ot_apply_context_t *c, bool enter); + typedef void * (*hb_cache_func_t) (const void *obj, hb_ot_apply_context_t *c, hb_ot_lookup_cache_op_t op); struct hb_applicable_t { @@ -972,11 +987,11 @@ struct hb_accelerate_subtables_context_t : } bool cache_enter (hb_ot_apply_context_t *c) const { - return cache_func (obj, c, true); + return (bool) cache_func (obj, c, hb_ot_lookup_cache_op_t::ENTER); } void cache_leave (hb_ot_apply_context_t *c) const { - cache_func (obj, c, false); + cache_func (obj, c, hb_ot_lookup_cache_op_t::LEAVE); } #endif @@ -2623,24 +2638,31 @@ struct ContextFormat2_5 unsigned c = (this+classDef).cost () * ruleSet.len; return c >= 4 ? c : 0; } - bool cache_func (hb_ot_apply_context_t *c, bool enter) const + void * cache_func (hb_ot_apply_context_t *c, hb_ot_lookup_cache_op_t op) const { - if (enter) + switch (op) { - if (!HB_BUFFER_TRY_ALLOCATE_VAR (c->buffer, syllable)) - return false; - auto &info = c->buffer->info; - unsigned count = c->buffer->len; - for (unsigned i = 0; i < count; i++) - info[i].syllable() = 255; - c->new_syllables = 255; - return true; - } - else - { - c->new_syllables = (unsigned) -1; - HB_BUFFER_DEALLOCATE_VAR (c->buffer, syllable); - return true; + case hb_ot_lookup_cache_op_t::CREATE: + return (void *) true; + case hb_ot_lookup_cache_op_t::ENTER: + { + if (!HB_BUFFER_TRY_ALLOCATE_VAR (c->buffer, syllable)) + return (void *) false; + auto &info = c->buffer->info; + unsigned count = c->buffer->len; + for (unsigned i = 0; i < count; i++) + info[i].syllable() = 255; + c->new_syllables = 255; + return (void *) true; + } + case hb_ot_lookup_cache_op_t::LEAVE: + { + c->new_syllables = (unsigned) -1; + HB_BUFFER_DEALLOCATE_VAR (c->buffer, syllable); + return (void *) true; + } + case hb_ot_lookup_cache_op_t::DESTROY: + return nullptr; } } @@ -3864,24 +3886,31 @@ struct ChainContextFormat2_5 unsigned c = (this+lookaheadClassDef).cost () * ruleSet.len; return c >= 4 ? c : 0; } - bool cache_func (hb_ot_apply_context_t *c, bool enter) const + void * cache_func (hb_ot_apply_context_t *c, hb_ot_lookup_cache_op_t op) const { - if (enter) + switch (op) { - if (!HB_BUFFER_TRY_ALLOCATE_VAR (c->buffer, syllable)) - return false; - auto &info = c->buffer->info; - unsigned count = c->buffer->len; - for (unsigned i = 0; i < count; i++) - info[i].syllable() = 255; - c->new_syllables = 255; - return true; - } - else - { - c->new_syllables = (unsigned) -1; - HB_BUFFER_DEALLOCATE_VAR (c->buffer, syllable); - return true; + case hb_ot_lookup_cache_op_t::CREATE: + return (void *) true; + case hb_ot_lookup_cache_op_t::ENTER: + { + if (!HB_BUFFER_TRY_ALLOCATE_VAR (c->buffer, syllable)) + return (void *) false; + auto &info = c->buffer->info; + unsigned count = c->buffer->len; + for (unsigned i = 0; i < count; i++) + info[i].syllable() = 255; + c->new_syllables = 255; + return (void *) true; + } + case hb_ot_lookup_cache_op_t::LEAVE: + { + c->new_syllables = (unsigned) -1; + HB_BUFFER_DEALLOCATE_VAR (c->buffer, syllable); + return (void *) true; + } + case hb_ot_lookup_cache_op_t::DESTROY: + return nullptr; } } From 726af2e4e51df3b08bf365fd73f25bce0310da14 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 5 Feb 2025 18:10:40 +0000 Subject: [PATCH 3/5] [PairPos] Use a class cache 10% speedup on Roboto-Regular. 5% on SF Pro. --- src/OT/Layout/GPOS/PairPosFormat2.hh | 44 +++++++++++++++++++- src/hb-ot-layout-gsubgpos.hh | 60 ++++++++++++++++++++-------- 2 files changed, 86 insertions(+), 18 deletions(-) diff --git a/src/OT/Layout/GPOS/PairPosFormat2.hh b/src/OT/Layout/GPOS/PairPosFormat2.hh index 5ffeb5d0c..85acbcf54 100644 --- a/src/OT/Layout/GPOS/PairPosFormat2.hh +++ b/src/OT/Layout/GPOS/PairPosFormat2.hh @@ -123,7 +123,43 @@ struct PairPosFormat2_4 : ValueBase const Coverage &get_coverage () const { return this+coverage; } - bool apply (hb_ot_apply_context_t *c) const + using pair_pos_cache_t = hb_pair_t; + + unsigned cache_cost () const + { + unsigned c = (this+classDef1).cost () + (this+classDef2).cost (); + return c >= 4 ? c : 0; + } + static void * cache_func (void *p, hb_ot_lookup_cache_op_t op) + { + switch (op) + { + case hb_ot_lookup_cache_op_t::CREATE: + { + pair_pos_cache_t *cache = (pair_pos_cache_t *) hb_malloc (sizeof (pair_pos_cache_t)); + if (likely (cache)) + { + cache->first.clear (); + cache->second.clear (); + } + return cache; + } + case hb_ot_lookup_cache_op_t::ENTER: + return (void *) true; + case hb_ot_lookup_cache_op_t::LEAVE: + return nullptr; + case hb_ot_lookup_cache_op_t::DESTROY: + { + pair_pos_cache_t *cache = (pair_pos_cache_t *) p; + hb_free (cache); + return nullptr; + } + } + } + + bool apply_cached (hb_ot_apply_context_t *c) const { return _apply (c, true); } + bool apply (hb_ot_apply_context_t *c) const { return _apply (c, false); } + bool _apply (hb_ot_apply_context_t *c, bool cached) const { TRACE_APPLY (this); hb_buffer_t *buffer = c->buffer; @@ -139,8 +175,14 @@ struct PairPosFormat2_4 : ValueBase return_trace (false); } +#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 unsigned int klass1 = (this+classDef1).get_class (buffer->cur().codepoint); unsigned int klass2 = (this+classDef2).get_class (buffer->info[skippy_iter.idx].codepoint); +#endif if (unlikely (klass1 >= class1Count || klass2 >= class2Count)) { buffer->unsafe_to_concat (buffer->idx, skippy_iter.idx + 1); diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index e5257a0a4..03d849165 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -713,6 +713,7 @@ struct hb_ot_apply_context_t : recurse_func_t recurse_func = nullptr; const GDEF &gdef; const GDEF::accelerator_t &gdef_accel; + const hb_ot_layout_lookup_accelerator_t *lookup_accel = nullptr; const ItemVariationStore &var_store; ItemVariationStore::cache_t *var_store_cache; hb_set_digest_t digest; @@ -930,27 +931,23 @@ struct hb_accelerate_subtables_context_t : } template - static inline auto cache_func_ (const T *obj, - hb_ot_apply_context_t *c, + static inline auto cache_func_ (void *p, hb_ot_lookup_cache_op_t op, - hb_priority<1>) HB_RETURN (void *, obj->cache_func (c, op) ) - template - static inline void * cache_func_ (const T *obj, - hb_ot_apply_context_t *c, + hb_priority<1>) HB_RETURN (void *, T::cache_func (p, op) ) + template + static inline void * cache_func_ (void *p, hb_ot_lookup_cache_op_t op HB_UNUSED, hb_priority<0>) { return (void *) false; } template - static inline void * cache_func_to (const void *obj, - hb_ot_apply_context_t *c, + static inline void * cache_func_to (void *p, hb_ot_lookup_cache_op_t op) { - const Type *typed_obj = (const Type *) obj; - return cache_func_ (typed_obj, c, op, hb_prioritize); + return cache_func_ (p, op, hb_prioritize); } #endif typedef bool (*hb_apply_func_t) (const void *obj, hb_ot_apply_context_t *c); - typedef void * (*hb_cache_func_t) (const void *obj, hb_ot_apply_context_t *c, hb_ot_lookup_cache_op_t op); + typedef void * (*hb_cache_func_t) (void *p, hb_ot_lookup_cache_op_t op); struct hb_applicable_t { @@ -987,11 +984,11 @@ struct hb_accelerate_subtables_context_t : } bool cache_enter (hb_ot_apply_context_t *c) const { - return (bool) cache_func (obj, c, hb_ot_lookup_cache_op_t::ENTER); + return (bool) cache_func (c, hb_ot_lookup_cache_op_t::ENTER); } void cache_leave (hb_ot_apply_context_t *c) const { - cache_func (obj, c, hb_ot_lookup_cache_op_t::LEAVE); + cache_func (c, hb_ot_lookup_cache_op_t::LEAVE); } #endif @@ -2638,7 +2635,7 @@ struct ContextFormat2_5 unsigned c = (this+classDef).cost () * ruleSet.len; return c >= 4 ? c : 0; } - void * cache_func (hb_ot_apply_context_t *c, hb_ot_lookup_cache_op_t op) const + static void * cache_func (void *p, hb_ot_lookup_cache_op_t op) { switch (op) { @@ -2646,6 +2643,7 @@ struct ContextFormat2_5 return (void *) true; case hb_ot_lookup_cache_op_t::ENTER: { + hb_ot_apply_context_t *c = (hb_ot_apply_context_t *) p; if (!HB_BUFFER_TRY_ALLOCATE_VAR (c->buffer, syllable)) return (void *) false; auto &info = c->buffer->info; @@ -2657,9 +2655,10 @@ struct ContextFormat2_5 } case hb_ot_lookup_cache_op_t::LEAVE: { + hb_ot_apply_context_t *c = (hb_ot_apply_context_t *) p; c->new_syllables = (unsigned) -1; HB_BUFFER_DEALLOCATE_VAR (c->buffer, syllable); - return (void *) true; + return nullptr; } case hb_ot_lookup_cache_op_t::DESTROY: return nullptr; @@ -3886,7 +3885,7 @@ struct ChainContextFormat2_5 unsigned c = (this+lookaheadClassDef).cost () * ruleSet.len; return c >= 4 ? c : 0; } - void * cache_func (hb_ot_apply_context_t *c, hb_ot_lookup_cache_op_t op) const + static void * cache_func (void *p, hb_ot_lookup_cache_op_t op) { switch (op) { @@ -3894,6 +3893,7 @@ struct ChainContextFormat2_5 return (void *) true; case hb_ot_lookup_cache_op_t::ENTER: { + hb_ot_apply_context_t *c = (hb_ot_apply_context_t *) p; if (!HB_BUFFER_TRY_ALLOCATE_VAR (c->buffer, syllable)) return (void *) false; auto &info = c->buffer->info; @@ -3905,9 +3905,10 @@ struct ChainContextFormat2_5 } case hb_ot_lookup_cache_op_t::LEAVE: { + hb_ot_apply_context_t *c = (hb_ot_apply_context_t *) p; c->new_syllables = (unsigned) -1; HB_BUFFER_DEALLOCATE_VAR (c->buffer, syllable); - return (void *) true; + return nullptr; } case hb_ot_lookup_cache_op_t::DESTROY: return nullptr; @@ -4438,6 +4439,14 @@ struct hb_ot_layout_lookup_accelerator_t #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE thiz->cache_user_idx = c_accelerate_subtables.cache_user_idx; + + if (thiz->cache_user_idx != (unsigned) -1) + { + thiz->cache = thiz->subtables[thiz->cache_user_idx].cache_func (nullptr, hb_ot_lookup_cache_op_t::CREATE); + if (!thiz->cache) + thiz->cache_user_idx = (unsigned) -1; + } + for (unsigned i = 0; i < count; i++) if (i != thiz->cache_user_idx) thiz->subtables[i].apply_cached_func = thiz->subtables[i].apply_func; @@ -4446,6 +4455,17 @@ struct hb_ot_layout_lookup_accelerator_t return thiz; } + void fini () + { +#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE + if (cache) + { + assert (cache_user_idx != (unsigned) -1); + subtables[cache_user_idx].cache_func (cache, hb_ot_lookup_cache_op_t::DESTROY); + } +#endif + } + bool may_have (hb_codepoint_t g) const { return digest.may_have (g); } @@ -4454,6 +4474,7 @@ struct hb_ot_layout_lookup_accelerator_t #endif bool apply (hb_ot_apply_context_t *c, unsigned subtables_count, bool use_cache) const { + c->lookup_accel = this; #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE if (use_cache) { @@ -4493,6 +4514,7 @@ struct hb_ot_layout_lookup_accelerator_t hb_set_digest_t digest; + void *cache = nullptr; private: #ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE unsigned cache_user_idx = (unsigned) -1; @@ -4881,7 +4903,11 @@ struct GSUBGPOS ~accelerator_t () { for (unsigned int i = 0; i < this->lookup_count; i++) + { + if (this->accels[i]) + this->accels[i]->fini (); hb_free (this->accels[i]); + } hb_free (this->accels); this->table.destroy (); } From f22943a2a2f5bb8d3f81c6efe3af837645ded8bb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 5 Feb 2025 18:51:11 +0000 Subject: [PATCH 4/5] [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; From 5be62a9f66b0964854e972f9ec36ffc0e3374a60 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 5 Feb 2025 19:30:01 +0000 Subject: [PATCH 5/5] [PairPosFormat1] Use a coverage cache as well Speeds up Arial shaping by 3%. --- src/OT/Layout/GPOS/PairPosFormat1.hh | 43 ++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/OT/Layout/GPOS/PairPosFormat1.hh b/src/OT/Layout/GPOS/PairPosFormat1.hh index ac2774a76..c9a3de426 100644 --- a/src/OT/Layout/GPOS/PairPosFormat1.hh +++ b/src/OT/Layout/GPOS/PairPosFormat1.hh @@ -103,11 +103,50 @@ struct PairPosFormat1_3 const Coverage &get_coverage () const { return this+coverage; } - bool apply (hb_ot_apply_context_t *c) const + unsigned cache_cost () const + { + return (this+coverage).cost (); + } + static void * cache_func (void *p, hb_ot_lookup_cache_op_t op) + { + switch (op) + { + case hb_ot_lookup_cache_op_t::CREATE: + { + hb_ot_lookup_cache_t *cache = (hb_ot_lookup_cache_t *) hb_malloc (sizeof (hb_ot_lookup_cache_t)); + if (likely (cache)) + cache->clear (); + return cache; + } + case hb_ot_lookup_cache_op_t::ENTER: + return (void *) true; + case hb_ot_lookup_cache_op_t::LEAVE: + return nullptr; + case hb_ot_lookup_cache_op_t::DESTROY: + { + hb_ot_lookup_cache_t *cache = (hb_ot_lookup_cache_t *) p; + hb_free (cache); + return nullptr; + } + } + } + + bool apply_cached (hb_ot_apply_context_t *c) const { return _apply (c, true); } + bool apply (hb_ot_apply_context_t *c) const { return _apply (c, false); } + bool _apply (hb_ot_apply_context_t *c, bool cached) const { TRACE_APPLY (this); + +#ifndef HB_NO_OT_LAYOUT_LOOKUP_CACHE + hb_ot_lookup_cache_t *cache = cached ? (hb_ot_lookup_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); +#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; @@ -156,7 +195,7 @@ struct PairPosFormat1_3 strip = true; newFormats = compute_effective_value_formats (glyphset, strip, true); } - + out->valueFormat[0] = newFormats.first; out->valueFormat[1] = newFormats.second;