From ffc6899b0cb788304d2ee7a8c415c3be8d04691b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 5 Jun 2023 14:16:47 -0600 Subject: [PATCH] [subset/cff1] Use a vector, instead of map, for glyph_to_sid_map Much faster. --- src/hb-ot-cff1-table.hh | 20 ++++++++++++-------- src/hb-ot-cff2-table.hh | 2 +- src/hb-subset-cff-common.hh | 12 +++++++++--- src/hb-subset-cff1.cc | 13 ++++++++----- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/hb-ot-cff1-table.hh b/src/hb-ot-cff1-table.hh index 6ccb2e242..20e6ad8bc 100644 --- a/src/hb-ot-cff1-table.hh +++ b/src/hb-ot-cff1-table.hh @@ -329,10 +329,11 @@ struct Charset0 return sids[glyph - 1]; } - void collect_glyph_to_sid_map (hb_map_t *mapping, unsigned int num_glyphs) const + void collect_glyph_to_sid_map (hb_vector_t *mapping, unsigned int num_glyphs) const { + mapping->resize (num_glyphs, false); for (hb_codepoint_t gid = 1; gid < num_glyphs; gid++) - mapping->set (gid, sids[gid - 1]); + mapping->arrayZ[gid] = sids[gid - 1]; } hb_codepoint_t get_glyph (hb_codepoint_t sid, unsigned int num_glyphs) const @@ -405,8 +406,9 @@ struct Charset1_2 { return 0; } - void collect_glyph_to_sid_map (hb_map_t *mapping, unsigned int num_glyphs) const + void collect_glyph_to_sid_map (hb_vector_t *mapping, unsigned int num_glyphs) const { + mapping->resize (num_glyphs, false); hb_codepoint_t gid = 1; if (gid >= num_glyphs) return; @@ -415,7 +417,7 @@ struct Charset1_2 { hb_codepoint_t sid = ranges[i].first; unsigned count = ranges[i].nLeft + 1; for (unsigned j = 0; j < count; j++) - mapping->set (gid++, sid++); + mapping->arrayZ[gid++] = sid++; if (gid >= num_glyphs) break; @@ -562,7 +564,7 @@ struct Charset } } - void collect_glyph_to_sid_map (hb_map_t *mapping, unsigned int num_glyphs) const + void collect_glyph_to_sid_map (hb_vector_t *mapping, unsigned int num_glyphs) const { switch (format) { @@ -1233,12 +1235,14 @@ struct cff1 } } - hb_map_t *create_glyph_to_sid_map () const + hb_vector_t *create_glyph_to_sid_map () const { if (charset != &Null (Charset)) { - hb_map_t *mapping = hb_map_create (); - mapping->set (0, 0); + auto *mapping = (hb_vector_t *) hb_malloc (sizeof (hb_vector_t)); + if (unlikely (!mapping)) return nullptr; + mapping = new (mapping) hb_vector_t (); + mapping->push (0); charset->collect_glyph_to_sid_map (mapping, num_glyphs); return mapping; } diff --git a/src/hb-ot-cff2-table.hh b/src/hb-ot-cff2-table.hh index 2134d4866..6a45c3b97 100644 --- a/src/hb-ot-cff2-table.hh +++ b/src/hb-ot-cff2-table.hh @@ -481,7 +481,7 @@ struct cff2 blob = nullptr; } - hb_map_t *create_glyph_to_sid_map () const + hb_vector_t *create_glyph_to_sid_map () const { return nullptr; } diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 3347bb941..99e89f781 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -510,15 +510,21 @@ struct cff_subset_accelerator_t original_blob = hb_blob_reference (original_blob_); } - ~cff_subset_accelerator_t() { + ~cff_subset_accelerator_t() + { hb_blob_destroy (original_blob); - hb_map_destroy (glyph_to_sid_map.get_relaxed ()); + auto *mapping = glyph_to_sid_map.get_relaxed (); + if (mapping) + { + mapping->~hb_vector_t (); + hb_free (mapping); + } } 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; + 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 e3180a662..f8b7591eb 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -481,9 +481,9 @@ struct cff_subset_plan { return; } - hb_map_t *glyph_to_sid_map = (plan->accelerator && plan->accelerator->cff_accelerator) ? - plan->accelerator->cff_accelerator->glyph_to_sid_map : - nullptr; + hb_vector_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) || @@ -512,7 +512,7 @@ struct cff_subset_plan { /* Retain the SID for the old missing glyph ID */ old_glyph = glyph; } - sid = glyph_to_sid_map ? glyph_to_sid_map->get (old_glyph) : acc.glyph_to_sid (old_glyph); + sid = glyph_to_sid_map ? glyph_to_sid_map->arrayZ[old_glyph] : acc.glyph_to_sid (old_glyph); if (not_is_cid) sid = sidmap.add (sid); @@ -529,7 +529,10 @@ struct cff_subset_plan { { 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); + { + glyph_to_sid_map->~hb_vector_t (); + hb_free (glyph_to_sid_map); + } } bool two_byte = subset_charset_ranges.complete (glyph);