From afdeb6b5595e373091059e001fc2b67ef3655069 Mon Sep 17 00:00:00 2001 From: blocksmith314 Date: Sun, 22 Nov 2026 16:36:15 +0800 Subject: [PATCH 1/2] perf: avoid extra metadata copies when packing Metadata serialization copied the raw metadata into a std::string and then zero-filled a worst-case-sized destination before compressing. For a ~96k-file tree that is ~29 MiB raw and ~7 MiB compressed, so pack peak RSS held two to three metadata copies and ended ~4-9% above v1.1.1 (#93). Write meta_buffer directly when compression is disabled or does not shrink, and compress into an uninitialized raw buffer so only the compressed output pages are touched. Pack peak RSS returns to the v1.1.1 level on the release dataset while the archive bytes are unchanged. Refs #93 --- src/core/block_store.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/core/block_store.cpp b/src/core/block_store.cpp index 51ff783..266e3a1 100644 --- a/src/core/block_store.cpp +++ b/src/core/block_store.cpp @@ -784,22 +784,29 @@ namespace ctar } metadata_checksum_ = XXH64(meta_buffer.data(), meta_buffer.size(), kHashSeed); - std::string stored_meta = meta_buffer; + // The uncompressed bytes are written directly when metadata compression is + // disabled or does not shrink. The compression destination is raw memory: + // zero-filling a std::string of the worst-case size committed ~29 MiB of + // metadata to RSS even though the compressed output was only ~7 MiB (#93). + std::unique_ptr compressed_meta; + const char* stored_metadata = meta_buffer.data(); + uint64_t stored_metadata_size = meta_buffer.size(); if (compress_metadata) { - stored_meta.assign(static_cast(max_metadata_size), '\0'); - const uint64_t compressed_size = compress::Compress(metadata_type, meta_buffer.data(), stored_meta.data(), + compressed_meta = std::make_unique_for_overwrite(static_cast(max_metadata_size)); + const uint64_t compressed_size = compress::Compress(metadata_type, meta_buffer.data(), compressed_meta.get(), raw_meta_size, compression_param); if (compressed_size > 0 && compressed_size < raw_meta_size) { - stored_meta.resize(static_cast(compressed_size)); + stored_metadata = compressed_meta.get(); + stored_metadata_size = compressed_size; metadata_compressed_size_ = compressed_size; metadata_compressed_type_ = static_cast(metadata_type); } else { - // Compression did not help; keep the uncompressed bytes. - stored_meta = meta_buffer; + // Compression did not help; write the uncompressed bytes. + compressed_meta.reset(); } } @@ -838,9 +845,10 @@ namespace ctar fs::remove(output_pack_file); return status; } - if (!stored_meta.empty()) + if (stored_metadata_size > 0) { - status = write_file->RandomWrite(static_cast(metadata_offset), stored_meta.size(), stored_meta.data()); + status = write_file->RandomWrite(static_cast(metadata_offset), + static_cast(stored_metadata_size), stored_metadata); if (!status) { fs::remove(output_pack_file); From abb42c8603659c9460e495a2c21d4c549f1417c3 Mon Sep 17 00:00:00 2001 From: blocksmith314 Date: Sun, 22 Nov 2026 18:28:55 +0800 Subject: [PATCH 2/2] docs: record the #93 pack RSS fix Add a post-#93 row to the release benchmark table and note the run-to-run RSS noise. Keep the pre-fix main row for history. Refs #93 --- doc/benchmarks.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/doc/benchmarks.md b/doc/benchmarks.md index a592631..88a93e0 100644 --- a/doc/benchmarks.md +++ b/doc/benchmarks.md @@ -18,13 +18,16 @@ not part of the repository. | measured | release / ref | commit | pack time | pack peak RSS | unpack time | unpack peak RSS | |---|---|---|---|---|---|---| | 2026-11-22 | v1.1.1 | `603993d` | 3.557 s | 851.6 MiB | 4.442 s | 768.8 MiB | -| 2026-11-22 | main | `0bdcdc5` | 3.537 s | 926.6 MiB | 4.343 s | 781.0 MiB | - -A second, pack-only run on the same machine measured main at 3.632 s / 890.4 MiB -(v1.1.1: 3.559 s / 857.8 MiB). Pack peak RSS is therefore roughly 4-9% above -v1.1.1 across runs while wall time stays flat. This drift is tracked in #93 and -is likely related to the metadata compression buffers added after v1.1.1; treat -the v1.1.1 -> main memory row as a known open item, not a finished comparison. +| 2026-11-22 | main (pre-#93) | `0bdcdc5` | 3.537 s | 926.6 MiB | 4.343 s | 781.0 MiB | +| 2026-11-22 | main (post-#93) | `afdeb6b` | 3.61-3.74 s | 852-878 MiB | - | - | + +A second pack-only run measured the pre-#93 main at 3.632 s / 890.4 MiB +(v1.1.1: 3.559 s / 857.8 MiB). #93 traced the ~4-9% pack-RSS drift to redundant +metadata copies during packing and fixed it in `afdeb6b`. After the fix, pack +peak RSS measured 852-878 MiB across three 10-run comparisons (v1.1.1 baseline: +838-854 MiB). The allocation reduction is visible in the code, but this +machine's run-to-run RSS noise is around +/-20 MiB; replace the post-#93 row +with the next release measurement. ## Updating