From 0a1b26b862467d318596d03b9300ae350be5c99e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 24 Feb 2025 20:07:59 -0700 Subject: [PATCH] [coord-setter] Reduce malloc pressure ~15% speedup benchmark-font draw of varc-hanzi.ttf --- src/OT/Var/VARC/coord-setter.hh | 38 +++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/OT/Var/VARC/coord-setter.hh b/src/OT/Var/VARC/coord-setter.hh index a2b483ce2..47685290c 100644 --- a/src/OT/Var/VARC/coord-setter.hh +++ b/src/OT/Var/VARC/coord-setter.hh @@ -11,22 +11,46 @@ namespace OT { struct coord_setter_t { - coord_setter_t (hb_array_t coords) : - coords (coords) {} + coord_setter_t (hb_array_t coords_) + { + length = coords_.length; + if (length <= ARRAY_LENGTH (static_coords)) + hb_memcpy (static_coords, coords_.arrayZ, length * sizeof (int)); + else + dynamic_coords.extend (coords_); + } int& operator [] (unsigned idx) { if (unlikely (idx >= HB_VAR_COMPOSITE_MAX_AXES)) return Crap(int); - if (coords.length < idx + 1) - coords.resize (idx + 1); - return coords[idx]; + + if (length <= ARRAY_LENGTH (static_coords)) + { + if (idx < ARRAY_LENGTH (static_coords)) + { + while (length <= idx) + static_coords[length++] = 0; + return static_coords[idx]; + } + else + { + dynamic_coords.extend (hb_array (static_coords, length)); + } + } + + if (dynamic_coords.length <= idx && + unlikely (!dynamic_coords.resize (idx + 1))) + return Crap(int); + return dynamic_coords.arrayZ[idx]; } hb_array_t get_coords () - { return coords.as_array (); } + { return dynamic_coords ? dynamic_coords.as_array () : hb_array (static_coords, length); } - hb_vector_t coords; + unsigned length; + int static_coords[32]; + hb_vector_t dynamic_coords; };