From ea2291d24d531a1ea221f1035636303ac0da787d Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Tue, 28 Jan 2025 10:13:50 +0100 Subject: Replace ContentBlobContainer with std::unordered_set --- src/buildtool/execution_api/common/common_api.hpp | 38 ++++++++++++++--------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'src/buildtool/execution_api/common/common_api.hpp') diff --git a/src/buildtool/execution_api/common/common_api.hpp b/src/buildtool/execution_api/common/common_api.hpp index f58e4893..5060c086 100644 --- a/src/buildtool/execution_api/common/common_api.hpp +++ b/src/buildtool/execution_api/common/common_api.hpp @@ -15,6 +15,7 @@ #ifndef INCLUDED_SRC_BUILDTOOL_EXECUTION_API_COMMON_COMMON_API_HPP #define INCLUDED_SRC_BUILDTOOL_EXECUTION_API_COMMON_COMMON_API_HPP +#include #include #include #include @@ -122,10 +123,11 @@ template /// \returns Returns true on success, false otherwise (failures or exceptions). template auto UpdateContainerAndUpload( - gsl::not_null*> const& container, + gsl::not_null>*> const& container, ContentBlob&& blob, bool exception_is_fatal, - std::function&&)> const& uploader, + std::function>&&)> const& + uploader, Logger const* logger = nullptr) noexcept -> bool { // Optimize upload of blobs with respect to the maximum transfer limit, such // that we never store unnecessarily more data in the container than we need @@ -133,24 +135,32 @@ auto UpdateContainerAndUpload( try { if (blob.data->size() > kMaxBatchTransferSize) { // large blobs use individual stream upload - if (not uploader(ContentBlobContainer{{blob}})) { + if (not uploader(std::unordered_set>{ + {std::move(blob)}})) { return false; } } else { - if (container->ContentSize() + blob.data->size() > - kMaxBatchTransferSize) { - // swap away from original container to allow move during upload - ContentBlobContainer tmp_container{}; - std::swap(*container, tmp_container); - // if we would surpass the transfer limit, upload the current - // container and clear it before adding more blobs - if (not uploader(std::move(tmp_container))) { - return false; + if (not container->contains(blob)) { + std::size_t content_size = 0; + for (auto const& blob : *container) { + content_size += blob.data->size(); } + + if (content_size + blob.data->size() > kMaxBatchTransferSize) { + // swap away from original container to allow move during + // upload + std::unordered_set> tmp_container{}; + std::swap(*container, tmp_container); + // if we would surpass the transfer limit, upload the + // current container and clear it before adding more blobs + if (not uploader(std::move(tmp_container))) { + return false; + } + } + // add current blob to container + container->emplace(std::move(blob)); } - // add current blob to container - container->Emplace(std::move(blob)); } } catch (std::exception const& ex) { if (exception_is_fatal) { -- cgit v1.2.3