perf(dsl): skip placeholder pattern in materializeClassInstance#37
Merged
Conversation
Per-call cost of every factory method that returns a DSL class (PublicKey.fromBytes, Signature.aggregate, SecretKey.sign, etc.) was: 1 napi_external alloc (V8 young-gen) 1 napi_new_instance -> constructor allocs placeholder native + napi_wrap 1 removeWrapChecked -> tears down placeholder finalizer entry 1 destroyInternalPlaceholder 1 native alloc for the real object 1 napi_wrap + typeTagObject That's 2 native allocs + 1 free, 2 napi_wraps + 1 unwrap, 2 typeTagObject writes, and an extra V8 young-gen object per call. In lodestar's attestation/signature hot path this measurably increases Scavenge time. Replace with a threadlocal marker: materializeClassInstance sets `materialize_target` before calling napi_new_instance(argc=0, args=null). The generated constructor early-returns when isMaterializing(T) is true, skipping the placeholder alloc/wrap entirely. materialize then wraps the real object directly. After patch: 1 native alloc + 1 napi_wrap + 1 typeTagObject per call. Bench (lodestar-z bench/napi/materialize.bench.mjs, 200k iters): PublicKey.fromBytes 3.28% -> 2.10% Scavenge (-1.18 pp, -69% count) Signature.fromBytes 0.78% -> 0.47% Scavenge (-38% count)
Consume the marker only in the intended constructor so nested normal construction cannot skip native wrapping. Clean up returned native resources when materialization fails.
ea4b430 to
ba7c8c7
Compare
nazarhussain
approved these changes
May 28, 2026
Contributor
Author
Analysis from claude, to be precise ;) |
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.
Summary
Optimize DSL class-return materialization by removing the internal placeholder instance path.
Previously, returning a DSL class from a function/method created a JS instance with an internal external-marker argument, allocated and wrapped a temporary placeholder native object, removed that wrap, destroyed the placeholder, then wrapped the real native object.
This PR changes materialization to call the class constructor directly in an internal materialization mode, skip normal
init, and wrap the real native object afternapi_new_instancereturns.Details
mainAnalysis from @spiral-ladder
Per-call cost of every factory method that returns a DSL class (PublicKey.fromBytes, Signature.aggregate, SecretKey.sign, etc.) was:
1 napi_external alloc (V8 young-gen)
1 napi_new_instance -> constructor allocs placeholder native + napi_wrap
1 removeWrapChecked -> tears down placeholder finalizer entry
1 destroyInternalPlaceholder
1 native alloc for the real object
1 napi_wrap + typeTagObject
That's 2 native allocs + 1 free, 2 napi_wraps + 1 unwrap, 2 typeTagObject writes, and an extra V8 young-gen object per call. In lodestar's attestation/signature hot path this measurably increases Scavenge time.
Replace with a threadlocal marker: materializeClassInstance sets
materialize_targetbefore calling napi_new_instance(argc=0, args=null). The generated constructor early-returns when isMaterializing(T) is true, skipping the placeholder alloc/wrap entirely. materialize then wraps the real object directly.After patch: 1 native alloc + 1 napi_wrap + 1 typeTagObject per call.
Bench (lodestar-z bench/napi/materialize.bench.mjs, 200k iters):
PublicKey.fromBytes 3.28% -> 2.10% Scavenge (-1.18 pp, -69% count)
Signature.fromBytes 0.78% -> 0.47% Scavenge (-38% count)