Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion Jint/Runtime/Interpreter/Statements/JintBlockStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Completion ExecuteBlock(EvaluationContext context)
{
// Reuse environment: update outer reference and reset slots
cachedEnv._outerEnv = oldEnv;
blockState.SlotTemplates.AsSpan().CopyTo(cachedEnv._slots);
ResetSlots(cachedEnv._slots!, blockState.SlotTemplates!);
blockEnv = cachedEnv;
}
else
Expand Down Expand Up @@ -244,6 +244,28 @@ protected override Completion ExecuteInternal(EvaluationContext context)
return ExecuteBlock(context);
}

/// <summary>
/// Reset the slots of a reused block environment to the pre-computed templates.
/// Hand-rolled small-array fast path: typical block scopes hold 1-3 bindings, where the
/// JIT can unroll this loop and avoid the Span construction + generic copy of CopyTo.
/// </summary>
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.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);
}
}

/// <summary>
/// Pre-computed block scope state, cached on AST node UserData.
/// </summary>
Expand Down
Loading