Skip to content

Commit 48d8031

Browse files
authored
lookup slot in hash() (RustPython#6102)
* avoid get_class_attr for __hash__; read hash slot via mro_find_map Reduce calls and lock acquisitions on hot paths by bypassing get_class_attr(__hash__) and directly resolving the hash implementation with mro_find_map(|cls| cls.slots.hash.load()). * fix linting in hash function
1 parent 0c8ae3a commit 48d8031

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

vm/src/protocol/object.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -642,20 +642,14 @@ impl PyObject {
642642
}
643643

644644
pub fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
645-
let hash = self.get_class_attr(identifier!(vm, __hash__)).unwrap();
646-
if vm.is_none(&hash) {
647-
return Err(vm.new_exception_msg(
648-
vm.ctx.exceptions.type_error.to_owned(),
649-
format!("unhashable type: '{}'", self.class().name()),
650-
));
645+
if let Some(hash) = self.class().mro_find_map(|cls| cls.slots.hash.load()) {
646+
return hash(self, vm);
651647
}
652648

653-
let hash = self
654-
.class()
655-
.mro_find_map(|cls| cls.slots.hash.load())
656-
.unwrap();
657-
658-
hash(self, vm)
649+
Err(vm.new_exception_msg(
650+
vm.ctx.exceptions.type_error.to_owned(),
651+
format!("unhashable type: '{}'", self.class().name()),
652+
))
659653
}
660654

661655
// type protocol

0 commit comments

Comments
 (0)