Skip to content

Commit 2d7c2f7

Browse files
committed
vm: tighten len/isinstance CALL specializations to builtin guards
1 parent d3eb89d commit 2d7c2f7

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

crates/vm/src/frame.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,7 +3507,10 @@ impl ExecutingFrame<'_> {
35073507
let null = self.pop_value_opt();
35083508
let callable = self.pop_value();
35093509
let callable_tag = &*callable as *const PyObject as u32;
3510-
if null.is_none() && cached_tag == callable_tag {
3510+
let is_len_callable = callable
3511+
.downcast_ref::<PyNativeFunction>()
3512+
.is_some_and(|native| native.zelf.is_none() && native.value.name == "len");
3513+
if null.is_none() && cached_tag == callable_tag && is_len_callable {
35113514
let len = obj.length(vm)?;
35123515
self.push_value(vm.ctx.new_int(len).into());
35133516
return Ok(None);
@@ -3532,7 +3535,12 @@ impl ExecutingFrame<'_> {
35323535
if effective_nargs == 2 {
35333536
let callable = self.nth_value(nargs + 1);
35343537
let callable_tag = callable as *const PyObject as u32;
3535-
if cached_tag == callable_tag {
3538+
let is_isinstance_callable = callable
3539+
.downcast_ref::<PyNativeFunction>()
3540+
.is_some_and(|native| {
3541+
native.zelf.is_none() && native.value.name == "isinstance"
3542+
});
3543+
if cached_tag == callable_tag && is_isinstance_callable {
35363544
let nargs_usize = nargs as usize;
35373545
let pos_args: Vec<PyObjectRef> = self.pop_multiple(nargs_usize).collect();
35383546
let self_or_null = self.pop_value_opt();
@@ -6908,9 +6916,16 @@ impl ExecutingFrame<'_> {
69086916
if let Some(native) = callable.downcast_ref::<PyNativeFunction>() {
69096917
let effective_nargs = nargs + u32::from(self_or_null_is_some);
69106918
let callable_tag = callable as *const PyObject as u32;
6911-
let new_op = if native.value.name == "len" && nargs == 1 && effective_nargs == 1 {
6919+
let new_op = if native.zelf.is_none()
6920+
&& native.value.name == "len"
6921+
&& nargs == 1
6922+
&& effective_nargs == 1
6923+
{
69126924
Instruction::CallLen
6913-
} else if native.value.name == "isinstance" && effective_nargs == 2 {
6925+
} else if native.zelf.is_none()
6926+
&& native.value.name == "isinstance"
6927+
&& effective_nargs == 2
6928+
{
69146929
Instruction::CallIsinstance
69156930
} else if effective_nargs == 1 {
69166931
Instruction::CallBuiltinO

0 commit comments

Comments
 (0)