Skip to content

Commit 4f04de4

Browse files
committed
Address clippy and review feedback for keys-version stamps
- Use BuildHasher::hash_one for shape hashing (clippy manual_hash_one) - Fix set_item_with_hint doc: a refreshed hint is returned on any hint miss, not only when the hinted slot was vacant - Route both holey-dict instances through a single LOAD_ATTR cache site in the snippet test Assisted-by: Claude
1 parent 33b55c7 commit 4f04de4

3 files changed

Lines changed: 12 additions & 12 deletions

File tree

‎crates/vm/src/builtins/dict.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,8 @@ impl Py<PyDict> {
775775

776776
/// Store using a cached entry index hint for the value-replace fast path.
777777
///
778-
/// Returns a refreshed hint for the key when the hinted slot was vacant
779-
/// (`None` otherwise).
778+
/// On a hint miss, returns a refreshed hint for the key (`None` when the
779+
/// hint hit or no hint is representable).
780780
pub(crate) fn set_item_with_hint<K: DictKey + ?Sized>(
781781
&self,
782782
key: &K,

‎crates/vm/src/dict_inner.rs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,13 @@ static SHAPE_TABLE: std::sync::LazyLock<Box<[core::sync::atomic::AtomicPtr<Shape
9393
});
9494

9595
fn shape_stamp(shape: &[usize]) -> Option<u32> {
96+
use core::hash::BuildHasher;
9697
use core::sync::atomic::AtomicPtr;
9798
// The hasher seed must be process-stable so equal shapes always probe
9899
// the same slots.
99100
static SHAPE_HASHER: std::sync::LazyLock<rapidhash::quality::RandomState> =
100101
std::sync::LazyLock::new(Default::default);
101-
let hash = {
102-
use core::hash::{BuildHasher, Hash, Hasher};
103-
let mut hasher = SHAPE_HASHER.build_hasher();
104-
shape.hash(&mut hasher);
105-
hasher.finish() as usize
106-
};
102+
let hash = SHAPE_HASHER.hash_one(shape) as usize;
107103
let mut candidate: *mut ShapeData = core::ptr::null_mut();
108104
let mut result = None;
109105
for probe in 0..SHAPE_MAX_PROBE {

‎extra_tests/snippets/vm_specialization.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,22 @@ class Holey:
192192
def m(self):
193193
return "g"
194194

195+
def call_m(obj):
196+
# single LOAD_ATTR cache site shared by both instances
197+
return obj.m()
198+
195199
g1, g2 = Holey(), Holey()
196200
for o in (g1, g2):
197201
o.x = 1
198202
o.y = 2
199203
o.z = 3
200204
del o.y # leaves a hole in the entries
201205
for _ in range(300):
202-
assert g1.m() == "g"
203-
assert g2.m() == "g"
206+
assert call_m(g1) == "g"
207+
assert call_m(g2) == "g"
204208
g2.m = lambda: "g2"
205-
assert g1.m() == "g"
206-
assert g2.m() == "g2"
209+
assert call_m(g1) == "g"
210+
assert call_m(g2) == "g2"
207211

208212

209213
holey_dict_falls_back()

0 commit comments

Comments
 (0)