From 52f882d27428307d05c0d49972f87b70f01b690a Mon Sep 17 00:00:00 2001 From: Dmitry Vedenko Date: Fri, 12 Apr 2024 15:26:11 +0300 Subject: [PATCH] Reduces the amount of reallocations needed This performance problem was observed while fixing 16 bit encoding. Reading six 500Mb blocks required 32Gb of RAM --- .../sync/RemoteProjectSnapshot.cpp | 23 +++++++++++++++++-- .../sync/WavPackCompressor.cpp | 5 ++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/libraries/lib-cloud-audiocom/sync/RemoteProjectSnapshot.cpp b/libraries/lib-cloud-audiocom/sync/RemoteProjectSnapshot.cpp index 5014c3095d03..65cdd36a6a0c 100644 --- a/libraries/lib-cloud-audiocom/sync/RemoteProjectSnapshot.cpp +++ b/libraries/lib-cloud-audiocom/sync/RemoteProjectSnapshot.cpp @@ -413,10 +413,28 @@ void RemoteProjectSnapshot::DownloadBlob( }); } +namespace +{ +std::vector +ReadResponseData(audacity::network_manager::IResponse& response) +{ + const auto size = response.getBytesAvailable(); + + if (size == 0) + return response.readAll>(); + + std::vector data(size); + response.readData(data.data(), size); + + return data; +} +} // namespace + + void RemoteProjectSnapshot::OnProjectBlobDownloaded( audacity::network_manager::ResponsePtr response) { - const std::vector data = response->readAll(); + const std::vector data = ReadResponseData(*response); uint64_t dictSize = 0; if (data.size() < sizeof(uint64_t)) @@ -511,7 +529,8 @@ void RemoteProjectSnapshot::OnProjectBlobDownloaded( void RemoteProjectSnapshot::OnBlockDownloaded( std::string blockHash, audacity::network_manager::ResponsePtr response) { - const auto compressedData = response->readAll>(); + const auto compressedData = ReadResponseData(*response); + const auto blockData = DecompressBlock(compressedData.data(), compressedData.size()); diff --git a/libraries/lib-cloud-audiocom/sync/WavPackCompressor.cpp b/libraries/lib-cloud-audiocom/sync/WavPackCompressor.cpp index 671d21e0cb59..288f4d4f53f2 100644 --- a/libraries/lib-cloud-audiocom/sync/WavPackCompressor.cpp +++ b/libraries/lib-cloud-audiocom/sync/WavPackCompressor.cpp @@ -80,6 +80,11 @@ struct Exporter final const size_t samplesRead = Block.Block->GetSamples( sampleData.data(), Block.Format, 0, sampleCount, false); + // Reserve 1.5 times the size of the original data + // The compressed data will be smaller than the original data, + // but we overallocate just in case + CompressedData.reserve(sampleData.size() * 3 / 2); + if (sampleFormat == int16Sample) { constexpr size_t conversionSamplesCount = 4096;