Skip to content

Commit b5b0738

Browse files
authored
Merge pull request RustPython#3699 from youknowone/slot-del-cold
call_slot_del is cold
2 parents 543360d + e76d79e commit b5b0738

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

vm/src/object/core.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -734,26 +734,37 @@ impl PyObject {
734734

735735
#[inline(always)] // the outer function is never inlined
736736
fn drop_slow_inner(&self) -> Result<(), ()> {
737-
// CPython-compatible drop implementation
738-
if let Some(slot_del) = self.class().mro_find_map(|cls| cls.slots.del.load()) {
739-
let ret = crate::vm::thread::with_vm(self, |vm| {
740-
self.0.ref_count.inc();
741-
if let Err(e) = slot_del(self, vm) {
742-
let del_method = self.get_class_attr("__del__").unwrap();
737+
// __del__ is mostly not implemented
738+
#[inline(never)]
739+
#[cold]
740+
fn call_slot_del(
741+
zelf: &PyObject,
742+
slot_del: fn(&PyObject, &VirtualMachine) -> PyResult<()>,
743+
) -> Result<(), ()> {
744+
let ret = crate::vm::thread::with_vm(zelf, |vm| {
745+
zelf.0.ref_count.inc();
746+
if let Err(e) = slot_del(zelf, vm) {
747+
let del_method = zelf.get_class_attr("__del__").unwrap();
743748
vm.run_unraisable(e, None, del_method);
744749
}
745-
self.0.ref_count.dec()
750+
zelf.0.ref_count.dec()
746751
});
747752
match ret {
748753
// the decref right above set ref_count back to 0
749-
Some(true) => {}
754+
Some(true) => Ok(()),
750755
// we've been resurrected by __del__
751-
Some(false) => return Err(()),
756+
Some(false) => Err(()),
752757
None => {
753-
warn!("couldn't run __del__ method for object")
758+
warn!("couldn't run __del__ method for object");
759+
Ok(())
754760
}
755761
}
756762
}
763+
764+
// CPython-compatible drop implementation
765+
if let Some(slot_del) = self.class().mro_find_map(|cls| cls.slots.del.load()) {
766+
call_slot_del(self, slot_del)?;
767+
}
757768
if let Some(wrl) = self.weak_ref_list() {
758769
wrl.clear();
759770
}
@@ -763,7 +774,6 @@ impl PyObject {
763774

764775
/// Can only be called when ref_count has dropped to zero. `ptr` must be valid
765776
#[inline(never)]
766-
#[cold]
767777
unsafe fn drop_slow(ptr: NonNull<PyObject>) {
768778
if let Err(()) = ptr.as_ref().drop_slow_inner() {
769779
// abort drop for whatever reason

0 commit comments

Comments
 (0)