From 4cce5f458d2e8a5ecbad0ffddc048eb56409b5e6 Mon Sep 17 00:00:00 2001 From: Yuwei Zhao <75137864+fyrsta7@users.noreply.github.com> Date: Mon, 22 Sep 2025 03:40:26 +0800 Subject: [PATCH] Perf: Optimize function append in include/fmt/base.h (#4541) --- include/fmt/base.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/fmt/base.h b/include/fmt/base.h index 42e192ac..7c937691 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -1844,10 +1844,13 @@ template class buffer { void append(const U* begin, const U* end) { while (begin != end) { - auto count = to_unsigned(end - begin); - try_reserve(size_ + count); auto free_cap = capacity_ - size_; - if (free_cap < count) count = free_cap; + auto count = to_unsigned(end - begin); + if (free_cap < count) { + grow_(*this, size_ + count); + free_cap = capacity_ - size_; + count = (count < free_cap) ? count : free_cap; + } // A loop is faster than memcpy on small sizes. T* out = ptr_ + size_; for (size_t i = 0; i < count; ++i) out[i] = begin[i];