From 00f2657bb8fea82613d67a059dd4c3a5550683f1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 29 Nov 2022 13:49:15 -0700 Subject: [PATCH] [subset] Accelerate sanitize-table-cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Big wins all across small subsets BM_subset/subset_codepoints/Roboto-Regular.ttf/nohinting/10                              -0.1140         -0.1129             0             0             0             0 BM_subset/subset_codepoints/Amiri-Regular.ttf/nohinting/10                               -0.4717         -0.4714             0             0             0             0 BM_subset/subset_codepoints/NotoNastaliqUrdu-Regular.ttf/nohinting/10                    -0.8147         -0.8146             0             0             0             0 BM_subset/subset_codepoints/NotoSansDevanagari-Regular.ttf/nohinting/10                  -0.3248         -0.3242             0             0             0             0 BM_subset/subset_codepoints/Mplus1p-Regular.ttf/nohinting/10                             -0.1262         -0.1260             0             0             0             0 BM_subset/subset_codepoints/SourceHanSans-Regular_subset.otf/nohinting/10                -0.0308         -0.0309             0             0             0             0 BM_subset/subset_codepoints/SourceSansPro-Regular.otf/nohinting/10                       -0.1374         -0.1373             0             0             0             0 BM_subset/subset_codepoints/AdobeVFPrototype.otf/nohinting/10                            -0.4555         -0.4555             0             0             0             0 BM_subset/subset_codepoints/MPLUS1-Variable.ttf/nohinting/10                             -0.4175         -0.4174             0             0             0             0 BM_subset/subset_codepoints/RobotoFlex-Variable.ttf/nohinting/10                         -0.4214         -0.4214             0             0             0 --- src/hb-mutex.hh | 7 ++++--- src/hb-subset-accelerator.hh | 12 ++++++++++-- src/hb-subset-plan.hh | 16 +++++++++------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/hb-mutex.hh b/src/hb-mutex.hh index 6914b2245..053f9ddcc 100644 --- a/src/hb-mutex.hh +++ b/src/hb-mutex.hh @@ -108,10 +108,11 @@ struct hb_mutex_t struct hb_lock_t { - hb_lock_t (hb_mutex_t &mutex_) : mutex (mutex_) { mutex.lock (); } - ~hb_lock_t () { mutex.unlock (); } + hb_lock_t (hb_mutex_t &mutex_) : mutex (&mutex_) { mutex->lock (); } + hb_lock_t (hb_mutex_t *mutex_) : mutex (mutex_) { if (mutex) mutex->lock (); } + ~hb_lock_t () { if (mutex) mutex->unlock (); } private: - hb_mutex_t &mutex; + hb_mutex_t *mutex; }; diff --git a/src/hb-subset-accelerator.hh b/src/hb-subset-accelerator.hh index 3158ad76c..37c7fa137 100644 --- a/src/hb-subset-accelerator.hh +++ b/src/hb-subset-accelerator.hh @@ -75,17 +75,25 @@ struct hb_subset_accelerator_t hb_free (accel); } - hb_subset_accelerator_t(const hb_map_t& unicode_to_gid_, + hb_subset_accelerator_t (const hb_map_t& unicode_to_gid_, const hb_set_t& unicodes_) : unicode_to_gid(unicode_to_gid_), unicodes(unicodes_), cmap_cache(nullptr), destroy_cmap_cache(nullptr), has_seac(false), cff_accelerator(nullptr), destroy_cff_accelerator(nullptr) + { sanitized_table_cache_lock.init (); } - {} + ~hb_subset_accelerator_t () + { sanitized_table_cache_lock.fini (); } // Generic + + mutable hb_mutex_t sanitized_table_cache_lock; + mutable hb_hashmap_t> sanitized_table_cache; + const hb_map_t unicode_to_gid; const hb_set_t unicodes; + + // cmap OT::SubtableUnicodesCache* cmap_cache; hb_destroy_func_t destroy_cmap_cache; diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index e6cccac40..1dea7aad9 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -219,18 +219,20 @@ struct hb_subset_plan_t template hb_blob_ptr_t source_table() { - if (sanitized_table_cache - && !sanitized_table_cache->in_error () - && sanitized_table_cache->has (+T::tableTag)) { - return hb_blob_reference (sanitized_table_cache->get (+T::tableTag).get ()); + hb_lock_t (accelerator ? &accelerator->sanitized_table_cache_lock : nullptr); + + auto *cache = accelerator ? &accelerator->sanitized_table_cache : sanitized_table_cache; + if (cache + && !cache->in_error () + && cache->has (+T::tableTag)) { + return hb_blob_reference (cache->get (+T::tableTag).get ()); } hb::unique_ptr table_blob {hb_sanitize_context_t ().reference_table (source)}; hb_blob_t* ret = hb_blob_reference (table_blob.get ()); - if (likely (sanitized_table_cache)) - sanitized_table_cache->set (+T::tableTag, - std::move (table_blob)); + if (likely (cache)) + cache->set (+T::tableTag, std::move (table_blob)); return ret; }