From 41626401b0ce15814620b47bd115dec36c1b9446 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 25 Feb 2025 18:48:34 -0700 Subject: [PATCH] [vector] Add faster extend() for array types --- src/hb-vector.hh | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/hb-vector.hh b/src/hb-vector.hh index d4a52f0d2..809f2d842 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -57,22 +57,6 @@ struct hb_vector_t { extend (o); } - template - void extend (const Iterable &o) - { - auto iter = hb_iter (o); - if (iter.is_random_access_iterator || iter.has_fast_len) - alloc (hb_len (iter), true); - while (iter) - { - if (unlikely (!alloc (length + 1))) - return; - unsigned room = allocated - length; - for (unsigned i = 0; i < room && iter; i++) - push_has_room (*iter++); - } - } hb_vector_t (const hb_vector_t &o) : hb_vector_t () { alloc_exact (o.length); @@ -100,6 +84,35 @@ struct hb_vector_t } ~hb_vector_t () { fini (); } + template + void extend (const Iterable &o) + { + auto iter = hb_iter (o); + if (iter.is_random_access_iterator || iter.has_fast_len) + alloc (hb_len (iter), true); + while (iter) + { + if (unlikely (!alloc (length + 1))) + return; + unsigned room = allocated - length; + for (unsigned i = 0; i < room && iter; i++) + push_has_room (*iter++); + } + } + void extend (array_t o) + { + alloc (length + o.length); + if (unlikely (in_error ())) return; + copy_array (o); + } + void extend (c_array_t o) + { + alloc (length + o.length); + if (unlikely (in_error ())) return; + copy_array (o); + } + public: int allocated = 0; /* < 0 means allocation failed. */ unsigned int length = 0;