Skip to content

Cache array-like wrapper factories and materialize length lazily#2730

Merged
lahma merged 1 commit into
sebastienros:mainfrom
lahma:perf/interop-wrapper-fastpath
Jul 22, 2026
Merged

Cache array-like wrapper factories and materialize length lazily#2730
lahma merged 1 commit into
sebastienros:mainfrom
lahma:perf/interop-wrapper-fastpath

Conversation

@lahma

@lahma lahma commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

With ArrayConversion defaulting to LiveView (#2728), the dominant cost of a CLR collection member read from script became wrapper construction itself (context in #2729). Profiling the interop-collection-traversal comparison row shows the conversion path at ~70% of the row, and inside it two avoidable blocks:

  • Activator.CreateInstance(arrayWrapperType, engine, target, type) pays the full reflection binder (DefaultBinder.BindToMethod + RuntimeConstructorInfo.Invoke + argument marshaling) on every wrapper creation — over half the wrapper-build subtree.
  • The ObjectWrapper constructor eagerly allocates the length forwarder (ClrFunction + GetSetPropertyDescriptor + the _properties store) that plain length reads never consult — they are served by the ICollection fast path in Get.

Changes

  • The array-like resolution cache now stores a typed ArrayLikeWrapperFactory (one Activator call per exposed type, ever) and every wrapper creation is a virtual call + direct constructor. The trimming fallback behavior is unchanged: a trimmed constructor caches a null factory and falls back to the non-generic ListWrapper exactly as before.
  • The length forwarder is materialized lazily on first own-property consultation (GetOwnProperty, the Set member gate, or an explicit removal). Descriptor shape, configurability, delete semantics and enumeration are unchanged — covered by a new test plus the existing extracted-getter tests.

Benchmarks (same machine, adjacent same-base A/B, default job)

InteropWrapperChurnBenchmark:

Row main PR delta
ArrayMemberTraversal_DefaultLiveView 60.80 µs / 83,544 B 23.28 µs / 16,344 B −62% / −80%
ArrayMemberTraversal_CopyMode 159.8 µs / 333,944 B 157.3 µs / 333,944 B flat
ArrayMemberTraversal_CopyIdentityFlag 14.8 µs / 344 B 15.0 µs / 344 B flat
SameObjectReturnedInLoop_* 2.5 µs / 2,336 B 2.6 µs / 2,336 B flat

EngineComparisonInteropBenchmark (Jint lane):

Row main PR delta
interop-collection-traversal 6.020 ms / 8,456 KB 2.284 ms / 1,894 KB −62% / −78%
interop-method-calls 2.257 ms 2.192 ms flat
interop-string-passing 1.061 ms 0.996 ms flat
interop-property-access 1.829 ms (launchCount-5, σ=39 µs) 1.821 ms (launchCount-5, σ=22 µs) flat

A single-launch run showed property-access +19% on this branch; a launchCount-5 A/B (both sides) adjudicated it as code-layout bimodality — the five-launch means are statistically identical. The collection-traversal delta was re-confirmed at launchCount-5 (6.044 → 2.302 ms).

Gates: full Jint.Tests (net10.0 + net472), Jint.Tests.PublicInterface, Test262 99,431 passed / 0 failed.

🤖 Generated with Claude Code

Every conversion of a CLR array/list under LiveView built its wrapper through
Activator.CreateInstance(type, args), paying the full reflection binder per
creation, and eagerly allocated the length forwarder (ClrFunction +
GetSetPropertyDescriptor + property store) that plain length reads never
consult (they are served by the ICollection fast path in Get). Resolve a
typed ArrayLikeWrapperFactory once per exposed type and create wrappers
through it, and materialize the length descriptor on first own-property
consultation instead of in the constructor.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant