Improve CompressedWriteBuffer to avoid unnecessary memcpy

This commit is contained in:
jinjunzh 2022-05-16 16:22:13 -04:00
parent 9ad7f2d081
commit 2ae9e22248

View File

@ -22,14 +22,29 @@ void CompressedWriteBuffer::nextImpl()
if (!offset())
return;
UInt32 compressed_size = 0;
size_t decompressed_size = offset();
UInt32 compressed_reserve_size = codec->getCompressedReserveSize(decompressed_size);
compressed_buffer.resize(compressed_reserve_size);
UInt32 compressed_size = codec->compress(working_buffer.begin(), decompressed_size, compressed_buffer.data());
if(out.available() > (compressed_reserve_size + CHECKSUM_SIZE))
{
char *out_checksum_ptr = out.position();
char *out_compressed_ptr = out.position() + CHECKSUM_SIZE;
compressed_size = codec->compress(working_buffer.begin(), decompressed_size, out_compressed_ptr);
CityHash_v1_0_2::uint128 checksum = CityHash_v1_0_2::CityHash128(compressed_buffer.data(), compressed_size);
out.write(reinterpret_cast<const char *>(&checksum), CHECKSUM_SIZE);
out.write(compressed_buffer.data(), compressed_size);
CityHash_v1_0_2::uint128 checksum = CityHash_v1_0_2::CityHash128(out_compressed_ptr, compressed_size);
memcpy(out_checksum_ptr, &checksum, CHECKSUM_SIZE);
out.position() += CHECKSUM_SIZE + compressed_size;
}
else
{
compressed_buffer.resize(compressed_reserve_size);
compressed_size = codec->compress(working_buffer.begin(), decompressed_size, compressed_buffer.data());
CityHash_v1_0_2::uint128 checksum = CityHash_v1_0_2::CityHash128(compressed_buffer.data(), compressed_size);
out.write(reinterpret_cast<const char *>(&checksum), CHECKSUM_SIZE);
out.write(compressed_buffer.data(), compressed_size);
}
}
CompressedWriteBuffer::~CompressedWriteBuffer()