From 3edd6cdcd55dabec87ed8da9ffed82b1c41e0720 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 27 Jun 2023 06:56:00 -0600 Subject: [PATCH] [vector] Minor micro-optimize shrink_vector The compiler seems to understand this pattern better. --- src/hb-vector.hh | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/hb-vector.hh b/src/hb-vector.hh index 8e11d65ee..23a96d708 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -374,14 +374,15 @@ struct hb_vector_t void shrink_vector (unsigned size) { - if (std::is_trivially_destructible::value) - length = size; - else - while ((unsigned) length > size) - { - arrayZ[(unsigned) length - 1].~Type (); - length--; - } + assert (size <= length); + if (!std::is_trivially_destructible::value) + { + unsigned count = length - size; + Type *p = arrayZ + length - 1; + while (count--) + p--->~Type (); + } + length = size; } void