From e333223f269a090732ae3a9d468bb93c35bbeb62 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 25 Nov 2022 14:23:57 -0700 Subject: [PATCH] [array] Optimize serializing copy() --- src/hb-array.hh | 16 +++++++++++++++- src/hb-vector.hh | 11 ++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/hb-array.hh b/src/hb-array.hh index 18bcd5225..5cadc4805 100644 --- a/src/hb-array.hh +++ b/src/hb-array.hh @@ -262,7 +262,9 @@ struct hb_array_t : hb_iter_with_fallback_t, Type&> void fini () { hb_free ((void *) arrayZ); arrayZ = nullptr; length = 0; } - template + template hb_array_t copy (hb_serialize_context_t *c) const { TRACE_SERIALIZE (this); @@ -273,6 +275,18 @@ struct hb_array_t : hb_iter_with_fallback_t, Type&> return_trace (hb_array_t (out, length)); } + template + hb_array_t copy (hb_serialize_context_t *c) const + { + TRACE_SERIALIZE (this); + auto* out = c->start_embed (arrayZ); + if (unlikely (!c->extend_size (out, get_size ()))) return_trace (hb_array_t ()); + hb_memcpy (out, arrayZ, length); + return_trace (hb_array_t (out, length)); + } + template bool sanitize (hb_sanitize_context_t *c) const { return c->check_array (arrayZ, length); } diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 5a82aa61b..e6bb7d22e 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -268,12 +268,13 @@ struct hb_vector_t { length = other.length; #ifndef HB_OPTIMIZE_SIZE - /* This runs faster because of alignment. */ - for (unsigned i = 0; i < length; i++) - arrayZ[i] = other.arrayZ[i]; -#else - hb_memcpy ((void *) arrayZ, (const void *) other.arrayZ, length * item_size); + if (sizeof (T) >= sizeof (long long)) + /* This runs faster because of alignment. */ + for (unsigned i = 0; i < length; i++) + arrayZ[i] = other.arrayZ[i]; + else #endif + hb_memcpy ((void *) arrayZ, (const void *) other.arrayZ, length * item_size); } template