Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5c341d2
vm: add per-interpreter runtime state and interpreter registry
youknowone Aug 13, 2026
43f4b23
gc: stop every interpreter while collecting
youknowone Aug 13, 2026
ffd3d1e
vm: scope the interpreter registry per thread without threading
youknowone Aug 13, 2026
0079994
vm: keep the interpreter registry usable across bootstrap, drop and fork
youknowone Aug 13, 2026
8f108ff
vm: attach and detach thread slots when switching interpreters
youknowone Aug 13, 2026
696a8c1
vm: list only the current interpreter's subclasses
youknowone Aug 14, 2026
562b657
gc: give each interpreter its own collector state
youknowone Aug 14, 2026
30e2020
_queue, _thread: detach before taking locks held across waits
youknowone Aug 14, 2026
5ce062e
_io, _winapi: detach on the remaining stopped-holdable lock takes
youknowone Aug 14, 2026
87b6c68
gc: size the interpreter owner tag to the header padding
youknowone Aug 15, 2026
fd27902
vm: reuse cleared frame blocks and shorten interpreter hot paths
youknowone Aug 15, 2026
c3fcc70
gc, vm: address review notes on owner tags and interpreter docs
youknowone Aug 15, 2026
57428cd
vm: gate interpreter registration on stop-the-world admission
youknowone Aug 15, 2026
8d07831
gc: keep the tracking counters off the barrier path
youknowone Aug 15, 2026
4e518d3
dict: settle lookups and iteration steps under one read guard
youknowone Aug 15, 2026
eb94daf
vm: build a call's argument vector once
youknowone Aug 15, 2026
f70db35
vm: reach an instance dict without cloning it
youknowone Aug 15, 2026
4a76424
vm: shorten the per-instruction safepoint and the call preamble
youknowone Aug 15, 2026
b0f0dcc
vm: give KwArgs a zero-sized hasher
youknowone Aug 15, 2026
f9bbc6d
vm: promote borrowed stack refs before a yield
youknowone Aug 15, 2026
b4bb43c
vm: run the exact-args vectorcall on a heap frame again
youknowone Aug 15, 2026
98d2410
vm: release a tail call's function at the callee's return
youknowone Aug 16, 2026
08bd522
vm: stop copying a running frame's locals when it materializes
youknowone Aug 16, 2026
c7c1452
vm: key data stack frame reuse on the exact frame size
youknowone Aug 16, 2026
2d58fc8
vm: store the frame object payload address in the cross-thread slot
youknowone Aug 16, 2026
e24d831
vm: copy a foreign thread's frames instead of linking them
youknowone Aug 16, 2026
5d0d9be
vm: resolve the two rustdoc links this branch added
youknowone Aug 16, 2026
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
28 changes: 16 additions & 12 deletions crates/capi/src/objimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub unsafe extern "C" fn PyObject_GC_Track(op: *mut PyObject) {
with_vm(|_vm| {
let obj = unsafe { &*op };
if !obj.is_gc_tracked() {
unsafe { gc_state::gc_state().track_object(obj.into()) };
unsafe { gc_state::gc_state().track_object(obj.into(), gc_state::current_owner()) };
}
})
}
Expand All @@ -36,29 +36,33 @@ pub unsafe extern "C" fn PyObject_GC_IsFinalized(op: *mut PyObject) -> c_int {

#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Collect() -> isize {
let result = gc_state::gc_state().collect(2);
(result.collected + result.uncollectable) as isize
with_vm(|vm| {
let result = vm.state.gc.collect(2);
(result.collected + result.uncollectable) as isize
})
}

#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Enable() -> c_int {
let gc = gc_state::gc_state();
let was_enabled = gc.is_enabled();
gc.enable();
was_enabled.into()
with_vm(|vm| {
let was_enabled: c_int = vm.state.gc.is_enabled().into();
vm.state.gc.enable();
was_enabled
})
}

#[unsafe(no_mangle)]
pub extern "C" fn PyGC_Disable() -> c_int {
let gc = gc_state::gc_state();
let was_enabled = gc.is_enabled();
gc.disable();
was_enabled.into()
with_vm(|vm| {
let was_enabled: c_int = vm.state.gc.is_enabled().into();
vm.state.gc.disable();
was_enabled
})
}

#[unsafe(no_mangle)]
pub extern "C" fn PyGC_IsEnabled() -> c_int {
gc_state::gc_state().is_enabled().into()
with_vm(|vm| -> c_int { vm.state.gc.is_enabled().into() })
}

#[unsafe(no_mangle)]
Expand Down
4 changes: 2 additions & 2 deletions crates/capi/src/pystate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ mod tests {
current_vm_is_set(),
"This thread did not have a vm attached"
);
vm.state.stop_the_world.stop_the_world(vm);
vm.state.stop_the_world.start_the_world(vm);
vm.state.stop_the_world.stop_the_world(&vm.state);
vm.state.stop_the_world.start_the_world(&vm.state);
});
});
});
Expand Down
33 changes: 24 additions & 9 deletions crates/stdlib/src/_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,20 @@ mod _queue {
}
}

fn release(&self) {
/// Take `mutex`, detaching first so that blocking on it cannot stall a
/// stop-the-world request.
///
/// A waiter holds this mutex across its `allow_threads` wait, so it can
/// still hold it when it is stopped. An attached thread blocking on it
/// would then never reach a safepoint, the stop would never complete,
/// and the holder would never be resumed to release it.
fn lock_count(&self, vm: &VirtualMachine) -> parking_lot::MutexGuard<'_, usize> {
vm.allow_threads(|| self.mutex.lock())
}

fn release(&self, vm: &VirtualMachine) {
{
let mut count = self.mutex.lock();
let mut count = self.lock_count(vm);
*count += 1;
} // lock dropped. now we can notify a waiting thread

Expand All @@ -95,7 +106,7 @@ mod _queue {
// Guard must be dropped before check_signals() below, since a
// signal handler may call back into this same queue.
{
let mut count = self.mutex.lock();
let mut count = self.lock_count(vm);

if *count > 0 {
*count -= 1;
Expand Down Expand Up @@ -151,11 +162,15 @@ mod _queue {
}

impl PySimpleQueue {
fn push(&self, item: PyObjectRef) {
#[cfg_attr(
not(feature = "threading"),
expect(unused_variables, reason = "only the semaphore needs the vm")
)]
fn push(&self, item: PyObjectRef, vm: &VirtualMachine) {
self.buf.lock().push_back(item);

#[cfg(feature = "threading")]
self.sem.release();
self.sem.release(vm);
}

/// Returns a strong reference from the head of the buffer.
Expand Down Expand Up @@ -221,14 +236,14 @@ mod _queue {
}

#[pymethod]
fn put(&self, args: PutArgs) {
fn put(&self, args: PutArgs, vm: &VirtualMachine) {
let PutArgs { item, .. } = args;
self.push(item);
self.push(item, vm);
}

#[pymethod]
fn put_nowait(&self, item: PyObjectRef) {
self.push(item);
fn put_nowait(&self, item: PyObjectRef, vm: &VirtualMachine) {
self.push(item, vm);
}

#[pymethod]
Expand Down
4 changes: 2 additions & 2 deletions crates/stdlib/src/faulthandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ mod decl {
use core::sync::atomic::Ordering;
let current_tid = rustpython_vm::stdlib::_thread::get_ident();
{
vm.state.stop_the_world.stop_the_world(vm);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(vm); }
vm.state.stop_the_world.stop_the_world(&vm.state);
scopeguard::defer! { vm.state.stop_the_world.start_the_world(&vm.state); }
let registry = vm.state.thread_frames.lock();
#[expect(
clippy::iter_over_hash_type,
Expand Down
7 changes: 7 additions & 0 deletions crates/vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ impl<'a> TryFromBorrowedObject<'a> for bool {

impl PyObjectRef {
/// Convert Python bool into Rust bool.
#[inline(always)]
pub fn try_to_bool(self, vm: &VirtualMachine) -> PyResult<bool> {
if self.is(&vm.ctx.true_value) {
return Ok(true);
} else if self.is(&vm.ctx.false_value) {
return Ok(false);
}

self.try_to_bool_slow(vm)
}

#[cold]
#[inline(never)]
fn try_to_bool_slow(self, vm: &VirtualMachine) -> PyResult<bool> {
let slots = &self.class().slots;

// 1. Try nb_bool slot first
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/builtin_func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ fn vectorcall_native_function(
let mut all_args = Vec::with_capacity(args.len() + 1);
all_args.push(self_obj);
all_args.extend(args);
FuncArgs::from_vectorcall(&all_args, nargs + 1, kwnames)
FuncArgs::from_vectorcall_owned(all_args, nargs + 1, kwnames)
} else {
FuncArgs::from_vectorcall(&args, nargs, kwnames)
FuncArgs::from_vectorcall_owned(args, nargs, kwnames)
};

(zelf.value.func)(vm, func_args)
Expand Down
89 changes: 47 additions & 42 deletions crates/vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ impl PyDict {
&self.entries
}

/// Monotonically increasing version for mutation tracking.
pub(crate) fn version(&self) -> u64 {
self.entries.version()
}

/// Returns all keys as a Vec, atomically under a single read lock.
/// Thread-safe: prevents "dictionary changed size during iteration" errors.
pub fn keys_vec(&self) -> Vec<PyObjectRef> {
Expand Down Expand Up @@ -821,18 +816,15 @@ impl Py<PyDict> {
}
}

/// Fast lookup using a cached entry index hint.
pub(crate) fn get_item_opt_hint<K: DictKey + ?Sized>(
/// Read a cached exact-dict entry after validating its key-layout stamp.
#[inline]
pub(crate) fn get_item_by_index_and_keys_version(
&self,
key: &K,
hint: u16,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
if self.exact_dict(vm) {
self.entries.get_hint(vm, key, usize::from(hint))
} else {
self.get_item_opt(key, vm)
}
version: u16,
index: u16,
) -> Option<PyObjectRef> {
self.entries
.get_index_if_keys_version(u32::from(version), usize::from(index))
}

/// Lookup trying a cached entry index hint first.
Expand Down Expand Up @@ -1098,6 +1090,7 @@ macro_rules! dict_view {
$class_name: literal,
$iter_class_name: literal,
$reverse_iter_class_name: literal,
$project_fn: expr,
$result_fn: expr
) => {
#[pyclass(module = false, name = $class_name)]
Expand All @@ -1120,7 +1113,7 @@ macro_rules! dict_view {
}

fn item(vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef) -> PyObjectRef {
$result_fn(vm, key, value)
$result_fn(vm, $project_fn(&key, &value))
}

fn __reversed__(&self) -> Self::ReverseIter {
Expand Down Expand Up @@ -1206,7 +1199,7 @@ macro_rules! dict_view {
while let Some((next_position, key, value)) =
dict.entries.next_entry(position)
{
entries.push(($result_fn)(vm, key, value));
entries.push(($result_fn)(vm, ($project_fn)(&key, &value)));
position = next_position;
}
entries
Expand All @@ -1223,18 +1216,22 @@ macro_rules! dict_view {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let mut internal = zelf.internal.lock();
let next = if let IterStatus::Active(dict) = &internal.status {
if dict.entries.has_changed_size(&zelf.size) {
internal.status = IterStatus::Exhausted;
return Err(
vm.new_runtime_error("dictionary changed size during iteration")
);
}
match dict.entries.next_entry(internal.position) {
Some((position, key, value)) => {
match dict.entries.next_entry_checked(
internal.position,
&zelf.size,
$project_fn,
) {
Err(dict_inner::DictChanged) => {
internal.status = IterStatus::Exhausted;
return Err(
vm.new_runtime_error("dictionary changed size during iteration")
);
}
Ok(Some((position, item))) => {
internal.position = position;
PyIterReturn::Return(($result_fn)(vm, key, value))
PyIterReturn::Return(($result_fn)(vm, item))
}
None => {
Ok(None) => {
internal.status = IterStatus::Exhausted;
PyIterReturn::StopIteration(None)
}
Expand Down Expand Up @@ -1282,7 +1279,7 @@ macro_rules! dict_view {
while let Some((found_index, key, value)) =
dict.entries.prev_entry(position)
{
entries.push(($result_fn)(vm, key, value));
entries.push(($result_fn)(vm, ($project_fn)(&key, &value)));
if found_index == 0 {
break;
}
Expand All @@ -1309,22 +1306,26 @@ macro_rules! dict_view {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
let mut internal = zelf.internal.lock();
let next = if let IterStatus::Active(dict) = &internal.status {
if dict.entries.has_changed_size(&zelf.size) {
internal.status = IterStatus::Exhausted;
return Err(
vm.new_runtime_error("dictionary changed size during iteration")
);
}
match dict.entries.prev_entry(internal.position) {
Some((found_index, key, value)) => {
match dict.entries.prev_entry_checked(
internal.position,
&zelf.size,
$project_fn,
) {
Err(dict_inner::DictChanged) => {
internal.status = IterStatus::Exhausted;
return Err(
vm.new_runtime_error("dictionary changed size during iteration")
);
}
Ok(Some((found_index, item))) => {
if found_index == 0 {
internal.status = IterStatus::Exhausted;
} else {
internal.position = found_index - 1;
}
PyIterReturn::Return(($result_fn)(vm, key, value))
PyIterReturn::Return(($result_fn)(vm, item))
}
None => {
Ok(None) => {
internal.status = IterStatus::Exhausted;
PyIterReturn::StopIteration(None)
}
Expand All @@ -1348,7 +1349,8 @@ dict_view! {
"dict_keys",
"dict_keyiterator",
"dict_reversekeyiterator",
|_vm: &VirtualMachine, key: PyObjectRef, _value: PyObjectRef| key
|key: &PyObjectRef, _value: &PyObjectRef| key.clone(),
|_vm: &VirtualMachine, key: PyObjectRef| key
}

dict_view! {
Expand All @@ -1361,7 +1363,8 @@ dict_view! {
"dict_values",
"dict_valueiterator",
"dict_reversevalueiterator",
|_vm: &VirtualMachine, _key: PyObjectRef, value: PyObjectRef| value
|_key: &PyObjectRef, value: &PyObjectRef| value.clone(),
|_vm: &VirtualMachine, value: PyObjectRef| value
}

dict_view! {
Expand All @@ -1374,7 +1377,9 @@ dict_view! {
"dict_items",
"dict_itemiterator",
"dict_reverseitemiterator",
|vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef|
|key: &PyObjectRef, value: &PyObjectRef| (key.clone(), value.clone()),
// Builds a tuple, so it runs after the dict's read guard is released.
|vm: &VirtualMachine, (key, value): (PyObjectRef, PyObjectRef)|
vm.new_tuple((key, value)).into()
}

Expand Down
Loading
Loading