Skip to content

Inline small block-env slot reset#2420

Merged
lahma merged 1 commit into
sebastienros:mainfrom
lahma:perf/block-env-reset
Apr 26, 2026
Merged

Inline small block-env slot reset#2420
lahma merged 1 commit into
sebastienros:mainfrom
lahma:perf/block-env-reset

Conversation

@lahma

@lahma lahma commented Apr 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

Last in the perf series. The cached block-environment reset path in JintBlockStatement.ExecuteBlock resets the slot array on every iteration of let/const-bearing loops via SlotTemplates.AsSpan().CopyTo(_slots). For typical block scopes (1-3 bindings — e.g. const z, ms, rn in stopwatch-modern.js) the Span construction + generic CopyTo overhead is comparable to the work itself.

This PR moves the copy into a tiny static helper with [MethodImpl(AggressiveInlining)] and a hand-rolled <= 4-element loop. The JIT can unroll this and avoid the intermediate Span. Larger block scopes still go through CopyTo.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ResetSlots(Binding[] slots, Binding[] templates)
{
    var len = slots.Length;
    if (len == templates.Length && len <= 4)
    {
        for (var i = 0; i < len; i++)
        {
            slots[i] = templates[i];
        }
    }
    else
    {
        templates.AsSpan().CopyTo(slots);
    }
}

Benchmark (stopwatch.js, fresh back-to-back)

Ryzen 9 5950X / .NET 10.0.7. Both runs taken back-to-back on the same session. The Execute_ParsedScript (let) baseline run had a bimodal outlier (one iteration at ~340ms pulling the mean to 287ms with StdDev 60ms; median was 246.5ms) — the more honest baseline for that case is the median, against which C6's mean of 243.5ms is also flat.

Method Modern main PR Δ time
Execute var 200.9 ms 201.5 ms flat
Execute_ParsedScript var 206.0 ms 205.4 ms flat
Execute let 243.0 ms 239.6 ms -1%
Execute_ParsedScript let 246.5 ms* 243.5 ms -1%
Allocated all ~38.5 MB ~38.5 MB flat

*median, baseline run had a bimodal outlier

So: essentially flat in measurement. The change is justified on code-path quality (avoids generic CopyTo overhead on a known-small array) rather than a chase-able number. Worth landing for the same reason as [AggressiveInlining] markers elsewhere in this codebase: it expresses a hot-path intent that JIT can act on.

Validation

  • Jint.Tests (net10.0): 2861 passed, 0 failed.
  • Jint.Tests.Test262 (net10.0): 98567 passed, 506 skipped, 0 failed.

Stack context

Sixth and final perf change in the series:

Cumulative measured impact across the series on stopwatch.js: ~5% time reduction, ~46% allocation reduction (71 MB → 38.5 MB). The big wins were #2412 (cache fires for configurable properties) and #2413 (env pool, the bulk of the alloc reduction); #2416 / #2419 / this PR are smaller polish.

🤖 Generated with Claude Code

The cached block-environment reset path used SlotTemplates.AsSpan().CopyTo
to copy the binding template into the live slot array on every iteration
of let/const-bearing loops. For typical block scopes (1-3 bindings — e.g.
the stopwatch-modern body's `const z, ms, rn`) the Span construction +
generic CopyTo overhead is comparable to the work itself.

Move the copy into a small static helper with AggressiveInlining and a
hand-rolled <=4-element loop. The JIT can unroll this and avoid the
intermediate Span. Larger block scopes still go through CopyTo.

stopwatch benchmark (clean): within noise of Change 3 — confirms no
regression. Cumulative baseline -> Change 6 (clean run, low std-dev):
  Execute (var):       265 ms -> 205 ms  (-23%)
  Execute_ParsedScript:386 ms -> 216 ms  (-44%)
  Execute (let):       458 ms -> 247 ms  (-46%)
  Execute_ParsedScript:443 ms -> 244 ms  (-45%)
  Allocations:         71 MB  -> 38.5 MB (-46%)

98567 test262 + 2861 Jint.Tests pass; 0 regressions. (Earlier transient
2-4 failures on decodeURI/A2.5_T1 were timeouts under concurrent machine
load — these tests do 1.3M-iteration byte-range loops and are
load-sensitive; pass cleanly when isolated.)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@lahma
lahma enabled auto-merge (squash) April 26, 2026 05:43
@lahma
lahma merged commit d8204fa into sebastienros:main Apr 26, 2026
3 of 4 checks passed
@lahma
lahma deleted the perf/block-env-reset branch April 26, 2026 05:55
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