Skip to content

Report zlib, brotli and zstd state in heap snapshots - #8686

Merged
littledivy merged 3 commits into
mainfrom
littledivy/2026-11-19-audit-api-memory-info
Nov 21, 2026
Merged

littledivy merged 3 commits into
mainfrom
littledivy/2026-11-19-audit-api-memory-info

Conversation

@littledivy

Copy link
Copy Markdown
Member

node:zlib's ZlibStream, BrotliCompressionStream and ZstdCompressionStream, and the CompressionCodec behind the TypeScript CompressionStream, had no memory info. A heap snapshot showed each one at its sizeof and left out the zlib, brotli or zstd state it holds. A deflate stream with Node's defaults holds about 285 KB, and a zstd encoder about 3.7 MB.

In this patch CompressionAllocator reports the sum of its live allocations, and the zstd contexts report ZSTD_sizeof_CCtx()/ZSTD_sizeof_DCtx(), since zstd allocates with malloc. CompressionCodec also reports its buffered output, and the node:zlib streams report their dictionary, callbacks and write result. The allocations are summed when a snapshot is built, so allocation and free don't change. Only heap snapshot output changes.

before after
zlib deflate stream 600 B 285 KB
brotli encoder 480 B 73 KB
zstd encoder 504 B 3.7 MB
deflate CompressionCodec 16.8 KB 334 KB

Measured by the new zlib-memory-test, which writes 64 KiB through each object and adds up its node and the native fields under it in a heap snapshot. It fails without the fix.

@littledivy
littledivy requested review from a team as code owners November 19, 2026 17:56
@littledivy
littledivy force-pushed the littledivy/2026-11-19-audit-api-memory-info branch from 7f021a3 to 9395ec7 Compare November 19, 2026 17:58

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 9395ec74.

This review, of 7f021a3d

💬 Approved with comments — The memory-info implementation is correct, and nothing blocks it. Two optional nits remain in the new test files: Bazel load order and a test comment that describes the code before this change.

Findings: 1 info · 1 suggestion (2 are inline comments on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info · 1 suggestion

The implementation is correct. Two small nits in the new test files.

Recommendation: 💬 Optional Nits

  • [INFO] The test comment describes how the streams behaved before this change. (src/workerd/api/node/tests/zlib-memory-test.c++:117)
  • [SUGGESTION] The kj_test load comes after the wd_test load, which breaks alphabetical load order. (src/workerd/api/node/tests/BUILD.bazel:4)
View full review

The memory-info methods are wired correctly. The allocator is counted once per object, the zstd contexts handle a null pointer, and OutputBuffer::size() returns bytes. There are two small nits in the new test files: the Bazel load order, and a test comment that describes the code before this change.


✅ All other checks passed

🔒 Security: ✅ LGTM

The change only adds read-only memory reporting for heap snapshots.


📖 AGENTS.md: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

The allocations are summed only when a snapshot is built, so the hot paths are unchanged.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: 7f021a3d

Comment thread src/workerd/api/node/tests/BUILD.bazel Resolved

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 8775e577.

This review, of 9395ec74

💬 Approved with comments — Nothing blocks this pull request. The load-order nit is fixed, and the only open item is an optional INFO about a test comment that describes how the code behaved before this change.

Findings: 1 info

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info

The load-order nit is fixed. One INFO remains on a test comment.

Recommendation: 💬 Optional Nits

  • [INFO] The test comment describes how the streams behaved before this change. (src/workerd/api/node/tests/zlib-memory-test.c++:115)
View full review
  • The kj_test load in src/workerd/api/node/tests/BUILD.bazel now comes before the wd_test load, so that nit is fixed.
  • src/workerd/api/node/tests/zlib-memory-test.c++:114-115: the comment still describes what heap snapshots showed before this change. The repository's comment guidelines put that history in the commit message.

The memory-info methods in compression.h, compression.c++ and zlib-util.h are consistent, and the new kj_test target has the dependencies it needs.


✅ All other checks passed

🔒 Security: ✅ LGTM

The change only adds read-only memory reporting for heap snapshots.


📖 AGENTS.md: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

The allocations are summed only when a heap snapshot is built, so the compression hot paths are unchanged. The ZSTD_sizeof_* calls are O(1).


📝 Documentation: ✅ LGTM

No documentation issues found. The test-comment note is under Code Quality.


Reviewed commit: 9395ec74

@littledivy
littledivy force-pushed the littledivy/2026-11-19-audit-api-memory-info branch from 9395ec7 to 8775e57 Compare November 19, 2026 18:02

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 50e661f5.

This review, of 8775e577

✅ Approved — The only open note from the previous review, about a test comment, is fixed, and nothing new came up. The change only adds memory reporting for heap snapshots and a test.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds read-only memory reporting for heap snapshots.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The test comments now give only the sizes measured at this commit. The memory-info methods are consistent with each other, and the new kj_test target has the dependencies it needs.


⚡ Performance: ✅ LGTM

The allocations are summed only when a heap snapshot is built, and the ZSTD_sizeof_* calls take constant time, so the compression hot paths are unchanged.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 8775e577

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 9dc78c81.

This review, of 50e661f5

✅ Approved — The only open note from the previous review, about a test comment, is fixed, and nothing new came up. The change only adds memory reporting for heap snapshots and a test.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds read-only memory reporting for heap snapshots.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The test comments now give only the sizes measured at this commit. The memory-info methods are consistent with each other, and the new kj_test target has the dependencies it needs.


⚡ Performance: ✅ LGTM

The allocations are summed only when a heap snapshot is built, and the ZSTD_sizeof_* calls take constant time, so the compression hot paths are unchanged.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 50e661f5 — same changes as 8775e577, so this is that commit's review (no new review was run; line numbers are as of 8775e577)

@littledivy
littledivy force-pushed the littledivy/2026-11-19-audit-api-memory-info branch from 50e661f to 9dc78c8 Compare November 20, 2026 18:01

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 9bba96b1.

This review, of 9dc78c81

✅ Approved — Nothing new came up, and the earlier kj::str(name) suggestion in the test has been applied. The change only adds memory reporting for heap snapshots and a test for it.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds read-only memory reporting for heap snapshots.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

Since the last review, only the test changed. nameIs now uses kj::str(name), as suggested in the earlier inline comment. The memory-info methods in compression.h, compression.c++ and zlib-util.h are consistent with each other. ZSTD_sizeof_CCtx and ZSTD_sizeof_DCtx return 0 for a null context, so a context that is not initialized yet is handled safely.


⚡ Performance: ✅ LGTM

The allocations are summed only when a heap snapshot is built, and the ZSTD_sizeof_* calls take constant time. Allocation, free and the rest of the compression hot path are unchanged.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 9dc78c81

@littledivy
littledivy enabled auto-merge November 21, 2026 02:53
The TypeScript CompressionStream and DecompressionStream drive a
CompressionCodec, which had no memory info. A heap snapshot showed it at
its sizeof, 16 KiB of inline scratch buffer, and left out the zlib or
brotli state its CompressionAllocator holds and the output it has
buffered. A deflate codec holds about 285 KB of zlib state.

In this patch CompressionAllocator reports the sum of its live
allocations as `allocations`, and CodecStage reports that plus its
buffered `output`. CompressionCodec reports its stage. The sum is taken
when a snapshot is built, so allocation and free do not change. The zstd
contexts report ZSTD_sizeof_CCtx() and ZSTD_sizeof_DCtx(), because zstd
allocates with malloc rather than the allocator. Only heap snapshot
output changes.
node:zlib's ZlibStream, BrotliCompressionStream and ZstdCompressionStream
had no memory info, so a heap snapshot showed each at its sizeof, about
500 bytes. A deflate stream with Node's defaults holds about 285 KB of
zlib state and a zstd encoder about 3.7 MB.

In this patch the streams' shared base reports its allocator, its
context and its callbacks and write result. ZlibContext reports its
dictionary and the zstd contexts their size. Only heap snapshot output
changes.
zlib-memory-test writes 64 KiB through a node:zlib deflate stream, a
brotli encoder, a zstd encoder and a deflate CompressionCodec, then takes
a heap snapshot and adds up each object's node and the native fields
under it. Without the memory info the streams show about 500 bytes and
the codec 16 KiB.

          before     after
  zlib    600 B      285 KB
  brotli  480 B      73 KB
  zstd    504 B      3.7 MB
  codec   16.8 KB    334 KB
@littledivy
littledivy force-pushed the littledivy/2026-11-19-audit-api-memory-info branch from 9dc78c8 to 9bba96b Compare November 21, 2026 02:56

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — No findings. The pull request only adds read-only memory reporting for heap snapshots and a test for it, and allocation and free are unchanged.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

The change only adds read-only memory reporting for heap snapshots. Allocation and free are unchanged, and the change does not handle untrusted input.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

The memory-info methods in compression.h, compression.c++ and zlib-util.h match each other.

  • zstd allocates with malloc, so the zstd contexts report their own size with ZSTD_sizeof_CCtx and ZSTD_sizeof_DCtx. Both return 0 for a null context, so a context that is not initialized yet is safe.
  • Brotli and zlib allocations are counted once, through CompressionAllocator::visitForMemoryInfo, so nothing is counted twice.
  • The test uses kj::str(name), as suggested earlier.

⚡ Performance: ✅ LGTM

The allocations are summed only when a heap snapshot is built, so the compression hot path is unchanged.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 9bba96b1

@littledivy
littledivy merged commit ca98c39 into main Nov 21, 2026
23 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-19-audit-api-memory-info branch November 21, 2026 03:15
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.

2 participants