Skip to content

feat: support create_external_arraybuffer#30

Merged
wemeetagain merged 2 commits into
mainfrom
bing/ext-ab
May 14, 2026
Merged

feat: support create_external_arraybuffer#30
wemeetagain merged 2 commits into
mainfrom
bing/ext-ab

Conversation

@spiral-ladder
Copy link
Copy Markdown
Contributor

ported from ChainSafe/lodestar-z#356

External array buffers have their lifetimes managed by V8's garbage collector, but their backing memory is still managed by the native implementation.

We need to call adjustExternalMemory to let V8 know about the native allocations; and we need a finalizer to cleanup such allocations (which we add in this PR, and use in lodestar-z)

More details from that PR:

This is one of possible likely causes for increased GC pressure on experiments to swap out blst-ts for lodestar-z/bls, as observed on feat2 and feat3 deployments in this PR.

With external array buffers, V8 is only aware of the pointer to the backing memory, instead of having to track both the pointer and the backing memory. This means that during marking phase the GC does not have to walk the backing memory to mark it as 'live' - the frequency of the GC firing off is still the same, but each cycle does less work.

This of course comes with a tradeoff, we need a finalizer to let V8 know how much external memory is in native heap so that the GC tells the native impl to free the useless memory.

Though, regardless of the effect, we should still probably do this anyway, since napi-rs does the same, and only defaults to V8 managed array buffers if it is disallowed (like in Electron).

ported from ChainSafe/lodestar-z#356

External array buffers have their lifetimes managed by V8's garbage
collector, but their backing memory is still managed by the native
implementation.

We need to call `adjustExternalMemory` to let V8 know about the
native allocations; and we need a finalizer to cleanup such
allocations (which we add in this PR, and use in lodestar-z)

More details from that PR:

> This is one of possible likely causes for increased GC pressure on experiments to swap out blst-ts for lodestar-z/bls, as observed on feat2 and feat3 deployments in [this PR](ChainSafe/lodestar#9342).
> 
> With external array buffers, V8 is only aware of the pointer to the backing memory, instead of having to track both the pointer and the backing memory. This means that during marking phase the GC does not have to walk the backing memory to mark it as 'live' - the frequency of the GC firing off is still the same, but each cycle does less work.
> 
> This of course comes with a tradeoff, we need a **finalizer** to let V8 know how much external memory is in native heap  so that the GC tells the native impl to free the useless memory.
> 
> Though, regardless of the effect, we should still probably do this anyway, since [napi-rs does the same](https://github.com/napi-rs/napi-rs/blob/159395b365c583a6642ad481edc5708d9f36a24b/crates/napi/src/bindgen_runtime/js_values/arraybuffer.rs#L175), and only defaults to V8 managed array buffers if it is disallowed (like in Electron).
Copy link
Copy Markdown
Member

@wemeetagain wemeetagain left a comment

Choose a reason for hiding this comment

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

lgtm, will wait for review from @nazarhussain

Copy link
Copy Markdown
Contributor

@nazarhussain nazarhussain left a comment

Choose a reason for hiding this comment

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

LGTM. A plus would be to add some example in this file https://github.com/ChainSafe/zapi/blob/main/examples/js_dsl/mod.zig

@spiral-ladder
Copy link
Copy Markdown
Contributor Author

added in 5363899

@wemeetagain wemeetagain merged commit a76b604 into main May 14, 2026
5 checks passed
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.

3 participants