Skip to content
Merged
Changes from 1 commit
Commits
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
Next Next commit
fast-path hash() for exact PyStr
str.__hash__ can’t be overridden, so for exact PyStr instances we
skip the attribute lookup and MRO walk and call the string hash directly.
This reduces overhead on hot paths (e.g. dict/set key hashing) while keeping
subclass semantics intact (downcast_ref_if_exact).
  • Loading branch information
giammirove committed Aug 18, 2025
commit 41a956a914ca92ebb5f8a482cf348efb9dd43f2e
5 changes: 5 additions & 0 deletions vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,11 @@ impl PyObject {
}

pub fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
// __hash__ function in str cannot be overwritten
if let Some(pystr) = self.downcast_ref_if_exact::<PyStr>(vm) {
return Ok(pystr.hash(vm));
}

let hash = self.get_class_attr(identifier!(vm, __hash__)).unwrap();
if vm.is_none(&hash) {
return Err(vm.new_exception_msg(
Expand Down
Loading