Skip to content

Report wrappables' shallow size to V8 as external memory - #8496

Merged
littledivy merged 2 commits into
mainfrom
littledivy/2026-11-16-wrappable-external-memory
Nov 18, 2026
Merged

littledivy merged 2 commits into
mainfrom
littledivy/2026-11-16-wrappable-external-memory

Conversation

@littledivy

Copy link
Copy Markdown
Member

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.

@littledivy
littledivy requested review from a team as code owners November 16, 2026 14:45

@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 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, linkRestoredWrapper and visitRef. chargeWrappable is idempotent because it returns early when accountedBytes != 0. unlinkFromTracer releases the charge, and unbind clears accountedBytes when the isolate is torn down, so a later isolate never releases a charge it did not make. Storing accountedBytes handles 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 in attachWrapper, before the no-GC region begins, and in applyDeferredActions under 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-autogates variant.

⚡ 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

Comment thread src/workerd/jsg/wrappable.h Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-16-wrappable-external-memory branch from ca0f319 to 4b61a3d Compare November 16, 2026 15:18

@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 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, linkRestoredWrapper and visitRef.
    • chargeWrappable returns early when accountedBytes is already non-zero, so binding the same Wrappable twice does not charge it twice.
    • unlinkFromTracer releases the charge.
    • unbind clears accountedBytes, so a Wrappable that outlives its isolate never releases a charge against a later isolate.
  • Where the total is reported: flushExternalMemory() runs only in attachWrapper, before the no-GC region, and in applyDeferredActions.
    • 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 == nullptr guard 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

@littledivy
littledivy force-pushed the littledivy/2026-11-16-wrappable-external-memory branch 2 times, most recently from 16dde57 to a2ad69e Compare November 16, 2026 16:07

@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 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, linkRestoredWrapper and visitRef.
    • chargeWrappable returns early when accountedBytes is already non-zero, so binding the same Wrappable twice does not charge it twice.
    • unlinkFromTracer releases the charge.
    • unbind clears accountedBytes, so a Wrappable that outlives its isolate never releases a charge against a later isolate.
  • Where the total is reported: flushExternalMemory() runs only in attachWrapper, before the no-GC region, and in applyDeferredActions.
    • 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 == nullptr guard 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)

@littledivy
littledivy force-pushed the littledivy/2026-11-16-wrappable-external-memory branch from a2ad69e to 98353ff Compare November 16, 2026 16:30

@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 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, linkRestoredWrapper and visitRef.
    • chargeWrappable returns early when accountedBytes is already non-zero, so binding the same Wrappable twice does not charge it twice.
    • unlinkFromTracer releases the charge.
    • unbind clears accountedBytes, so a Wrappable that outlives its isolate never releases a charge against a later isolate.
  • Where the total is reported: flushExternalMemory() runs only in attachWrapper, before the no-GC region, and in applyDeferredActions.
    • 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 == nullptr guard 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)

@littledivy
littledivy force-pushed the littledivy/2026-11-16-wrappable-external-memory branch from 98353ff to 9c63fff Compare November 16, 2026 18:32

@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 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, linkRestoredWrapper and visitRef.
    • chargeWrappable returns early when accountedBytes is already non-zero, so binding the same Wrappable twice does not charge it twice.
    • unlinkFromTracer releases the charge.
    • unbind clears accountedBytes, so a Wrappable that outlives its isolate never releases a charge against a later isolate.
  • Where the total is reported: flushExternalMemory() runs only in attachWrapper, before the no-GC region, and in applyDeferredActions.
    • 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 == nullptr guard 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)

@littledivy
littledivy force-pushed the littledivy/2026-11-16-wrappable-external-memory branch from 9c63fff to 41f6ef9 Compare November 17, 2026 07:22

@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 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: chargeWrappable returns early when accountedBytes is already non-zero, so a Wrappable is charged only once. unlinkFromTracer releases the charge, and unbind clears it.
  • Where the total is reported: flushExternalMemory runs only in attachWrapper, before the no-GC region, and in applyDeferredActions. 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 < kExternalMemoryFlushBytes assertion holds: NumberBox holds no Refs, so only attachWrapper charges it, and attachWrapper flushes straight after each charge.

⚡ 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

@littledivy
littledivy force-pushed the littledivy/2026-11-16-wrappable-external-memory branch from 41f6ef9 to c703985 Compare November 17, 2026 07:40

@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 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: chargeWrappable returns early when accountedBytes is already non-zero, so a Wrappable is charged only once. unlinkFromTracer releases the charge, and unbind clears it.
  • Where the total is reported: flushExternalMemory runs only in attachWrapper, before the no-GC region, and in applyDeferredActions. 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 < kExternalMemoryFlushBytes assertion holds: NumberBox holds no Refs, so only attachWrapper charges it, and attachWrapper flushes straight after each charge.

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

@littledivy
littledivy force-pushed the littledivy/2026-11-16-wrappable-external-memory branch from c703985 to bfe528d Compare November 17, 2026 08:11

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

@littledivy
littledivy force-pushed the littledivy/2026-11-16-wrappable-external-memory branch from bfe528d to 4625de9 Compare November 17, 2026 08:43

@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 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>
@littledivy
littledivy force-pushed the littledivy/2026-11-16-wrappable-external-memory branch from 4625de9 to c81e418 Compare November 18, 2026 04:18

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

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

@littledivy
littledivy merged commit 73eeb4c into main Nov 18, 2026
23 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-16-wrappable-external-memory branch November 18, 2026 06:15
mikef-eng added a commit to chelabase/chelabase that referenced this pull request Nov 22, 2026
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.
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