Skip to content

perf: avoid extra metadata copies when packing - #95

Merged
blocksmith314 merged 2 commits into
mainfrom
perf/93-pack-metadata-rss
Nov 22, 2026
Merged

blocksmith314 merged 2 commits into
mainfrom
perf/93-pack-metadata-rss

Conversation

@blocksmith314

Copy link
Copy Markdown
Owner

Summary

  • Remove the unconditional stored_meta = meta_buffer copy and the zero-filled worst-case std::string used as the metadata compression destination.
  • Compress into an uninitialized raw buffer; only the compressed output pages are touched. Write meta_buffer directly when compression is disabled or does not shrink.
  • Archive bytes are unchanged; the release benchmark table records the post-fix pack RSS.

Root cause

On the release dataset the metadata is ~28.9 MB raw and ~7.0 MB compressed (LZ4 4.11x). The old code held the raw metadata, a full copy, and a zero-filled worst-case buffer at the same time; v1.1.1 held only one metadata buffer. This explains the ~4-9% pack peak-RSS drift reported in #93.

Tests

  • Release ctest: 177/177 passed
  • ASan ctest: 177/177 passed

Performance

  • Pre-fix main: pack peak RSS 887-926 MiB across runs.
  • Post-fix: 852-878 MiB across three 10-run comparisons; one comparison measured 852.3 vs 854.1 MiB against v1.1.1 (-0.2%).
  • This machine's run-to-run RSS noise is around +/-20 MiB. The allocation reduction is deterministic in the code, but the next release measurement should confirm the number.

Closes #93

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
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

@blocksmith314 blocksmith314 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

LGTM. The change removes both avoidable metadata copies correctly:

  • compression disabled: stored_metadata points at meta_buffer; no copy
  • compression enabled and smaller: output goes into an uninitialized make_unique_for_overwrite buffer, and only the compressed bytes are written
  • compression not shrinking / failing: falls back to the raw meta_buffer after releasing the scratch buffer
  • lifetime is safe: both meta_buffer and compressed_meta outlive the synchronous RandomWrite

The bytes written are the same as before; the header sizes/types/checksum are still set before the write.

Verified locally: Release ctest 177/177, ASan ctest 177/177, CI 5/5 green.

Minor suggestions, not blocking:

  • A regression test that packs the same fixture through both the compressed and fallback paths and compares the stored metadata bytes/round-trip would pin the "archive bytes unchanged" claim.
  • The benchmark row records afdeb6b while the docs commit is abb42c8; that is fine, just noting the table references the code commit.

@blocksmith314
blocksmith314 merged commit b2fc3a6 into main Nov 22, 2026
5 checks passed
@blocksmith314
blocksmith314 deleted the perf/93-pack-metadata-rss branch November 22, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Perf: pack peak RSS is ~4-9% above v1.1.1

1 participant