Skip to content

Commit 22a6661

Browse files
hellovaiclaude
andcommitted
perf(vm): enter bytecode closures from CALL_INDIRECT without leaving compact dispatch
A call through a function value always took the general path: a callable resolve, layout remap, the general frame push, and a return to `exec_compact` to re-derive the frame's function, code and pc. A bytecode function or closure whose parameter slots are exactly what the site pushed now enters the way `CallExactArgs` does (closure type arguments seeded into the frame, same telemetry entry as the general path) and stays in `run_compact`. Hooked and pass-through callees, pending trace or type lanes, remapped layouts, bound methods, host closures and generic function values keep the existing path. The entry is out of line: inlined into `run_compact` it made every ordinary exact call ~10 instructions slower. Instructions per iteration (20M loop minus empty run): direct capturing closure call 1460 -> 1093, `apply(double, i)` 2329 -> 1958, `leaf(i)` 860 -> 858. speedtest vm-off: closure-apply-1m 230.0 -> 193.7 ns/iter, pure-call-1m 71.5 -> 71.3. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6d12ef4 commit 22a6661

2 files changed

Lines changed: 204 additions & 0 deletions

File tree

‎baml_language/crates/bex_vm/src/vm.rs‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7486,6 +7486,144 @@ impl BexVm {
74867486
})
74877487
}
74887488

7489+
/// Enter a bytecode function or closure called through `CallIndirect` the
7490+
/// way `CallExactArgs` enters its callee, when the site pushed exactly the
7491+
/// callee's slots. Returns the entered function; the caller then switches
7492+
/// dispatch to it. `None` leaves everything untouched for the general
7493+
/// path, which handles compiled, hooked and pass-through callees, a
7494+
/// pending trace or type lane, layouts that need remapping, and every
7495+
/// other callable.
7496+
///
7497+
/// Kept out of line: inlined into `run_compact`, it made every ordinary
7498+
/// exact call about ten instructions slower.
7499+
#[inline(never)]
7500+
fn enter_indirect_bytecode_call(
7501+
&mut self,
7502+
callee_ptr: HeapPtr,
7503+
frame_idx: usize,
7504+
caller: &'static Function,
7505+
) -> Result<Option<&'static Function>, VmError> {
7506+
if self.pending_call_trace.is_some() || !self.pending_call_type_args.is_empty() {
7507+
return Ok(None);
7508+
}
7509+
// SAFETY: the active heap permit prevents collection during
7510+
// frame/stack growth, as at `CallExactArgs`; a closure's function
7511+
// lives at least as long as the closure.
7512+
let (callee, callee_fn_ptr, captured_type_args) = match unsafe { callee_ptr.get() } {
7513+
Object::Function(callee) => (callee, callee_ptr, &[][..]),
7514+
Object::Closure(closure) => match unsafe { closure.function.get() } {
7515+
Object::Function(callee) => {
7516+
(callee, closure.function, &closure.captured_type_args[..])
7517+
}
7518+
_ => return Ok(None),
7519+
},
7520+
_ => return Ok(None),
7521+
};
7522+
if !matches!(callee.kind, FunctionKind::Bytecode)
7523+
|| callee.compiled_body.is_some()
7524+
|| callee.trace_passthrough
7525+
|| callee
7526+
.bytecode
7527+
.compact
7528+
.as_ref()
7529+
.is_none_or(|code| code.trace_hook_finish_pc.is_some())
7530+
|| Self::recorded_call_layout(caller, self.cur_pc)
7531+
.is_some_and(|layout| !callee.argument_layout_is(layout))
7532+
{
7533+
return Ok(None);
7534+
}
7535+
7536+
let _popped_callee = self.stack.ensure_pop();
7537+
// Eager ok_or constructs/drops VmInternalError even on success.
7538+
#[expect(clippy::unnecessary_lazy_evaluations)]
7539+
let locals_offset = StackIndex::from_raw(
7540+
self.stack
7541+
.len()
7542+
.checked_sub(callee.arity)
7543+
.ok_or_else(|| VmInternalError::NotEnoughItemsOnStack(callee.arity))?,
7544+
);
7545+
let Frame::Bytecode(caller_frame) = &mut self.frames[frame_idx] else {
7546+
verifier_unreachable!()
7547+
};
7548+
caller_frame.faulting_pc = self.cur_pc;
7549+
self.reserve_call_frame()?;
7550+
self.reserve_stack(callee.real_local_count)?;
7551+
let context = self.current_context().clone();
7552+
if let Some(telemetry) = &mut self.telemetry {
7553+
telemetry.set_context(context.clone());
7554+
}
7555+
let frame_telemetry = if self.telemetry.is_some() {
7556+
self.enter_indirect_bytecode_telemetry(
7557+
frame_idx,
7558+
callee,
7559+
callee_fn_ptr,
7560+
locals_offset,
7561+
captured_type_args,
7562+
)
7563+
} else {
7564+
None
7565+
};
7566+
// Written in place, as in the general path.
7567+
self.frames.push_reserved_with(|| {
7568+
Frame::Bytecode(BytecodeFrame {
7569+
function: callee_ptr,
7570+
instruction_ptr: 0,
7571+
locals_offset,
7572+
// A closure made in a generic context reads the type
7573+
// arguments it captured.
7574+
type_args: captured_type_args.to_vec(),
7575+
faulting_pc: 0,
7576+
telemetry: frame_telemetry,
7577+
context,
7578+
})
7579+
});
7580+
if callee.real_local_count != 0 {
7581+
let new_len = self.stack.len() + callee.real_local_count;
7582+
self.resize_stack(new_len, Value::NULL)?;
7583+
}
7584+
Ok(Some(callee))
7585+
}
7586+
7587+
/// Telemetry entry for a bytecode callee `CallIndirect` enters directly:
7588+
/// what `execute_call_from_locals_offset` records for an untraced call
7589+
/// that passes no type arguments.
7590+
#[inline(never)]
7591+
fn enter_indirect_bytecode_telemetry(
7592+
&mut self,
7593+
frame_idx: usize,
7594+
callee: &Function,
7595+
callee_fn_ptr: HeapPtr,
7596+
locals_offset: StackIndex,
7597+
carried: &[bex_vm_types::RealizedTy],
7598+
) -> Option<FrameTelemetry> {
7599+
// As the general path records it, with this instruction as the call site.
7600+
let caller = self.bytecode_caller(frame_idx);
7601+
let actual_caller = self
7602+
.visible_bytecode_caller(frame_idx)
7603+
.and_then(|at| self.frame_identity(at));
7604+
let caller_is_observed = self.frame_receipt(caller).is_some();
7605+
let caller_pc = self.caller_pc(caller, frame_idx, self.cur_pc);
7606+
let args = &self.stack.as_slice()
7607+
[locals_offset.raw()..locals_offset.raw().saturating_add(callee.arity)];
7608+
// SAFETY: arguments and callable remain live under the heap permit.
7609+
self.telemetry.as_mut().and_then(|telemetry| unsafe {
7610+
telemetry.enter_bytecode_with_trace(
7611+
callee,
7612+
callee_fn_ptr,
7613+
actual_caller,
7614+
caller_pc,
7615+
caller_is_observed,
7616+
args,
7617+
None,
7618+
CallTypeArgs {
7619+
carried,
7620+
passed: &[],
7621+
},
7622+
|caller, callee| Self::register_call_path_functions(&self.heap, caller, callee),
7623+
)
7624+
})
7625+
}
7626+
74897627
fn execute_call_from_locals_offset(
74907628
&mut self,
74917629
callee_ptr: HeapPtr,
@@ -10575,6 +10713,22 @@ impl BexVm {
1057510713
let callee_value = self.stack[callee_slot];
1057610714
let callee_ptr =
1057710715
self.as_object_ptr(callee_value, FunctionType::Callable.into())?;
10716+
// Bytecode functions and closures stay in compact
10717+
// dispatch, as at `CallExactArgs`.
10718+
if let Some(callee) =
10719+
self.enter_indirect_bytecode_call(callee_ptr, *frame_idx, function)?
10720+
{
10721+
*frame_idx = self.frames.len() - 1;
10722+
*function = callee;
10723+
runtime_package = self.runtime_package_of(function);
10724+
if self.should_early_yield() {
10725+
return Ok(Some(VmExecState::EarlyYield));
10726+
}
10727+
*pc = 0;
10728+
*code = &callee.bytecode.compact.as_ref().unwrap().code;
10729+
*dispatch_frame_idx = *frame_idx;
10730+
continue;
10731+
}
1057810732
let obj = self.get_object(callee_ptr);
1057910733

1058010734
if let Object::HostClosure(host_closure) = obj {

‎baml_language/crates/bex_vm/tests/call_exact_args.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,53 @@ fn exact_transitions_preserve_resume_pc_across_callbacks_and_reused_depths() {
254254
assert!(yields > 0, "interval {interval} did not exercise a handoff");
255255
}
256256
}
257+
258+
#[test]
259+
fn indirect_calls_enter_closures_functions_and_bound_methods() {
260+
// A capturing closure called directly, a plain function and a capturing
261+
// closure called through a parameter, a generic closure whose body needs
262+
// its captured `T`, and a generic class's bound method, whose receiver
263+
// type arguments must still reach the frame.
264+
let source = r#"
265+
class Box<T> {
266+
value T
267+
function parse(self, s: string) -> T { baml.json.decode<T>(s) }
268+
}
269+
function leaf(n: int) -> int { n }
270+
function invoke(f: (int) -> int, n: int) -> int { f(n) }
271+
function parse_later<T>(s: string) -> () -> T throws baml.json.DecodeError | baml.json.ParseError {
272+
() -> T { baml.json.decode<T>(s) }
273+
}
274+
function main() -> int {
275+
let k = 3;
276+
let add = (x: int) -> int { x + k };
277+
let later = parse_later<int>("10");
278+
let b = Box<int> { value: 0 };
279+
let parse = b.parse;
280+
add(1) + invoke(leaf, 5) + invoke(add, 2) + later() + parse("18")
281+
}
282+
"#;
283+
let program = convert_program(compile_source(source)).unwrap();
284+
assert!(
285+
ops(function(&program, "main"))
286+
.iter()
287+
.any(|(_, op)| *op == OpCode::CallIndirect)
288+
);
289+
assert_eq!(run(source), 42);
290+
// Hand off at varying points, including right after a closure frame is
291+
// entered, and resume inside it.
292+
for interval in [1, 2, 3, 5, 11] {
293+
let program = compile_source(source);
294+
let entry = program.rendered_callables()["user.main"].object.raw();
295+
let flag = Arc::new(AtomicBool::new(true));
296+
let mut vm = BexVm::from_program(program, Arc::clone(&flag)).unwrap();
297+
vm.early_yield = bex_vm_types::EarlyYieldCheck::with_interval(flag, interval);
298+
vm.set_entry_point(vm.heap.compile_time_ptr(entry), &[]);
299+
let result = (0..500).find_map(|_| match vm.exec().unwrap() {
300+
VmExecState::EarlyYield => None,
301+
VmExecState::Complete(value) => Some(value.as_int()),
302+
other => panic!("unexpected state at interval {interval}: {other:?}"),
303+
});
304+
assert_eq!(result, Some(Some(42)), "interval {interval}");
305+
}
306+
}

0 commit comments

Comments
 (0)