镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit 9edcb64

Browse files
committed
perf(vm): inline bounded ordinary calls (#48)
1 parent b343ad0 commit 9edcb64

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎src/interpreter.zig‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,14 @@ pub fn functionRealmGlobal(env: *Environment, fallback: ?*value.Object) ?*value.
749749
return fallback;
750750
}
751751

752+
pub fn vmChunkAllowsInlineCalls(chunk: *const bc.Chunk) bool {
753+
for (chunk.code.items) |inst| switch (inst.op) {
754+
.tail_call, .tail_call_eval, .tail_call_method, .tail_call_with_this => return false,
755+
else => {},
756+
};
757+
return true;
758+
}
759+
752760
const TreeTailCall = struct {
753761
callee: Value,
754762
args: []const Value,
@@ -783,6 +791,11 @@ pub const Function = struct {
783791
/// compiler does not lower yet, in which case the function is invoked via the
784792
/// tree-walker (`callFunction`). Generators/async use `gen_chunk`/`async_chunk`.
785793
chunk: ?*bc.Chunk = null,
794+
/// True when a plain VM chunk can execute through the bounded native-stack
795+
/// call fast path. Chunks containing a tail-call opcode must stay entirely
796+
/// under the heap activation driver because those opcodes yield a pending
797+
/// activation directly to that driver.
798+
vm_inline_calls_safe: bool = false,
786799
/// VM closure capture: the defining function's frame (type-erased `*vm.Frame`),
787800
/// for resolving upvalues. Null at the top level. The slot count to allocate
788801
/// for this function's own frame.
@@ -1265,6 +1278,12 @@ pub const Interpreter = struct {
12651278
/// It counts backing activation allocations, not calls; a bounded value for
12661279
/// repeated shallow recursion proves that the free list is doing real work.
12671280
vm_activation_allocations: usize = 0,
1281+
/// Bounded native-stack VM call depth. Ordinary shallow calls avoid a
1282+
/// driver round-trip; once the bound is reached, a nested heap activation
1283+
/// driver handles the remainder of the chain. Both fields are interpreter-
1284+
/// local, including in shared-realm worker lanes.
1285+
vm_inline_call_depth: u8 = 0,
1286+
vm_inline_calls_disabled: bool = false,
12681287

12691288
/// Guard a function call against native-stack exhaustion: throw a catchable
12701289
/// `RangeError` when either the logical call-depth limit or the real OS
@@ -4192,6 +4211,7 @@ pub const Interpreter = struct {
41924211
if (compiled) |code| {
41934212
func.chunk = code.chunk;
41944213
func.local_count = code.local_count;
4214+
func.vm_inline_calls_safe = vmChunkAllowsInlineCalls(code.chunk);
41954215
}
41964216
}
41974217
// A function object's [[Prototype]] is the kind-specific function

‎src/vm.zig‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const EvalError = interp.EvalError;
3434

3535
const async_gen_request_reserve_granularity: usize = 16;
3636
const native_tier_entry_threshold: u32 = 3;
37+
const inline_call_depth_limit: u8 = 32;
3738

3839
fn bindThisForCall(vm: *Interpreter, func: *Function, this_val: Value) EvalError!Value {
3940
if (func.is_arrow) return func.arrow_this;
@@ -2335,6 +2336,15 @@ fn runChunk(vm: *Interpreter, exec: *Exec, chunk: *Chunk, frame: ?*Frame, gen: ?
23352336
try stack.append(stack_alloc, result);
23362337
continue;
23372338
}
2339+
if (func.vm_inline_calls_safe and !vm.vm_inline_calls_disabled) {
2340+
const result = if (vm.vm_inline_call_depth < inline_call_depth_limit)
2341+
try runInlineFunction(vm, func, func.chunk.?, stack.items[base..], Value.undef(), Value.undef())
2342+
else
2343+
try callValueWithInlineCallsDisabled(vm, callee, stack.items[base..], Value.undef());
2344+
stack.shrinkRetainingCapacity(base - 1);
2345+
try stack.append(stack_alloc, result);
2346+
continue;
2347+
}
23382348
}
23392349
}
23402350
// Trampoline plain JS→JS calls under the driver: build the callee
@@ -3847,6 +3857,7 @@ fn makeClosure(vm: *Interpreter, tmpl: *bc.FnTemplate, frame: ?*Frame) EvalError
38473857
.import_meta_slot = vm.import_meta_slot,
38483858
.module_referrer = vm.cur_module,
38493859
.chunk = if (tmpl.is_generator or tmpl.is_async) null else tmpl.chunk,
3860+
.vm_inline_calls_safe = if (tmpl.chunk) |compiled| interp.vmChunkAllowsInlineCalls(compiled) else false,
38503861
.gen_chunk = if (tmpl.is_generator) tmpl.chunk else null,
38513862
.frame = frame,
38523863
.local_count = tmpl.local_count,
@@ -4101,6 +4112,32 @@ fn inheritCallerState(dst: *Activation, src: *const Activation) void {
41014112
dst.saved_pm = src.saved_pm;
41024113
}
41034114

4115+
/// Execute a shallow ordinary VM call directly, retaining the same activation,
4116+
/// root registration, handler unwinding, step accounting, and state restoration
4117+
/// as a driver-owned activation. The depth bound keeps native stack use fixed;
4118+
/// the boundary call disables this path while a nested heap driver handles the
4119+
/// remainder of a deep chain.
4120+
fn runInlineFunction(vm: *Interpreter, func: *Function, fchunk: *Chunk, args: []const Value, this_val: Value, new_target: Value) EvalError!Value {
4121+
try vm.stackGuard();
4122+
vm.depth += 1;
4123+
defer vm.depth -= 1;
4124+
const act = try buildActivation(vm, func, fchunk, args, this_val, new_target);
4125+
defer {
4126+
popActivation(vm, act);
4127+
releaseActivation(vm, act);
4128+
}
4129+
vm.vm_inline_call_depth += 1;
4130+
defer vm.vm_inline_call_depth -= 1;
4131+
return execLoop(vm, &act.exec, fchunk, act.frame, null);
4132+
}
4133+
4134+
fn callValueWithInlineCallsDisabled(vm: *Interpreter, callee: Value, args: []const Value, this_val: Value) EvalError!Value {
4135+
const saved = vm.vm_inline_calls_disabled;
4136+
vm.vm_inline_calls_disabled = true;
4137+
defer vm.vm_inline_calls_disabled = saved;
4138+
return callValue(vm, callee, args, this_val);
4139+
}
4140+
41044141
/// If `callee` is a plain JS-chunk function (not generator/async/native/bound/
41054142
/// proxy), return it — those are the calls the trampoline pushes onto its
41064143
/// activation stack. Everything else takes the native call path.
@@ -4502,7 +4539,7 @@ test "vm: recursive calls throw a catchable RangeError before native stack overf
45024539
defer arena.deinit();
45034540
const a = arena.allocator();
45044541
var parser = try Parser.init(a,
4505-
\\function recurse(n) { return recurse(n + 1); }
4542+
\\function recurse(n) { return 1 + recurse(n + 1); }
45064543
\\recurse(0)
45074544
);
45084545
const prog = try parser.parseProgram();

0 commit comments

Comments
 (0)