From 87a1bcd0555c2b75c6978ba20c7b86fd64dc3a24 Mon Sep 17 00:00:00 2001 From: KRM7 <70973547+KRM7@users.noreply.github.com> Date: Wed, 13 Mar 2024 21:46:38 +0100 Subject: [PATCH] better resize implementation --- benchmark/small_vector.cpp | 2 +- src/small_vector.hpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmark/small_vector.cpp b/benchmark/small_vector.cpp index 0d9d1b9..5f1a191 100644 --- a/benchmark/small_vector.cpp +++ b/benchmark/small_vector.cpp @@ -119,7 +119,7 @@ void benchmark_resize(benchmark::State& state) for (auto _ : state) { - vec.resize(size - 1); + vec.resize(0); benchmark::DoNotOptimize(vec); vec.resize(size); diff --git a/src/small_vector.hpp b/src/small_vector.hpp index 08923e9..f807b7b 100644 --- a/src/small_vector.hpp +++ b/src/small_vector.hpp @@ -978,12 +978,12 @@ class small_vector template constexpr void resize_impl(size_type count, Args&&... args) { - if (count < size()) + if (count <= size()) { detail::destroy_range(alloc_, first_ + count, last_); last_ = first_ + count; } - else if (count > size()) + else { reserve(count); detail::construct_range(alloc_, last_, first_ + count, std::forward(args)...);