Perf: Optimize function append in include/fmt/base.h (#4541)

This commit is contained in:
Yuwei Zhao
2025-09-22 03:40:26 +08:00
committed by GitHub
parent aa8a30838a
commit 4cce5f458d

View File

@@ -1844,10 +1844,13 @@ template <typename T> class buffer {
void void
append(const U* begin, const U* end) { append(const U* begin, const U* end) {
while (begin != end) { while (begin != end) {
auto count = to_unsigned(end - begin);
try_reserve(size_ + count);
auto free_cap = capacity_ - size_; 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. // A loop is faster than memcpy on small sizes.
T* out = ptr_ + size_; T* out = ptr_ + size_;
for (size_t i = 0; i < count; ++i) out[i] = begin[i]; for (size_t i = 0; i < count; ++i) out[i] = begin[i];