From: Joel Rosdahl Date: Sat, 25 Oct 2025 15:19:22 +0000 (+0200) Subject: perf: Avoid zero-filling in util::Bytes in all cases X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=04293ba23cd0d83ab9c9c0904c5ce6846e950022;p=thirdparty%2Fccache.git perf: Avoid zero-filling in util::Bytes in all cases --- diff --git a/src/ccache/util/bytes.cpp b/src/ccache/util/bytes.cpp index 4856d184..3bb76183 100644 --- a/src/ccache/util/bytes.cpp +++ b/src/ccache/util/bytes.cpp @@ -36,11 +36,9 @@ assign_from_data(Bytes* bytes, const void* data, size_t size) noexcept } // namespace Bytes::Bytes(const Bytes& other) noexcept - : m_data(std::make_unique(other.m_size)), - m_size(other.m_size), - m_capacity(other.m_size) { - if (m_size > 0) { + if (other.m_size > 0) { + resize(other.m_size); std::memcpy(m_data.get(), other.m_data.get(), m_size); } } @@ -98,11 +96,12 @@ void Bytes::reserve(size_t size) noexcept { if (size > m_capacity) { - auto bytes = std::make_unique(size); + // In C++20, use std::make_unique_for_overwrite instead. + auto new_data = std::unique_ptr(new uint8_t[size]); if (m_size > 0) { - std::memcpy(bytes.get(), m_data.get(), m_size); + std::memcpy(new_data.get(), m_data.get(), m_size); } - m_data = std::move(bytes); + m_data = std::move(new_data); m_capacity = size; } } @@ -119,7 +118,8 @@ Bytes::insert(const uint8_t* pos, const size_t offset = pos - m_data.get(); if (m_size + inserted_size > m_capacity) { m_capacity = std::max(2 * m_capacity, m_size + inserted_size); - auto new_data = std::make_unique(m_capacity); + // In C++20, use std::make_unique_for_overwrite instead. + auto new_data = std::unique_ptr(new uint8_t[m_capacity]); if (offset > 0) { std::memcpy(new_data.get(), m_data.get(), offset); } @@ -141,15 +141,7 @@ Bytes::insert(const uint8_t* pos, void Bytes::resize(size_t size) noexcept { - if (size > m_capacity) { - // In C++20, use std::make_unique_for_overwrite instead. - auto new_data = std::unique_ptr(new uint8_t[size]); - if (m_size > 0) { - std::memcpy(new_data.get(), m_data.get(), m_size); - } - m_data = std::move(new_data); - m_capacity = size; - } + reserve(size); m_size = size; }