From 088133d939d8bc4ce3d97eed7d835c1831e68766 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Mon, 2 May 2022 21:29:16 +0000 Subject: [PATCH 1/6] [subset] cache cp to new gid list in subset plan. This avoids having to recompute the ordered list multiple times during cmap generation. --- src/hb-ot-cmap-table.hh | 9 +-------- src/hb-subset-plan.cc | 30 ++++++++++++++++++++++++++++++ src/hb-subset-plan.hh | 1 + 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/hb-ot-cmap-table.hh b/src/hb-ot-cmap-table.hh index b36f40b6b..8ae05cc9c 100644 --- a/src/hb-ot-cmap-table.hh +++ b/src/hb-ot-cmap-table.hh @@ -332,7 +332,6 @@ struct CmapSubtableFormat4 if (unlikely (!c->extend_min (this))) return; this->format = 4; - // TODO(grieger): does pre-alloc make this faster? hb_vector_t> cp_to_gid { format4_iter }; @@ -1664,13 +1663,7 @@ struct cmap if (unlikely (has_format12 && (!unicode_ucs4 && !ms_ucs4))) return_trace (false); auto it = - + hb_iter (c->plan->unicodes) - | hb_map ([&] (hb_codepoint_t _) - { - hb_codepoint_t new_gid = HB_MAP_VALUE_INVALID; - c->plan->new_gid_for_codepoint (_, &new_gid); - return hb_pair_t (_, new_gid); - }) + + c->plan->unicode_to_new_gid_list->iter () | hb_filter ([&] (const hb_pair_t _) { return (_.second != HB_MAP_VALUE_INVALID); }) ; diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 74b7e3977..401a1dd4b 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -287,6 +287,14 @@ _remove_invalid_gids (hb_set_t *glyphs, } } +static inline int +_compare_cp_gid_pair (const void* a, + const void* b) +{ + return ((hb_pair_t*)a)->first - + ((hb_pair_t*)b)->first; +} + static void _populate_unicodes_to_retain (const hb_set_t *unicodes, const hb_set_t *glyphs, @@ -300,6 +308,7 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, { /* This is the fast path if it's anticipated that size of unicodes * is << than the number of codepoints in the font. */ + plan->unicode_to_new_gid_list->alloc (unicodes->get_population ()); for (hb_codepoint_t cp : *unicodes) { hb_codepoint_t gid; @@ -310,12 +319,14 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, } plan->codepoint_to_glyph->set (cp, gid); + plan->unicode_to_new_gid_list->push (hb_pair (cp, gid)); } } else { hb_map_t unicode_glyphid_map; cmap.collect_mapping (hb_set_get_empty (), &unicode_glyphid_map); + plan->unicode_to_new_gid_list->alloc (unicode_glyphid_map.get_population ()); for (hb_pair_t cp_gid : + unicode_glyphid_map.iter ()) @@ -324,8 +335,11 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, continue; plan->codepoint_to_glyph->set (cp_gid.first, cp_gid.second); + plan->unicode_to_new_gid_list->push (hb_pair (cp_gid.first, cp_gid.second)); } + plan->unicode_to_new_gid_list->qsort (_compare_cp_gid_pair); + /* Add gids which where requested, but not mapped in cmap */ // TODO(garretrieger): // Once https://github.com/harfbuzz/harfbuzz/issues/3169 @@ -485,6 +499,10 @@ hb_subset_plan_create_or_fail (hb_face_t *face, plan->successful = true; plan->flags = input->flags; plan->unicodes = hb_set_create (); + plan->unicode_to_new_gid_list = + (hb_vector_t>*) + hb_calloc (1, sizeof(hb_vector_t>)); + plan->unicode_to_new_gid_list->init (); plan->name_ids = hb_set_copy (input->sets.name_ids); _nameid_closure (face, plan->name_ids); plan->name_languages = hb_set_copy (input->sets.name_languages); @@ -536,6 +554,13 @@ hb_subset_plan_create_or_fail (hb_face_t *face, plan->reverse_glyph_map, &plan->_num_output_glyphs); + // Now that we have old to new gid map update the unicode to new gid list. + for (unsigned i = 0; i < plan->unicode_to_new_gid_list->length; i++) + { + plan->unicode_to_new_gid_list->arrayZ[i].second = + plan->glyph_map->get(plan->unicode_to_new_gid_list->arrayZ[i].second); + } + if (unlikely (plan->in_error ())) { hb_subset_plan_destroy (plan); return nullptr; @@ -558,6 +583,11 @@ hb_subset_plan_destroy (hb_subset_plan_t *plan) if (!hb_object_destroy (plan)) return; hb_set_destroy (plan->unicodes); + if (plan->unicode_to_new_gid_list) + { + plan->unicode_to_new_gid_list->fini (); + hb_free (plan->unicode_to_new_gid_list); + } hb_set_destroy (plan->name_ids); hb_set_destroy (plan->name_languages); hb_set_destroy (plan->layout_features); diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index ab2c4c302..b58c55c2a 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -44,6 +44,7 @@ struct hb_subset_plan_t // For each cp that we'd like to retain maps to the corresponding gid. hb_set_t *unicodes; + hb_vector_t>* unicode_to_new_gid_list; // name_ids we would like to retain hb_set_t *name_ids; From 85ec5cbcefeb2361536031f2e05518c2d817d98a Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Mon, 2 May 2022 22:29:43 +0000 Subject: [PATCH 2/6] [subset] In _populate_unicodes_to_retain populate unicodes in order. Allows the set insert to take advantage of page lookup cache. --- src/hb-subset-plan.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 401a1dd4b..e1b7583be 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -352,8 +352,12 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, } } - + plan->codepoint_to_glyph->keys () | hb_sink (plan->unicodes); - + plan->codepoint_to_glyph->values () | hb_sink (plan->_glyphset_gsub); + for (unsigned i = 0; i < plan->unicode_to_new_gid_list->length; i++) + { + hb_pair_t pair = plan->unicode_to_new_gid_list->arrayZ[i]; + plan->unicodes->add(pair.first); + plan->_glyphset_gsub->add(pair.second); + } } static void From f0c04114bc229b3b519ed2242689959ccec64098 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 3 May 2022 22:02:59 +0000 Subject: [PATCH 3/6] [subset] Embed unicode to gid list vector in subset plan. --- src/hb-ot-cmap-table.hh | 2 +- src/hb-subset-plan.cc | 35 ++++++++++++++++------------------- src/hb-subset-plan.hh | 2 +- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/hb-ot-cmap-table.hh b/src/hb-ot-cmap-table.hh index 8ae05cc9c..f4529b75c 100644 --- a/src/hb-ot-cmap-table.hh +++ b/src/hb-ot-cmap-table.hh @@ -1663,7 +1663,7 @@ struct cmap if (unlikely (has_format12 && (!unicode_ucs4 && !ms_ucs4))) return_trace (false); auto it = - + c->plan->unicode_to_new_gid_list->iter () + + c->plan->unicode_to_new_gid_list.iter () | hb_filter ([&] (const hb_pair_t _) { return (_.second != HB_MAP_VALUE_INVALID); }) ; diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index e1b7583be..8de3c69f3 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -308,7 +308,7 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, { /* This is the fast path if it's anticipated that size of unicodes * is << than the number of codepoints in the font. */ - plan->unicode_to_new_gid_list->alloc (unicodes->get_population ()); + plan->unicode_to_new_gid_list.alloc (unicodes->get_population ()); for (hb_codepoint_t cp : *unicodes) { hb_codepoint_t gid; @@ -319,14 +319,14 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, } plan->codepoint_to_glyph->set (cp, gid); - plan->unicode_to_new_gid_list->push (hb_pair (cp, gid)); + plan->unicode_to_new_gid_list.push (hb_pair (cp, gid)); } } else { hb_map_t unicode_glyphid_map; cmap.collect_mapping (hb_set_get_empty (), &unicode_glyphid_map); - plan->unicode_to_new_gid_list->alloc (unicode_glyphid_map.get_population ()); + plan->unicode_to_new_gid_list.alloc (unicode_glyphid_map.get_population ()); for (hb_pair_t cp_gid : + unicode_glyphid_map.iter ()) @@ -335,10 +335,10 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, continue; plan->codepoint_to_glyph->set (cp_gid.first, cp_gid.second); - plan->unicode_to_new_gid_list->push (hb_pair (cp_gid.first, cp_gid.second)); + plan->unicode_to_new_gid_list.push (hb_pair (cp_gid.first, cp_gid.second)); } - plan->unicode_to_new_gid_list->qsort (_compare_cp_gid_pair); + plan->unicode_to_new_gid_list.qsort (_compare_cp_gid_pair); /* Add gids which where requested, but not mapped in cmap */ // TODO(garretrieger): @@ -352,9 +352,10 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, } } - for (unsigned i = 0; i < plan->unicode_to_new_gid_list->length; i++) + for (unsigned i = 0; i < plan->unicode_to_new_gid_list.length; i++) { - hb_pair_t pair = plan->unicode_to_new_gid_list->arrayZ[i]; + // Use raw array access for performance. + hb_pair_t pair = plan->unicode_to_new_gid_list.arrayZ[i]; plan->unicodes->add(pair.first); plan->_glyphset_gsub->add(pair.second); } @@ -503,10 +504,9 @@ hb_subset_plan_create_or_fail (hb_face_t *face, plan->successful = true; plan->flags = input->flags; plan->unicodes = hb_set_create (); - plan->unicode_to_new_gid_list = - (hb_vector_t>*) - hb_calloc (1, sizeof(hb_vector_t>)); - plan->unicode_to_new_gid_list->init (); + + plan->unicode_to_new_gid_list.init (); + plan->name_ids = hb_set_copy (input->sets.name_ids); _nameid_closure (face, plan->name_ids); plan->name_languages = hb_set_copy (input->sets.name_languages); @@ -559,10 +559,11 @@ hb_subset_plan_create_or_fail (hb_face_t *face, &plan->_num_output_glyphs); // Now that we have old to new gid map update the unicode to new gid list. - for (unsigned i = 0; i < plan->unicode_to_new_gid_list->length; i++) + for (unsigned i = 0; i < plan->unicode_to_new_gid_list.length; i++) { - plan->unicode_to_new_gid_list->arrayZ[i].second = - plan->glyph_map->get(plan->unicode_to_new_gid_list->arrayZ[i].second); + // Use raw array access for performance. + plan->unicode_to_new_gid_list.arrayZ[i].second = + plan->glyph_map->get(plan->unicode_to_new_gid_list.arrayZ[i].second); } if (unlikely (plan->in_error ())) { @@ -587,11 +588,7 @@ hb_subset_plan_destroy (hb_subset_plan_t *plan) if (!hb_object_destroy (plan)) return; hb_set_destroy (plan->unicodes); - if (plan->unicode_to_new_gid_list) - { - plan->unicode_to_new_gid_list->fini (); - hb_free (plan->unicode_to_new_gid_list); - } + plan->unicode_to_new_gid_list.fini (); hb_set_destroy (plan->name_ids); hb_set_destroy (plan->name_languages); hb_set_destroy (plan->layout_features); diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index b58c55c2a..0f20d74db 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -44,7 +44,7 @@ struct hb_subset_plan_t // For each cp that we'd like to retain maps to the corresponding gid. hb_set_t *unicodes; - hb_vector_t>* unicode_to_new_gid_list; + hb_vector_t> unicode_to_new_gid_list; // name_ids we would like to retain hb_set_t *name_ids; From 7c7c01d28cee221f1c64684539c8e6160f144f61 Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Tue, 3 May 2022 22:40:56 +0000 Subject: [PATCH 4/6] [subset] Remove switch to alternate unicode collection at large subset sizes. Benchmarks show that the first path is always faster even at large subset sizes: BM_subset_codepoints/subset_roboto/10_median +0.0324 +0.0325 0 0 0 0 BM_subset_codepoints/subset_roboto/64_median +0.0253 +0.0255 0 1 0 1 BM_subset_codepoints/subset_roboto/512_median +0.0126 +0.0128 1 1 1 1 BM_subset_codepoints/subset_roboto/4000_median +0.0500 +0.0491 6 7 6 7 BM_subset_codepoints/subset_amiri/10_median +0.0338 +0.0332 1 1 1 1 BM_subset_codepoints/subset_amiri/64_median +0.0238 +0.0234 1 1 1 1 BM_subset_codepoints/subset_amiri/512_median +0.0066 +0.0063 8 8 8 8 BM_subset_codepoints/subset_amiri/4000_median -0.0011 -0.0012 13 13 13 13 BM_subset_codepoints/subset_noto_nastaliq_urdu/10_median +0.0226 +0.0226 0 0 0 0 BM_subset_codepoints/subset_noto_nastaliq_urdu/64_median +0.0047 +0.0044 20 20 20 20 BM_subset_codepoints/subset_noto_nastaliq_urdu/512_median +0.0022 +0.0021 165 166 165 166 BM_subset_codepoints/subset_noto_nastaliq_urdu/1000_median -0.0021 -0.0023 166 166 166 165 BM_subset_codepoints/subset_noto_devangari/10_median +0.0054 +0.0054 0 0 0 0 BM_subset_codepoints/subset_noto_devangari/64_median +0.0024 +0.0019 0 0 0 0 BM_subset_codepoints/subset_noto_devangari/512_median +0.0089 +0.0090 5 5 5 5 BM_subset_codepoints/subset_noto_devangari/1000_median -0.0028 -0.0019 5 5 5 5 BM_subset_codepoints/subset_mplus1p/10_median +0.0001 +0.0002 0 0 0 0 BM_subset_codepoints/subset_mplus1p/64_median +0.0073 +0.0075 1 1 1 1 BM_subset_codepoints/subset_mplus1p/512_median +0.0034 +0.0034 1 1 1 1 BM_subset_codepoints/subset_mplus1p/4096_median -0.1248 -0.1248 7 6 7 6 BM_subset_codepoints/subset_mplus1p/10000_median -0.0885 -0.0885 13 12 13 12 BM_subset_codepoints/subset_notocjk/10_median +0.0031 +0.0032 2 2 2 2 BM_subset_codepoints/subset_notocjk/64_median -0.0010 -0.0010 2 2 2 2 BM_subset_codepoints/subset_notocjk/512_median -0.0023 -0.0023 9 9 9 9 BM_subset_codepoints/subset_notocjk/4096_median -0.1725 -0.1726 28 23 28 23 BM_subset_codepoints/subset_notocjk/32768_median -0.0277 -0.0287 140 137 140 136 BM_subset_codepoints/subset_notocjk/100000_median -0.0929 -0.0926 162 147 162 147 --- src/hb-subset-plan.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 8de3c69f3..6deab6dd9 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -302,12 +302,10 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, { OT::cmap::accelerator_t cmap (plan->source); - constexpr static const int size_threshold = 4096; - - if (glyphs->is_empty () && unicodes->get_population () < size_threshold) + if (glyphs->is_empty ()) { - /* This is the fast path if it's anticipated that size of unicodes - * is << than the number of codepoints in the font. */ + // This is approach to collection is faster, but can only be used if glyphs + // are not being explicitly added to the subset. plan->unicode_to_new_gid_list.alloc (unicodes->get_population ()); for (hb_codepoint_t cp : *unicodes) { @@ -324,6 +322,8 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, } else { + // This approach is slower, but can handle adding in glyphs to the subset and will match + // them with cmap entries. hb_map_t unicode_glyphid_map; cmap.collect_mapping (hb_set_get_empty (), &unicode_glyphid_map); plan->unicode_to_new_gid_list.alloc (unicode_glyphid_map.get_population ()); From 42c54eba839f510c885fe1a63732b0f706af1bff Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Wed, 4 May 2022 20:21:43 +0000 Subject: [PATCH 5/6] [subset] Presize unicode to gid list to unicodes + glyphs size. --- src/hb-subset-plan.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 6deab6dd9..5a9ad71d8 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -326,7 +326,8 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, // them with cmap entries. hb_map_t unicode_glyphid_map; cmap.collect_mapping (hb_set_get_empty (), &unicode_glyphid_map); - plan->unicode_to_new_gid_list.alloc (unicode_glyphid_map.get_population ()); + plan->unicode_to_new_gid_list.alloc (unicodes->get_population () + + glyphs->get_population ()); for (hb_pair_t cp_gid : + unicode_glyphid_map.iter ()) From 7cb36e422218305329102849c156ab94db91cbef Mon Sep 17 00:00:00 2001 From: Garret Rieger Date: Wed, 4 May 2022 21:22:26 +0000 Subject: [PATCH 6/6] [subset] Re-introduce size threshold in choosing unicode collection method. Threshold is needed since the unicodes set might be an inverted set. --- src/hb-subset-plan.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 5a9ad71d8..4cff60e54 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -302,10 +302,12 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, { OT::cmap::accelerator_t cmap (plan->source); - if (glyphs->is_empty ()) + unsigned size_threshold = plan->source->get_num_glyphs (); + if (glyphs->is_empty () && unicodes->get_population () < size_threshold) { // This is approach to collection is faster, but can only be used if glyphs - // are not being explicitly added to the subset. + // are not being explicitly added to the subset and the input unicodes set is + // not excessively large (eg. an inverted set). plan->unicode_to_new_gid_list.alloc (unicodes->get_population ()); for (hb_codepoint_t cp : *unicodes) { @@ -326,8 +328,10 @@ _populate_unicodes_to_retain (const hb_set_t *unicodes, // them with cmap entries. hb_map_t unicode_glyphid_map; cmap.collect_mapping (hb_set_get_empty (), &unicode_glyphid_map); - plan->unicode_to_new_gid_list.alloc (unicodes->get_population () - + glyphs->get_population ()); + plan->unicode_to_new_gid_list.alloc (hb_min(unicodes->get_population () + + glyphs->get_population (), + unicode_glyphid_map.get_population ())); + for (hb_pair_t cp_gid : + unicode_glyphid_map.iter ())