Repository navigation
Report wrappables' shallow size to V8 as external memory - #8496
Conversation
There was a problem hiding this comment.
Superseded by the review of
4b61a3de.
This review, of ca0f3194
💬 Approved with comments — No correctness, security or performance defects found. Charges and releases pair up on every bind path, and the flush runs only outside GC. One optional suggestion to record the new invariant in the JSG guidance.
Findings: 1 suggestion (1 is an inline comment on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📖 AGENTS.md: 💬 1 suggestion
Consider adding the new Wrappable external-memory invariant to the JSG agent guidance.
Recommendation: 💬 Optional Nits
- [SUGGESTION] Document the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md. (
src/workerd/jsg/wrappable.h:643)
View full review
This change adds an invariant tied to how a Wrappable is bound to its isolate: each Wrappable is charged when it is first bound and released when it is destroyed, and the total is reported to V8 only from outside GC. A short note in src/workerd/jsg/AGENTS.md would help future changes to HeapTracer keep that invariant.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns in this change.
📋 Code Quality: ✅ LGTM
I traced the lifecycle of each charge and release, the points where the total is flushed, and the new tests:
- Charging and releasing. Every path that binds a Wrappable to an isolate charges it:
attachWrapper,linkRestoredWrapperandvisitRef.chargeWrappableis idempotent because it returns early whenaccountedBytes != 0.unlinkFromTracerreleases the charge, andunbindclearsaccountedByteswhen the isolate is torn down, so a later isolate never releases a charge it did not make. StoringaccountedByteshandles two cases correctly: the subclass destructors run before~Wrappable, and the autogate can change while charged Wrappables are still alive. - Where the flush runs.
flushExternalMemory()runs only inattachWrapper, before the no-GC region begins, and inapplyDeferredActionsunder the isolate lock. The pending total is zeroed before the call into V8, so a GC inside that call can neither lose a charge nor count it twice. - Tests. The tests cover the charged, released and gate-off cases. The gate-off test ignores all autogates, so it stays valid in the
@all-autogatesvariant.
⚡ Performance: ✅ LGTM
Reporting in 64 KB batches keeps calls into V8 off the hot path. The new fields fit in existing padding, so a Wrappable does not grow.
📝 Documentation: ✅ LGTM
The new header comments match the behavior of the code.
Reviewed commit: ca0f3194
ca0f319 to
4b61a3d
Compare
There was a problem hiding this comment.
Superseded by the review of
a2ad69e7.
This review, of 4b61a3de
💬 Approved with comments — Charges and releases still pair up on every bind path at this commit, and the total is only reported to V8 outside GC. The one remaining note is an optional documentation suggestion.
Findings: 1 suggestion
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📖 AGENTS.md: 💬 1 suggestion
The new Wrappable accounting invariant is not yet written down in the JSG guidance.
Recommendation: 💬 Optional Nits
- [SUGGESTION] Document the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md. (
src/workerd/jsg/wrappable.h:643)
View full review
src/workerd/jsg/wrappable.h:643-650 adds an accounting invariant: each Wrappable is charged when it is first bound to an isolate and released when it is destroyed, and the total is reported to V8 only from outside GC. A short note in src/workerd/jsg/AGENTS.md would help future changes to HeapTracer keep that invariant.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns in this change.
📋 Code Quality: ✅ LGTM
I re-checked the changes to wrappable.c++ and wrappable.h since the last review.
- Charging and releasing: Every path that binds a Wrappable to an isolate charges it:
attachWrapper,linkRestoredWrapperandvisitRef.chargeWrappablereturns early whenaccountedBytesis already non-zero, so binding the same Wrappable twice does not charge it twice.unlinkFromTracerreleases the charge.unbindclearsaccountedBytes, so a Wrappable that outlives its isolate never releases a charge against a later isolate.
- Where the total is reported:
flushExternalMemory()runs only inattachWrapper, before the no-GC region, and inapplyDeferredActions.- The pending total is zeroed before the call into V8, so a GC inside that call can neither lose a charge nor count it twice.
- The
isolate == nullptrguard covers a flush after the isolate is torn down.
- Size limit: Each charge is capped at
UINT16_MAX, so a Wrappable larger than 64 KB is under-reported. This trade-off does not affect correctness. - Tests: They cover the gate-on and gate-off cases.
⚡ Performance: ✅ LGTM
Reporting in 64 KB batches keeps calls into V8 off the hot path.
📝 Documentation: ✅ LGTM
The new header comments match the behavior of the code.
Reviewed commit: 4b61a3de
16dde57 to
a2ad69e
Compare
There was a problem hiding this comment.
Superseded by the review of
98353ff8.
This review, of a2ad69e7
💬 Approved with comments — Charges and releases still pair up on every bind path at this commit, and the total is only reported to V8 outside GC. The one remaining note is an optional documentation suggestion.
Findings: 1 suggestion
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📖 AGENTS.md: 💬 1 suggestion
The new Wrappable accounting invariant is not yet written down in the JSG guidance.
Recommendation: 💬 Optional Nits
- [SUGGESTION] Document the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md. (
src/workerd/jsg/wrappable.h:643)
View full review
src/workerd/jsg/wrappable.h:643-650 adds an accounting invariant: each Wrappable is charged when it is first bound to an isolate and released when it is destroyed, and the total is reported to V8 only from outside GC. A short note in src/workerd/jsg/AGENTS.md would help future changes to HeapTracer keep that invariant.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns in this change.
📋 Code Quality: ✅ LGTM
I re-checked the changes to wrappable.c++ and wrappable.h since the last review.
- Charging and releasing: Every path that binds a Wrappable to an isolate charges it:
attachWrapper,linkRestoredWrapperandvisitRef.chargeWrappablereturns early whenaccountedBytesis already non-zero, so binding the same Wrappable twice does not charge it twice.unlinkFromTracerreleases the charge.unbindclearsaccountedBytes, so a Wrappable that outlives its isolate never releases a charge against a later isolate.
- Where the total is reported:
flushExternalMemory()runs only inattachWrapper, before the no-GC region, and inapplyDeferredActions.- The pending total is zeroed before the call into V8, so a GC inside that call can neither lose a charge nor count it twice.
- The
isolate == nullptrguard covers a flush after the isolate is torn down.
- Size limit: Each charge is capped at
UINT16_MAX, so a Wrappable larger than 64 KB is under-reported. This trade-off does not affect correctness. - Tests: They cover the gate-on and gate-off cases.
⚡ Performance: ✅ LGTM
Reporting in 64 KB batches keeps calls into V8 off the hot path.
📝 Documentation: ✅ LGTM
The new header comments match the behavior of the code.
Reviewed commit: a2ad69e7 — same changes as 4b61a3de, so this is that commit's review (no new review was run; line numbers are as of 4b61a3de)
a2ad69e to
98353ff
Compare
There was a problem hiding this comment.
Superseded by the review of
9c63fff2.
This review, of 98353ff8
💬 Approved with comments — Charges and releases still pair up on every bind path at this commit, and the total is only reported to V8 outside GC. The one remaining note is an optional documentation suggestion.
Findings: 1 suggestion
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📖 AGENTS.md: 💬 1 suggestion
The new Wrappable accounting invariant is not yet written down in the JSG guidance.
Recommendation: 💬 Optional Nits
- [SUGGESTION] Document the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md. (
src/workerd/jsg/wrappable.h:643)
View full review
src/workerd/jsg/wrappable.h:643-650 adds an accounting invariant: each Wrappable is charged when it is first bound to an isolate and released when it is destroyed, and the total is reported to V8 only from outside GC. A short note in src/workerd/jsg/AGENTS.md would help future changes to HeapTracer keep that invariant.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns in this change.
📋 Code Quality: ✅ LGTM
I re-checked the changes to wrappable.c++ and wrappable.h since the last review.
- Charging and releasing: Every path that binds a Wrappable to an isolate charges it:
attachWrapper,linkRestoredWrapperandvisitRef.chargeWrappablereturns early whenaccountedBytesis already non-zero, so binding the same Wrappable twice does not charge it twice.unlinkFromTracerreleases the charge.unbindclearsaccountedBytes, so a Wrappable that outlives its isolate never releases a charge against a later isolate.
- Where the total is reported:
flushExternalMemory()runs only inattachWrapper, before the no-GC region, and inapplyDeferredActions.- The pending total is zeroed before the call into V8, so a GC inside that call can neither lose a charge nor count it twice.
- The
isolate == nullptrguard covers a flush after the isolate is torn down.
- Size limit: Each charge is capped at
UINT16_MAX, so a Wrappable larger than 64 KB is under-reported. This trade-off does not affect correctness. - Tests: They cover the gate-on and gate-off cases.
⚡ Performance: ✅ LGTM
Reporting in 64 KB batches keeps calls into V8 off the hot path.
📝 Documentation: ✅ LGTM
The new header comments match the behavior of the code.
Reviewed commit: 98353ff8 — same changes as 4b61a3de, so this is that commit's review (no new review was run; line numbers are as of 4b61a3de)
98353ff to
9c63fff
Compare
There was a problem hiding this comment.
Superseded by the review of
41f6ef9c.
This review, of 9c63fff2
💬 Approved with comments — Charges and releases still pair up on every bind path at this commit, and the total is only reported to V8 outside GC. The one remaining note is an optional documentation suggestion.
Findings: 1 suggestion
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📖 AGENTS.md: 💬 1 suggestion
The new Wrappable accounting invariant is not yet written down in the JSG guidance.
Recommendation: 💬 Optional Nits
- [SUGGESTION] Document the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md. (
src/workerd/jsg/wrappable.h:643)
View full review
src/workerd/jsg/wrappable.h:643-650 adds an accounting invariant: each Wrappable is charged when it is first bound to an isolate and released when it is destroyed, and the total is reported to V8 only from outside GC. A short note in src/workerd/jsg/AGENTS.md would help future changes to HeapTracer keep that invariant.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns in this change.
📋 Code Quality: ✅ LGTM
I re-checked the changes to wrappable.c++ and wrappable.h since the last review.
- Charging and releasing: Every path that binds a Wrappable to an isolate charges it:
attachWrapper,linkRestoredWrapperandvisitRef.chargeWrappablereturns early whenaccountedBytesis already non-zero, so binding the same Wrappable twice does not charge it twice.unlinkFromTracerreleases the charge.unbindclearsaccountedBytes, so a Wrappable that outlives its isolate never releases a charge against a later isolate.
- Where the total is reported:
flushExternalMemory()runs only inattachWrapper, before the no-GC region, and inapplyDeferredActions.- The pending total is zeroed before the call into V8, so a GC inside that call can neither lose a charge nor count it twice.
- The
isolate == nullptrguard covers a flush after the isolate is torn down.
- Size limit: Each charge is capped at
UINT16_MAX, so a Wrappable larger than 64 KB is under-reported. This trade-off does not affect correctness. - Tests: They cover the gate-on and gate-off cases.
⚡ Performance: ✅ LGTM
Reporting in 64 KB batches keeps calls into V8 off the hot path.
📝 Documentation: ✅ LGTM
The new header comments match the behavior of the code.
Reviewed commit: 9c63fff2 — same changes as 4b61a3de, so this is that commit's review (no new review was run; line numbers are as of 4b61a3de)
9c63fff to
41f6ef9
Compare
There was a problem hiding this comment.
Superseded by the review of
c7039857.
This review, of 41f6ef9c
💬 Approved with comments — Charges and releases still pair up on every bind path, and the total is reported to V8 only outside GC. The one remaining note is an optional documentation suggestion.
Findings: 1 suggestion
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📖 AGENTS.md: 💬 1 suggestion
The external-memory accounting invariant is not yet documented in the JSG agent guidance.
Recommendation: 💬 Optional Nits
- [SUGGESTION] Document the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md. (
src/workerd/jsg/wrappable.h:643)
View full review
src/workerd/jsg/wrappable.h:643-650 introduces an invariant: each Wrappable is charged when it is first bound to an isolate and released when it is destroyed, and the total is reported to V8 only from outside GC. A short note in src/workerd/jsg/AGENTS.md would help future changes keep it.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns in this change.
📋 Code Quality: ✅ LGTM
I re-checked the changes to wrappable.h and tracing-test.c++ since the last review and found no correctness problems.
- Charging and releasing:
chargeWrappablereturns early whenaccountedBytesis already non-zero, so a Wrappable is charged only once.unlinkFromTracerreleases the charge, andunbindclears it. - Where the total is reported:
flushExternalMemoryruns only inattachWrapper, before the no-GC region, and inapplyDeferredActions. It zeroes the pending total before calling into V8, so a GC inside that call can neither lose a charge nor count it twice. - Tests: They cover the gate-on and gate-off cases.
- The
pending < kExternalMemoryFlushBytesassertion holds:NumberBoxholds noRefs, so onlyattachWrappercharges it, andattachWrapperflushes straight after each charge.
- The
⚡ Performance: ✅ LGTM
Reporting in 64 KB batches keeps calls into V8 off the hot path.
📝 Documentation: ✅ LGTM
The header comments match the behavior of the code.
Reviewed commit: 41f6ef9c
41f6ef9 to
c703985
Compare
There was a problem hiding this comment.
Superseded by the review of
bfe528d9.
This review, of c7039857
💬 Approved with comments — Charges and releases still pair up on every bind path, and the total is reported to V8 only outside GC. The one remaining note is an optional documentation suggestion.
Findings: 1 suggestion
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
📖 AGENTS.md: 💬 1 suggestion
The external-memory accounting invariant is not yet documented in the JSG agent guidance.
Recommendation: 💬 Optional Nits
- [SUGGESTION] Document the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md. (
src/workerd/jsg/wrappable.h:643)
View full review
src/workerd/jsg/wrappable.h:643-650 introduces an invariant: each Wrappable is charged when it is first bound to an isolate and released when it is destroyed, and the total is reported to V8 only from outside GC. A short note in src/workerd/jsg/AGENTS.md would help future changes keep it.
✅ All other checks passed
🔒 Security: ✅ LGTM
No security concerns in this change.
📋 Code Quality: ✅ LGTM
I re-checked the changes to wrappable.h and tracing-test.c++ since the last review and found no correctness problems.
- Charging and releasing:
chargeWrappablereturns early whenaccountedBytesis already non-zero, so a Wrappable is charged only once.unlinkFromTracerreleases the charge, andunbindclears it. - Where the total is reported:
flushExternalMemoryruns only inattachWrapper, before the no-GC region, and inapplyDeferredActions. It zeroes the pending total before calling into V8, so a GC inside that call can neither lose a charge nor count it twice. - Tests: They cover the gate-on and gate-off cases.
- The
pending < kExternalMemoryFlushBytesassertion holds:NumberBoxholds noRefs, so onlyattachWrappercharges it, andattachWrapperflushes straight after each charge.
- The
⚡ Performance: ✅ LGTM
Reporting in 64 KB batches keeps calls into V8 off the hot path.
📝 Documentation: ✅ LGTM
The header comments match the behavior of the code.
Reviewed commit: c7039857 — same changes as 41f6ef9c, so this is that commit's review (no new review was run; line numbers are as of 41f6ef9c)
c703985 to
bfe528d
Compare
There was a problem hiding this comment.
Superseded by the review of
4625de9f.
This review, of bfe528d9
✅ Approved — No issues at this commit. Every charge has a matching release on each bind path, and the total is reported to V8 only from outside GC.
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
This is a memory-accounting change and crosses no security boundary.
📖 AGENTS.md: ✅ LGTM
One earlier optional suggestion is still open: noting the accounting invariant in src/workerd/jsg/AGENTS.md.
📋 Code Quality: ✅ LGTM
Charges and releases pair up across attachWrapper, linkRestoredWrapper and visitRef, and the early return on accountedBytes prevents a second charge. unbind clears the charge so a later isolate does not release it, and flushExternalMemory zeroes the pending total before it calls V8. Sizes above UINT16_MAX are under-reported rather than wrapped.
⚡ Performance: ✅ LGTM
Reporting in 64 KB batches keeps calls into V8 off the hot path.
📝 Documentation: ✅ LGTM
The header comments match what the code does.
Reviewed commit: bfe528d9
bfe528d to
4625de9
Compare
There was a problem hiding this comment.
Superseded by the review of
c81e4180.
This review, of 4625de9f
✅ Approved — I found no issues at this commit. The new tests cover the charge and release cycle with the autogate on and off, and the production code is unchanged since the last review.
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
This change only does memory accounting and crosses no security boundary.
📖 AGENTS.md: ✅ LGTM
One earlier optional note is still open: documenting the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md.
📋 Code Quality: ✅ LGTM
The new tests in tracing-test.c++ use the existing autogate test helpers correctly. Teardown runs in the right order: the isolate is destroyed before deinitAutogate(). Charges and releases still pair up on every bind path.
⚡ Performance: ✅ LGTM
Charges and releases are plain integer updates. The total is reported to V8 in 64 KB batches, outside GC. The new tests add no production cost.
📝 Documentation: ✅ LGTM
The header comments match what the code does.
Reviewed commit: 4625de9f
V8 sees a JSG object only as its small CppgcShim, so a loop that creates and drops API objects gives V8 no reason to run a major GC, and the C++ objects pile up until one runs. Behind the WRAPPABLE_EXTERNAL_MEMORY autogate, charge each Wrappable its jsgGetMemorySelfSize() once when it is first bound to an isolate, and release it when it is destroyed. Both happen under that isolate's lock, so the total is a plain per-isolate integer. It is reported to V8 in 64 KB batches from attachWrapper() and lock acquisition, never during GC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4625de9 to
c81e418
Compare
There was a problem hiding this comment.
Superseded by the review of
32b52c65.
This review, of c81e4180
✅ Approved — I found no issues at this commit. Charges and releases still pair up on every bind path, and the new tests cover the change with the autogate on and off.
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
This change only does memory accounting and crosses no security boundary.
📖 AGENTS.md: ✅ LGTM
One optional note from earlier reviews is still open: documenting the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md.
📋 Code Quality: ✅ LGTM
Charges and releases pair up across attachWrapper(), linkRestoredWrapper(), visitRef() and unlinkFromTracer(). unbind() zeroes accountedBytes at isolate teardown.
Every direct Wrappable subclass overrides jsgGetMemorySelfSize(), so the call in attachWrapper() cannot throw.
The new tests use the autogate test helpers correctly and tear down in the right order.
⚡ Performance: ✅ LGTM
Charges and releases are plain integer updates. The total is reported to V8 in 64 KB batches, outside GC.
📝 Documentation: ✅ LGTM
The header and inline comments match what the code does.
Reviewed commit: c81e4180
The per-isolate total is now kept whatever the WRAPPABLE_EXTERNAL_MEMORY autogate says, and exposed as HeapTracer::getChargedWrappableBytes(); the gate only controls whether flushExternalMemory() reports it to V8. An embedder can then observe how much this would add per isolate before turning anything on. Counting makes no V8 calls and adds no per-object memory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
✅ Approved — No issues at this commit: charges and releases still pair up on every bind path, and counting with the autogate off is correct and covered by tests.
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
This change only does memory accounting and crosses no security boundary.
📖 AGENTS.md: ✅ LGTM
One optional note from earlier reviews is still open: documenting the Wrappable external-memory accounting invariant in src/workerd/jsg/AGENTS.md.
📋 Code Quality: ✅ LGTM
The total is now counted whatever the autogate says. Charges and releases still pair up across attachWrapper(), linkRestoredWrapper(), visitRef() and unlinkFromTracer(), and unbind() zeroes accountedBytes at isolate teardown. flushExternalMemory() returns early while the gate is off, so nothing is reported to V8, and getChargedWrappableBytes() still gives the total.
⚡ Performance: ✅ LGTM
Counting is always on now, but charges and releases are plain integer updates under the isolate lock. Reporting to V8 still happens in 64 KB batches, outside GC.
📝 Documentation: ✅ LGTM
The header comment and the autogate comment match what the code does at this commit.
Reviewed commit: 32b52c65
Per-run memory growth is API objects left in long-lived isolates, each holding 10-25 KiB of C++ memory that only a major GC frees. Turn on the workerd-autogate-wrappable-external-memory gate (cloudflare/workerd#8496) so V8 collects in time, and make the memory test a real check: warm up past the first major GC, take the lowest of four readings, 8 MiB limit, no todo.
V8 sees a JSG object only as its small CppgcShim, so a loop that creates and drops API objects gives V8 no reason to run a major GC, and the C++ objects pile up until one runs.
Behind the WRAPPABLE_EXTERNAL_MEMORY autogate, charge each Wrappable its jsgGetMemorySelfSize() once when it is first bound to an isolate, and release it when it is destroyed. Both happen under that isolate's lock, so the total is a plain per-isolate integer. It is reported to V8 in 64 KB batches from attachWrapper() and lock acquisition, never during GC.