Skip to content

Commit

Permalink
Reduces the amount of reallocations needed
Browse files Browse the repository at this point in the history
This performance problem was observed while fixing 16 bit encoding.

Reading six 500Mb blocks required 32Gb of RAM
  • Loading branch information
crsib committed Apr 12, 2024
1 parent e38e1c2 commit 52f882d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
23 changes: 21 additions & 2 deletions libraries/lib-cloud-audiocom/sync/RemoteProjectSnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,28 @@ void RemoteProjectSnapshot::DownloadBlob(
});
}

namespace
{
std::vector<uint8_t>
ReadResponseData(audacity::network_manager::IResponse& response)
{
const auto size = response.getBytesAvailable();

if (size == 0)
return response.readAll<std::vector<uint8_t>>();

std::vector<uint8_t> data(size);
response.readData(data.data(), size);

return data;
}
} // namespace


void RemoteProjectSnapshot::OnProjectBlobDownloaded(
audacity::network_manager::ResponsePtr response)
{
const std::vector<uint8_t> data = response->readAll();
const std::vector<uint8_t> data = ReadResponseData(*response);
uint64_t dictSize = 0;

if (data.size() < sizeof(uint64_t))
Expand Down Expand Up @@ -511,7 +529,8 @@ void RemoteProjectSnapshot::OnProjectBlobDownloaded(
void RemoteProjectSnapshot::OnBlockDownloaded(
std::string blockHash, audacity::network_manager::ResponsePtr response)
{
const auto compressedData = response->readAll<std::vector<uint8_t>>();
const auto compressedData = ReadResponseData(*response);

const auto blockData =
DecompressBlock(compressedData.data(), compressedData.size());

Expand Down
5 changes: 5 additions & 0 deletions libraries/lib-cloud-audiocom/sync/WavPackCompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 52f882d

Please sign in to comment.