From 97a5c52af467df2dc9c4d2f22b4f1ad4adc2d018 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 24 Feb 2025 20:53:19 -0700 Subject: [PATCH] [VARC/MultiVarStore] Reduce malloc pressure more --- src/OT/Var/VARC/VARC.cc | 5 +++-- src/hb-ot-layout-common.hh | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/OT/Var/VARC/VARC.cc b/src/OT/Var/VARC/VARC.cc index a474054fd..af30fc5d1 100644 --- a/src/OT/Var/VARC/VARC.cc +++ b/src/OT/Var/VARC/VARC.cc @@ -381,7 +381,8 @@ VARC::get_path_at (hb_font_t *font, hb_ubytes_t record = (this+glyphRecords)[idx]; - VarRegionList::cache_t *cache = coords ? (this+varStore).create_cache () : nullptr; + float static_cache[128]; + VarRegionList::cache_t *cache = (this+varStore).create_cache (hb_array (static_cache)); transform.scale (font->x_multf, font->y_multf); @@ -392,7 +393,7 @@ VARC::get_path_at (hb_font_t *font, scratch, cache); - (this+varStore).destroy_cache (cache); + (this+varStore).destroy_cache (cache, hb_array (static_cache)); return true; } diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 1dfda05e3..f9a5c1b9c 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -3440,7 +3440,7 @@ struct MultiItemVariationStore { using cache_t = SparseVarRegionList::cache_t; - cache_t *create_cache () const + cache_t *create_cache (hb_array_t static_cache = hb_array_t ()) const { #ifdef HB_NO_VAR return nullptr; @@ -3448,8 +3448,14 @@ struct MultiItemVariationStore auto &r = this+regions; unsigned count = r.regions.len; - float *cache = (float *) hb_malloc (sizeof (float) * count); - if (unlikely (!cache)) return nullptr; + float *cache; + if (count <= static_cache.length) + cache = static_cache.arrayZ; + else + { + cache = (float *) hb_malloc (sizeof (float) * count); + if (unlikely (!cache)) return nullptr; + } for (unsigned i = 0; i < count; i++) cache[i] = REGION_CACHE_ITEM_CACHE_INVALID; @@ -3457,7 +3463,12 @@ struct MultiItemVariationStore return cache; } - static void destroy_cache (cache_t *cache) { hb_free (cache); } + static void destroy_cache (cache_t *cache, + hb_array_t static_cache = hb_array_t ()) + { + if (cache != static_cache.arrayZ) + hb_free (cache); + } private: void get_delta (unsigned int outer, unsigned int inner,