From 32dd9810cf156b7710bc849030d69b902e58077b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 30 Nov 2022 13:15:58 -0700 Subject: [PATCH] [subset-cff1] Cache glyph-to-sid-map in the accelerator Benchmark Time CPU Time Old Time New CPU Old CPU New --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- BM_subset/subset_codepoints/SourceHanSans-Regular_subset.otf/nohinting/10 -0.0841 -0.0843 0 0 0 0 BM_subset/subset_codepoints/SourceHanSans-Regular_subset.otf/nohinting/64 -0.1305 -0.1305 0 0 0 0 BM_subset/subset_codepoints/SourceHanSans-Regular_subset.otf/nohinting/512 -0.1398 -0.1401 1 1 1 1 BM_subset/subset_codepoints/SourceHanSans-Regular_subset.otf/nohinting/4096 +0.0382 +0.0380 9 9 9 9 BM_subset/subset_codepoints/SourceHanSans-Regular_subset.otf/nohinting/10000 +0.0213 +0.0211 11 11 11 11 --- src/hb-ot-cff2-table.hh | 5 +++++ src/hb-subset-cff-common.hh | 2 ++ src/hb-subset-cff1.cc | 21 +++++++++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/hb-ot-cff2-table.hh b/src/hb-ot-cff2-table.hh index e84709fa7..9081930bb 100644 --- a/src/hb-ot-cff2-table.hh +++ b/src/hb-ot-cff2-table.hh @@ -483,6 +483,11 @@ struct cff2 blob = nullptr; } + hb_map_t *create_glyph_to_sid_map () const + { + return nullptr; + } + bool is_valid () const { return blob; } protected: diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index a49f8d9f0..d724bd65e 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -433,11 +433,13 @@ struct cff_subset_accelerator_t ~cff_subset_accelerator_t() { hb_blob_destroy (original_blob); + hb_map_destroy (glyph_to_sid_map.get_relaxed ()); } parsed_cs_str_vec_t parsed_charstrings; parsed_cs_str_vec_t parsed_global_subrs; hb_vector_t parsed_local_subrs; + mutable hb_atomic_ptr_t glyph_to_sid_map = nullptr; private: hb_blob_t* original_blob; diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index fd169b21b..d46e102f2 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -443,8 +443,17 @@ struct cff_subset_plan { return; } - bool use_glyph_to_sid_map = plan->num_output_glyphs () > plan->source->get_num_glyphs () / 8.; - hb_map_t *glyph_to_sid_map = use_glyph_to_sid_map ? acc.create_glyph_to_sid_map () : nullptr; + hb_map_t *glyph_to_sid_map = (plan->accelerator && plan->accelerator->cff_accelerator) ? + plan->accelerator->cff_accelerator->glyph_to_sid_map : + nullptr; + bool created_map = false; + if (!glyph_to_sid_map && + ((plan->accelerator && plan->accelerator->cff_accelerator) || + plan->num_output_glyphs () > plan->source->get_num_glyphs () / 8.)) + { + created_map = true; + glyph_to_sid_map = acc.create_glyph_to_sid_map (); + } unsigned int glyph; for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++) @@ -468,8 +477,12 @@ struct cff_subset_plan { last_sid = sid; } - if (glyph_to_sid_map) - hb_map_destroy (glyph_to_sid_map); + if (created_map) + { + if (!(plan->accelerator && plan->accelerator->cff_accelerator) || + !plan->accelerator->cff_accelerator->glyph_to_sid_map.cmpexch (nullptr, glyph_to_sid_map)) + hb_map_destroy (glyph_to_sid_map); + } bool two_byte = subset_charset_ranges.complete (glyph);