Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
vm: align class-call specialization flow with CPython
  • Loading branch information
youknowone committed Mar 5, 2026
commit 01a4c27014ab5b94200388fbb53b96d9bcec4003
46 changes: 27 additions & 19 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7866,30 +7866,38 @@ impl ExecutingFrame<'_> {
}

// type/str/tuple(x) and class-call specializations
if callable.class().is(vm.ctx.types.type_type)
&& let Some(cls) = callable.downcast_ref::<PyType>()
{
if !self_or_null_is_some && nargs == 1 {
let new_op = if callable.is(&vm.ctx.types.type_type.as_object()) {
Some(Instruction::CallType1)
} else if callable.is(&vm.ctx.types.str_type.as_object()) {
Some(Instruction::CallStr1)
} else if callable.is(&vm.ctx.types.tuple_type.as_object()) {
Some(Instruction::CallTuple1)
} else {
None
};
if let Some(new_op) = new_op {
self.specialize_at(instr_idx, cache_base, new_op);
if let Some(cls) = callable.downcast_ref::<PyType>() {
if cls.slots.flags.has_feature(PyTypeFlags::IMMUTABLETYPE) {
if !self_or_null_is_some && nargs == 1 {
let new_op = if callable.is(&vm.ctx.types.type_type.as_object()) {
Some(Instruction::CallType1)
} else if callable.is(&vm.ctx.types.str_type.as_object()) {
Some(Instruction::CallStr1)
} else if callable.is(&vm.ctx.types.tuple_type.as_object()) {
Some(Instruction::CallTuple1)
} else {
None
};
if let Some(new_op) = new_op {
self.specialize_at(instr_idx, cache_base, new_op);
return;
}
}
if cls.slots.vectorcall.load().is_some() {
self.specialize_at(instr_idx, cache_base, Instruction::CallBuiltinClass);
return;
}
self.specialize_at(instr_idx, cache_base, Instruction::CallNonPyGeneral);
return;
}
if cls.slots.flags.has_feature(PyTypeFlags::IMMUTABLETYPE)
&& cls.slots.vectorcall.load().is_some()
{
self.specialize_at(instr_idx, cache_base, Instruction::CallBuiltinClass);

// CPython only considers CALL_ALLOC_AND_ENTER_INIT for types whose
// metaclass is exactly `type`.
if !callable.class().is(vm.ctx.types.type_type) {
self.specialize_at(instr_idx, cache_base, Instruction::CallNonPyGeneral);
return;
}

// CallAllocAndEnterInit: heap type with default __new__
if !self_or_null_is_some && cls.slots.flags.has_feature(PyTypeFlags::HEAPTYPE) {
let object_new = vm.ctx.types.object_type.slots.new.load();
Expand Down