Skip to content

Commit

Permalink
replay: optimize decompressZST with pre-allocated buffer (commaai#33562)
Browse files Browse the repository at this point in the history
improve decompressZST
  • Loading branch information
deanlee authored Sep 16, 2024
1 parent dcbeda1 commit fd893df
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion tools/replay/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,17 @@ std::string decompressZST(const std::byte *in, size_t in_size, std::atomic<bool>

// Initialize input and output buffers
ZSTD_inBuffer input = {in, in_size, 0};

// Estimate and reserve memory for decompressed data
size_t estimatedDecompressedSize = ZSTD_getFrameContentSize(in, in_size);
if (estimatedDecompressedSize == ZSTD_CONTENTSIZE_ERROR || estimatedDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN) {
estimatedDecompressedSize = in_size * 2; // Use a fallback size
}

std::string decompressedData;
const size_t bufferSize = ZSTD_DStreamOutSize(); // recommended output buffer size
decompressedData.reserve(estimatedDecompressedSize);

const size_t bufferSize = ZSTD_DStreamOutSize(); // Recommended output buffer size
std::string outputBuffer(bufferSize, '\0');

while (input.pos < input.size && !(abort && *abort)) {
Expand Down

0 comments on commit fd893df

Please sign in to comment.