Skip to content

Commit d7e1d69

Browse files
committed
Avoid additional clone in objtype::subinstance.
1 parent 4d779bb commit d7e1d69

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

vm/src/obj/objtype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn _mro(cls: PyObjectRef) -> Option<Vec<PyObjectRef>> {
8181
/// Determines if `obj` actually an instance of `cls`, this doesn't call __instancecheck__, so only
8282
/// use this if `cls` is known to have not overridden the base __instancecheck__ magic method.
8383
pub fn isinstance(obj: &PyObjectRef, cls: &PyObjectRef) -> bool {
84-
issubclass(&obj.typ(), &cls)
84+
issubclass(obj.type_ref(), &cls)
8585
}
8686

8787
fn type_instance_check(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

vm/src/pyobject.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -761,19 +761,22 @@ pub trait FromPyObjectRef {
761761
}
762762

763763
pub trait TypeProtocol {
764-
fn typ(&self) -> PyObjectRef;
764+
fn typ(&self) -> PyObjectRef {
765+
self.type_ref().clone()
766+
}
767+
fn type_ref(&self) -> &PyObjectRef;
765768
}
766769

767770
impl TypeProtocol for PyObjectRef {
768-
fn typ(&self) -> PyObjectRef {
769-
(**self).typ()
771+
fn type_ref(&self) -> &PyObjectRef {
772+
(**self).type_ref()
770773
}
771774
}
772775

773776
impl TypeProtocol for PyObject {
774-
fn typ(&self) -> PyObjectRef {
777+
fn type_ref(&self) -> &PyObjectRef {
775778
match self.typ {
776-
Some(ref typ) => typ.clone(),
779+
Some(ref typ) => &typ,
777780
None => panic!("Object {:?} doesn't have a type!", self),
778781
}
779782
}

0 commit comments

Comments
 (0)