From 83e5f33786e31c8fb57b6f4232d02bb18164d956 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 16:05:31 +0800 Subject: [PATCH 1/5] perf(cpp): reuse the LZ4 decompression buffer across pages LZ4Compressor::uncompress() allocated a fresh buffer for every page and released it in after_uncompress(), and it guessed the output size with a 1.5x/2.5x/... retry ladder, re-allocating until the guess fit. Keep one buffer on the compressor, grow it only when a page needs more room, and release it in destroy() (the destructor now runs destroy() so callers that free the placement-new'ed object do not leak it). Pages after the first therefore decompress without any allocation. Note the destructor change: LZ4Compressor previously had an empty destructor although the object is placement-new'ed by CompressorFactory, so buffers held here would otherwise never be freed. --- cpp/src/compress/lz4_compressor.cc | 55 +++++++++++++++--------------- cpp/src/compress/lz4_compressor.h | 10 ++++-- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/cpp/src/compress/lz4_compressor.cc b/cpp/src/compress/lz4_compressor.cc index a7333f2464..74ac515cc3 100644 --- a/cpp/src/compress/lz4_compressor.cc +++ b/cpp/src/compress/lz4_compressor.cc @@ -34,6 +34,7 @@ void LZ4Compressor::destroy() { mem_free(uncompressed_buf_); uncompressed_buf_ = nullptr; } + uncompressed_buf_capacity_ = 0; } int LZ4Compressor::reset(bool for_compress) { @@ -109,42 +110,40 @@ int LZ4Compressor::uncompress(char* compressed_buf, uint32_t compressed_buf_len, char*& uncompressed_buf, uint32_t& uncompressed_buf_len, float ratio) { int ret = E_OK; - char* regen_buffer = nullptr; - int src_size = 0; - if (ratio > 0) { - src_size = (int)(compressed_buf_len * ratio); - regen_buffer = (char*)mem_alloc(src_size, MOD_COMPRESSOR_OBJ); - } else { - ret = E_COMPRESS_ERR; + int src_size = (int)(compressed_buf_len * ratio); + // Reuse the persistent buffer across pages; (re)allocate only when the + // requested capacity grows. This avoids one malloc + free per page, which + // dominates the read path's allocation churn. + if (uncompressed_buf_ == nullptr || + src_size > static_cast(uncompressed_buf_capacity_)) { + if (uncompressed_buf_ != nullptr) { + mem_free(uncompressed_buf_); + uncompressed_buf_ = nullptr; + } + uncompressed_buf_ = + (char*)mem_alloc((size_t)src_size, MOD_COMPRESSOR_OBJ); + if (uncompressed_buf_ == nullptr) { + uncompressed_buf_capacity_ = 0; + return E_OOM; + } + uncompressed_buf_capacity_ = static_cast(src_size); } - if (regen_buffer != nullptr) { - int decompressed_size = LZ4_decompress_safe( - compressed_buf, regen_buffer, compressed_buf_len, src_size); - - if (decompressed_size < 0) { - // Release regen_buffer since decompress failed - mem_free(regen_buffer); - regen_buffer = nullptr; - ret = E_COMPRESS_ERR; - } else { - uncompressed_buf = regen_buffer; - uncompressed_buf_ = regen_buffer; - uncompressed_buf_len = decompressed_size; - } + int decompressed_size = LZ4_decompress_safe( + compressed_buf, uncompressed_buf_, compressed_buf_len, src_size); + if (decompressed_size < 0) { + ret = E_COMPRESS_ERR; } else { - ret = E_OOM; + uncompressed_buf = uncompressed_buf_; + uncompressed_buf_len = static_cast(decompressed_size); } return ret; } void LZ4Compressor::after_uncompress(char* uncompressed_buf) { - if (uncompressed_buf != nullptr) { - mem_free(uncompressed_buf); - if (uncompressed_buf_ == uncompressed_buf) { - uncompressed_buf_ = nullptr; - } - } + // The decompression buffer is owned by this compressor and reused across + // pages; it is freed in destroy(). Do not free it per page. + (void)uncompressed_buf; } } // end namespace storage diff --git a/cpp/src/compress/lz4_compressor.h b/cpp/src/compress/lz4_compressor.h index b33732c671..887001f08d 100644 --- a/cpp/src/compress/lz4_compressor.h +++ b/cpp/src/compress/lz4_compressor.h @@ -38,8 +38,11 @@ namespace storage { class LZ4Compressor : public Compressor { public: - LZ4Compressor() : compressed_buf_(nullptr), uncompressed_buf_(nullptr){}; - ~LZ4Compressor(){}; + LZ4Compressor() + : compressed_buf_(nullptr), + uncompressed_buf_(nullptr), + uncompressed_buf_capacity_(0){}; + ~LZ4Compressor() { destroy(); } // @for_compress // true - for compressiom // false - for uncompression @@ -55,7 +58,10 @@ class LZ4Compressor : public Compressor { private: char* compressed_buf_; + // Reused across uncompress() calls; owned by this compressor and freed in + // destroy(). uncompressed_buf_capacity_ tracks its allocated size. char* uncompressed_buf_; + uint32_t uncompressed_buf_capacity_; int uncompress(char* compressed_buf, uint32_t compressed_buf_len, char*& uncompressed_buf, uint32_t& uncompressed_buf_len, From 7d1a69b8fa1e1d953d9467d028048240adb272b6 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 16:36:29 +0800 Subject: [PATCH 2/5] test(cpp): cover LZ4 decompression buffer reuse Uncompress pages of growing then shrinking sizes through one compressor and verify every page's contents, so a reused (or grown) buffer cannot silently corrupt later pages. Also pin the contract that after_uncompress() leaves the compressor usable for the next page. --- cpp/test/compress/lz4_compressor_test.cc | 72 ++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/cpp/test/compress/lz4_compressor_test.cc b/cpp/test/compress/lz4_compressor_test.cc index 0b2249f8d7..3d9bac3e70 100644 --- a/cpp/test/compress/lz4_compressor_test.cc +++ b/cpp/test/compress/lz4_compressor_test.cc @@ -162,4 +162,76 @@ TEST_F(LZ4Test, AfterUncompressFreesParamNotMember) { compressor.after_compress(compressed_a); compressor.after_compress(compressed_b); } + +TEST_F(LZ4Test, UncompressReusesBufferAcrossDifferentPageSizes) { + storage::LZ4Compressor compressor; + // Small page, then a much larger one, then a small one again: the reused + // buffer must grow for the large page and stay correct for every page. + const std::vector payloads = {std::string(1024, 'a'), + std::string(200000, 'b'), + std::string(4096, 'c')}; + + for (const auto& payload : payloads) { + std::string input = payload; + char* compressed = nullptr; + uint32_t compressed_len = 0; + ASSERT_EQ(compressor.reset(true), common::E_OK); + ASSERT_EQ(compressor.compress(&input[0], input.size(), compressed, + compressed_len), + common::E_OK); + + char* uncompressed = nullptr; + uint32_t uncompressed_len = 0; + ASSERT_EQ(compressor.reset(false), common::E_OK); + ASSERT_EQ(compressor.uncompress(compressed, compressed_len, + uncompressed, uncompressed_len), + common::E_OK); + ASSERT_NE(uncompressed, nullptr); + EXPECT_EQ(uncompressed_len, input.size()); + EXPECT_EQ(std::string(uncompressed, uncompressed_len), input); + + compressor.after_uncompress(uncompressed); + compressor.after_compress(compressed); + } +} + +TEST_F(LZ4Test, UncompressAfterPreviousPageNoLongerInvalidated) { + storage::LZ4Compressor compressor; + std::string input_a(2048, 'x'); + std::string input_b(8192, 'y'); + char* compressed_a = nullptr; + char* compressed_b = nullptr; + uint32_t compressed_a_len = 0; + uint32_t compressed_b_len = 0; + ASSERT_EQ(compressor.reset(true), common::E_OK); + ASSERT_EQ(compressor.compress(&input_a[0], input_a.size(), compressed_a, + compressed_a_len), + common::E_OK); + ASSERT_EQ(compressor.compress(&input_b[0], input_b.size(), compressed_b, + compressed_b_len), + common::E_OK); + + char* out_a = nullptr; + uint32_t out_a_len = 0; + ASSERT_EQ(compressor.reset(false), common::E_OK); + ASSERT_EQ( + compressor.uncompress(compressed_a, compressed_a_len, out_a, out_a_len), + common::E_OK); + // after_uncompress() releases ownership back to the compressor but must + // leave the compressor usable for the next page. + compressor.after_uncompress(out_a); + + char* out_b = nullptr; + uint32_t out_b_len = 0; + ASSERT_EQ( + compressor.uncompress(compressed_b, compressed_b_len, out_b, out_b_len), + common::E_OK); + ASSERT_EQ(out_b_len, input_b.size()); + EXPECT_EQ(std::string(out_b, out_b_len), input_b); + + compressor.after_uncompress(out_b); + compressor.after_compress(compressed_a); + compressor.after_compress(compressed_b); +} + } // namespace From 857bca18b0460cec958858f44bfd7a7afa6039bd Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 17:30:48 +0800 Subject: [PATCH 3/5] fix(cpp): run compressor destructors from CompressorFactory::free Compressors are placement-new'd into mem_alloc() storage, so free() only returned the storage and never ran ~Compressor(). That was harmless while every compressor released its buffers in after_uncompress(), but LZ4 now retains its decompression buffer for reuse, so every recovered single-page chunk leaked one page (LeakSanitizer: RestorableTsFileIOWriterTest.* failed on the Ubuntu ASan job). Make free() call the (virtual) destructor before mem_free and drop the explicit ~Compressor() calls in the chunk readers so nothing is destroyed twice. Also guard the LZ4 ratio ladder against non-positive ratios and make LZ4Compressor non-copyable, since it owns the reused buffer. Tests: assert the buffer pointer is reused for a smaller page and that factory-allocated compressors release the retained buffer (LeakSanitizer catches the old behaviour). --- cpp/src/compress/compressor_factory.h | 12 ++++++- cpp/src/compress/lz4_compressor.cc | 3 ++ cpp/src/compress/lz4_compressor.h | 3 ++ cpp/src/reader/aligned_chunk_reader.cc | 4 --- cpp/src/reader/chunk_reader.cc | 1 - cpp/test/compress/lz4_compressor_test.cc | 42 ++++++++++++++++++++++-- 6 files changed, 56 insertions(+), 9 deletions(-) diff --git a/cpp/src/compress/compressor_factory.h b/cpp/src/compress/compressor_factory.h index c9a9c64052..b378676497 100644 --- a/cpp/src/compress/compressor_factory.h +++ b/cpp/src/compress/compressor_factory.h @@ -114,7 +114,17 @@ class CompressorFactory { } } - static void free(Compressor* c) { common::mem_free(c); } + // Compressors are placement-new'd into mem_alloc() storage, so freeing + // that storage alone skips ~Compressor(). Any resource a compressor + // retains outside its own object (e.g. LZ4's reused decompression + // buffer) would leak; run the (virtual) destructor first. + static void free(Compressor* c) { + if (c == nullptr) { + return; + } + c->~Compressor(); + common::mem_free(c); + } }; } // end namespace storage diff --git a/cpp/src/compress/lz4_compressor.cc b/cpp/src/compress/lz4_compressor.cc index 74ac515cc3..20c405261b 100644 --- a/cpp/src/compress/lz4_compressor.cc +++ b/cpp/src/compress/lz4_compressor.cc @@ -110,6 +110,9 @@ int LZ4Compressor::uncompress(char* compressed_buf, uint32_t compressed_buf_len, char*& uncompressed_buf, uint32_t& uncompressed_buf_len, float ratio) { int ret = E_OK; + if (ratio <= 0) { + return E_COMPRESS_ERR; + } int src_size = (int)(compressed_buf_len * ratio); // Reuse the persistent buffer across pages; (re)allocate only when the // requested capacity grows. This avoids one malloc + free per page, which diff --git a/cpp/src/compress/lz4_compressor.h b/cpp/src/compress/lz4_compressor.h index 887001f08d..21c1be5e57 100644 --- a/cpp/src/compress/lz4_compressor.h +++ b/cpp/src/compress/lz4_compressor.h @@ -43,6 +43,9 @@ class LZ4Compressor : public Compressor { uncompressed_buf_(nullptr), uncompressed_buf_capacity_(0){}; ~LZ4Compressor() { destroy(); } + // Owns the reused decompression buffer; a copy would double-free it. + LZ4Compressor(const LZ4Compressor&) = delete; + LZ4Compressor& operator=(const LZ4Compressor&) = delete; // @for_compress // true - for compressiom // false - for uncompression diff --git a/cpp/src/reader/aligned_chunk_reader.cc b/cpp/src/reader/aligned_chunk_reader.cc index a0a0b23069..bee6bc3e03 100644 --- a/cpp/src/reader/aligned_chunk_reader.cc +++ b/cpp/src/reader/aligned_chunk_reader.cc @@ -161,12 +161,10 @@ void AlignedChunkReader::destroy() { value_decoder_ = nullptr; } if (time_compressor_ != nullptr) { - time_compressor_->~Compressor(); CompressorFactory::free(time_compressor_); time_compressor_ = nullptr; } if (value_compressor_ != nullptr) { - value_compressor_->~Compressor(); CompressorFactory::free(value_compressor_); value_compressor_ = nullptr; } @@ -196,7 +194,6 @@ void AlignedChunkReader::destroy() { col->decoder = nullptr; } if (col->compressor != nullptr) { - col->compressor->~Compressor(); CompressorFactory::free(col->compressor); col->compressor = nullptr; } @@ -236,7 +233,6 @@ void AlignedChunkReader::destroy() { std::vector().swap(time_decoder_pool_); for (auto* c : time_compressor_pool_) { if (c != nullptr) { - c->~Compressor(); CompressorFactory::free(c); } } diff --git a/cpp/src/reader/chunk_reader.cc b/cpp/src/reader/chunk_reader.cc index 76d6951d81..b35570c23c 100644 --- a/cpp/src/reader/chunk_reader.cc +++ b/cpp/src/reader/chunk_reader.cc @@ -81,7 +81,6 @@ void ChunkReader::destroy() { value_decoder_ = nullptr; } if (compressor_ != nullptr) { - compressor_->~Compressor(); CompressorFactory::free(compressor_); compressor_ = nullptr; } diff --git a/cpp/test/compress/lz4_compressor_test.cc b/cpp/test/compress/lz4_compressor_test.cc index 3d9bac3e70..b573f26226 100644 --- a/cpp/test/compress/lz4_compressor_test.cc +++ b/cpp/test/compress/lz4_compressor_test.cc @@ -26,6 +26,8 @@ #include #include +#include "compress/compressor_factory.h" + namespace { class LZ4Test : public ::testing::Test { @@ -166,13 +168,15 @@ TEST_F(LZ4Test, AfterUncompressFreesParamNotMember) { TEST_F(LZ4Test, UncompressReusesBufferAcrossDifferentPageSizes) { storage::LZ4Compressor compressor; // Small page, then a much larger one, then a small one again: the reused - // buffer must grow for the large page and stay correct for every page. + // buffer must grow for the large page (new allocation) and must be handed + // out again, unchanged, for the following smaller page. const std::vector payloads = {std::string(1024, 'a'), std::string(200000, 'b'), std::string(4096, 'c')}; - for (const auto& payload : payloads) { - std::string input = payload; + char* out_ptr[3] = {nullptr, nullptr, nullptr}; + for (size_t i = 0; i < payloads.size(); ++i) { + std::string input = payloads[i]; char* compressed = nullptr; uint32_t compressed_len = 0; ASSERT_EQ(compressor.reset(true), common::E_OK); @@ -189,10 +193,42 @@ TEST_F(LZ4Test, UncompressReusesBufferAcrossDifferentPageSizes) { ASSERT_NE(uncompressed, nullptr); EXPECT_EQ(uncompressed_len, input.size()); EXPECT_EQ(std::string(uncompressed, uncompressed_len), input); + out_ptr[i] = uncompressed; compressor.after_uncompress(uncompressed); compressor.after_compress(compressed); } + + EXPECT_NE(out_ptr[0], out_ptr[1]); // grew: a new allocation + EXPECT_EQ(out_ptr[1], out_ptr[2]); // smaller page reuses the same buffer +} + +// CompressorFactory::free() must run ~Compressor(): compressors are +// placement-new'd into mem_alloc() storage, and LZ4 retains its decompression +// buffer there. Before the fix the buffer leaked on every recovered chunk +// (LeakSanitizer caught it in the file-recovery tests). +TEST_F(LZ4Test, FactoryFreeReleasesRetainedBuffer) { + storage::Compressor* compressor = + storage::CompressorFactory::alloc_compressor(common::LZ4); + ASSERT_NE(nullptr, compressor); + + std::string input(100000, 'z'); + char* compressed = nullptr; + uint32_t compressed_len = 0; + ASSERT_EQ(compressor->reset(true), common::E_OK); + ASSERT_EQ(compressor->compress(&input[0], input.size(), compressed, + compressed_len), + common::E_OK); + char* uncompressed = nullptr; + uint32_t uncompressed_len = 0; + ASSERT_EQ(compressor->reset(false), common::E_OK); + ASSERT_EQ(compressor->uncompress(compressed, compressed_len, uncompressed, + uncompressed_len), + common::E_OK); + EXPECT_EQ(std::string(uncompressed, uncompressed_len), input); + compressor->after_uncompress(uncompressed); + compressor->after_compress(compressed); + storage::CompressorFactory::free(compressor); } TEST_F(LZ4Test, UncompressAfterPreviousPageNoLongerInvalidated) { From 482296fcb5988235f781726f538866d086f7aecd Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 23:11:34 +0800 Subject: [PATCH 4/5] fix(cpp): compute the LZ4 retry capacity without overflow Compute compressed_buf_len * ratio in double and reject values above INT_MAX before the int conversion; the largest retry ratio is 255x and the cast was undefined for huge inputs. Also drop the pointer-identity assertion for a grown buffer (the allocator may reuse the address) and fix a comment typo. --- cpp/src/compress/lz4_compressor.cc | 12 +++++++++++- cpp/src/compress/lz4_compressor.h | 2 +- cpp/test/compress/lz4_compressor_test.cc | 6 ++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cpp/src/compress/lz4_compressor.cc b/cpp/src/compress/lz4_compressor.cc index 20c405261b..873dd6869f 100644 --- a/cpp/src/compress/lz4_compressor.cc +++ b/cpp/src/compress/lz4_compressor.cc @@ -18,6 +18,8 @@ */ #include "lz4_compressor.h" +#include + #include "common/allocator/alloc_base.h" using namespace common; @@ -113,7 +115,15 @@ int LZ4Compressor::uncompress(char* compressed_buf, uint32_t compressed_buf_len, if (ratio <= 0) { return E_COMPRESS_ERR; } - int src_size = (int)(compressed_buf_len * ratio); + // Compute the requested capacity in double and check it before converting: + // compressed_buf_len * ratio can exceed INT_MAX (the last retry ratio is + // 255x), and an out-of-range double-to-int conversion is undefined. + const double requested_size = + static_cast(compressed_buf_len) * ratio; + if (requested_size > static_cast(std::numeric_limits::max())) { + return E_COMPRESS_ERR; + } + int src_size = static_cast(requested_size); // Reuse the persistent buffer across pages; (re)allocate only when the // requested capacity grows. This avoids one malloc + free per page, which // dominates the read path's allocation churn. diff --git a/cpp/src/compress/lz4_compressor.h b/cpp/src/compress/lz4_compressor.h index 21c1be5e57..1089983453 100644 --- a/cpp/src/compress/lz4_compressor.h +++ b/cpp/src/compress/lz4_compressor.h @@ -47,7 +47,7 @@ class LZ4Compressor : public Compressor { LZ4Compressor(const LZ4Compressor&) = delete; LZ4Compressor& operator=(const LZ4Compressor&) = delete; // @for_compress - // true - for compressiom + // true - for compression // false - for uncompression int reset(bool for_compress) OVERRIDE; void destroy() OVERRIDE; diff --git a/cpp/test/compress/lz4_compressor_test.cc b/cpp/test/compress/lz4_compressor_test.cc index b573f26226..efe6fff29f 100644 --- a/cpp/test/compress/lz4_compressor_test.cc +++ b/cpp/test/compress/lz4_compressor_test.cc @@ -199,8 +199,10 @@ TEST_F(LZ4Test, UncompressReusesBufferAcrossDifferentPageSizes) { compressor.after_compress(compressed); } - EXPECT_NE(out_ptr[0], out_ptr[1]); // grew: a new allocation - EXPECT_EQ(out_ptr[1], out_ptr[2]); // smaller page reuses the same buffer + // Growth frees the old buffer and allocates a new one; the allocator may + // reuse the same address, so only the guaranteed reuse for the smaller + // page is asserted. + EXPECT_EQ(out_ptr[1], out_ptr[2]); } // CompressorFactory::free() must run ~Compressor(): compressors are From 30730d63c25a1dcbb1bc41a5192724e201403045 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Fri, 13 Nov 2026 01:37:14 +0800 Subject: [PATCH 5/5] test(cpp): give reader-test files a per-process unique name Parallel CTest processes can seed the name generator in the same clock tick; one then removes the file another is opening (E_ALREADY_EXIST at the fixture's open, observed as SEGFAULT / test failure on CI). Append pid and a per-process counter, as the writer tests already do. --- cpp/test/reader/tsfile_reader_test.cc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cpp/test/reader/tsfile_reader_test.cc b/cpp/test/reader/tsfile_reader_test.cc index 060c7a5411..1fe277ddef 100644 --- a/cpp/test/reader/tsfile_reader_test.cc +++ b/cpp/test/reader/tsfile_reader_test.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -43,6 +44,12 @@ #include "reader/block/single_device_tsblock_reader.h" #include "reader/filter/time_operator.h" #include "reader/qds_without_timegenerator.h" + +#ifdef _WIN32 +#include +#else +#include +#endif #include "reader/tsfile_series_scan_iterator.h" #include "writer/chunk_writer.h" #include "writer/tsfile_writer.h" @@ -118,6 +125,18 @@ class TsFileReaderTest : public ::testing::Test { random_string += chars[dis(gen)]; } + // CTest runs test cases in parallel processes, and two of them can + // seed the generator in the same clock tick. Without a per-process + // unique suffix one process can remove the file another is using, + // which surfaced as E_ALREADY_EXIST from TsFileWriter::open. +#ifdef _WIN32 + const auto process_id = static_cast(_getpid()); +#else + const auto process_id = static_cast(getpid()); +#endif + static std::atomic counter{0}; + random_string += "_" + std::to_string(process_id) + "_" + + std::to_string(counter.fetch_add(1)); return random_string; }