Skip to content
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Align type cache seqlock writer protocol with CPython
  • Loading branch information
youknowone committed Mar 5, 2026
commit de010994143681b51d739e7dacad70c352ff2490
4 changes: 4 additions & 0 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@ impl CodeUnits {
///
/// # Safety
/// - `index` must be in bounds.
/// - `value` must be `0` or a valid `*const PyObject` encoded as `usize`.
/// - Callers must follow the cache invalidation/upgrade protocol:
/// invalidate the version guard before writing and publish the new
/// version after writing.
pub unsafe fn write_cache_ptr(&self, index: usize, value: usize) {
self.pointer_cache[index].store(value, Ordering::Relaxed);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
Expand Down
23 changes: 22 additions & 1 deletion crates/vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,28 @@ impl TypeCacheEntry {

#[inline]
fn begin_write(&self) {
self.sequence.fetch_add(1, Ordering::AcqRel);
let mut seq = self.sequence.load(Ordering::Acquire);
loop {
while (seq & 1) != 0 {
core::hint::spin_loop();
seq = self.sequence.load(Ordering::Acquire);
}
match self.sequence.compare_exchange_weak(
seq,
seq.wrapping_add(1),
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => {
core::sync::atomic::fence(Ordering::Release);
break;
}
Err(observed) => {
core::hint::spin_loop();
seq = observed;
}
}
}
}

#[inline]
Expand Down
Loading