[do not merge] perf(dsl): skip placeholder pattern in materializeClassInstance#37
Draft
spiral-ladder wants to merge 1 commit into
Draft
[do not merge] perf(dsl): skip placeholder pattern in materializeClassInstance#37spiral-ladder wants to merge 1 commit into
spiral-ladder wants to merge 1 commit into
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)
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.
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)