feat: support create_external_arraybuffer#30
Merged
Merged
Conversation
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).
wemeetagain
reviewed
May 13, 2026
Member
wemeetagain
left a comment
There was a problem hiding this comment.
lgtm, will wait for review from @nazarhussain
nazarhussain
approved these changes
May 13, 2026
Contributor
nazarhussain
left a comment
There was a problem hiding this comment.
LGTM. A plus would be to add some example in this file https://github.com/ChainSafe/zapi/blob/main/examples/js_dsl/mod.zig
Contributor
Author
|
added in 5363899 |
wemeetagain
approved these changes
May 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
adjustExternalMemoryto 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: