From 054f966a570ef37e0153b6591cbb2ff165517738 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 3 Jun 2023 22:52:16 -0600 Subject: [PATCH] [subset/cff1] Don't allocate memory for retaingid holes 40% speedup in BM_subset/subset_glyphs/SourceHanSans-Regular_subset.otf/retaingids/10 benchmark. --- src/hb-cff-interp-common.hh | 2 ++ src/hb-static.cc | 3 +++ src/hb-subset-cff-common.hh | 14 ++++++++++++-- src/hb-vector.hh | 10 ++++++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/hb-cff-interp-common.hh b/src/hb-cff-interp-common.hh index 949bfebf9..1cb342ad3 100644 --- a/src/hb-cff-interp-common.hh +++ b/src/hb-cff-interp-common.hh @@ -26,6 +26,8 @@ #ifndef HB_CFF_INTERP_COMMON_HH #define HB_CFF_INTERP_COMMON_HH +extern HB_INTERNAL const unsigned char *endchar_str; + namespace CFF { using namespace OT; diff --git a/src/hb-static.cc b/src/hb-static.cc index a1a2522ed..c9bd0a61b 100644 --- a/src/hb-static.cc +++ b/src/hb-static.cc @@ -31,6 +31,7 @@ #include "hb-aat-layout-common.hh" #include "hb-aat-layout-feat-table.hh" +#include "hb-cff-interp-common.hh" #include "hb-ot-layout-common.hh" #include "hb-ot-cmap-table.hh" #include "OT/Color/COLR/COLR.hh" @@ -58,6 +59,8 @@ DEFINE_NULL_NAMESPACE_BYTES (AAT, Lookup) = {0xFF,0xFF}; /* hb_map_t */ const hb_codepoint_t minus_1 = -1; +static const unsigned char static_endchar_str[] = {OpCode_endchar}; +const unsigned char *endchar_str = static_endchar_str; /* hb_face_t */ diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 23e95d454..e0d62a126 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -809,7 +809,12 @@ struct subr_subsetter_t if (endchar_op != OpCode_Invalid) for (; last < gid; last++) - buffArray.arrayZ[last].push (endchar_op); + { + // Hack to point vector to static string. + auto &b = buffArray.arrayZ[last]; + b.length = 1; + b.arrayZ = const_cast(endchar_str); + } last++; // Skip over gid unsigned int fd = acc.fdSelect->get_fd (old_glyph); @@ -820,7 +825,12 @@ struct subr_subsetter_t } if (endchar_op != OpCode_Invalid) for (; last < num_glyphs; last++) - buffArray.arrayZ[last].push (endchar_op); + { + // Hack to point vector to static string. + auto &b = buffArray.arrayZ[last]; + b.length = 1; + b.arrayZ = const_cast(endchar_str); + } return true; } diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 935ade5b3..9efd48010 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -102,8 +102,14 @@ struct hb_vector_t void fini () { - shrink_vector (0); - hb_free (arrayZ); + /* We allow a hack to make the vector point to a foriegn array + * by the user. In that case length/arrayZ are non-zero but + * allocated is zero. Don't free anything. */ + if (!allocated) + { + shrink_vector (0); + hb_free (arrayZ); + } init (); }