Visitar URL original
Per-interpreter runtime state and a process-wide GC stop-the-world by youknowone · Pull Request #8517 · RustPython/RustPython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
vm: reach an instance dict without cloning it
The specialized attribute instructions cloned the instance dict — a rwlock
round-trip plus a refcount round-trip — for two things that only look at it:
`LoadAttrMethodLazyDict` asking whether the dict exists, and the keys-version
stamp check that is the whole of `shadowing_instance_attr`'s fast path. Both
now read it borrowed, through `has_instance_dict` / `with_instance_dict`.

`generic_getattr_opt` probed the dict with the name's `&Wtf8`, which hashes
the name on every lookup and can never match a key by pointer. Passing the
`Py<PyStr>` uses the string's cached hash and the interned-key identity
check, and drops an allocation when a stored key is not an exact `str`.

Assisted-by: Claude
  • Loading branch information
youknowone committed Aug 15, 2026
commit f70db351e2b009044024f24ed9b45df9b401f5c1
16 changes: 11 additions & 5 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5271,7 +5271,7 @@ impl ExecutingFrame<'_> {

if type_version != 0
&& owner.class().tp_version_tag.load(Acquire) == type_version
&& owner.dict().is_none()
&& !owner.has_instance_dict()
&& let Some(func) = self.try_read_cached_descriptor(cache_base, type_version)
{
let owner = self.pop_value();
Expand Down Expand Up @@ -8933,13 +8933,19 @@ impl ExecutingFrame<'_> {
attr_name: &'static PyStrInterned,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
let Some(dict) = self.top_value().dict() else {
return Ok(None);
};
let stamp = self.code.instructions.read_cache_ptr(cache_base + 3);
if stamp != 0 && stamp == dict.keys_version() as usize {
// Take the stamp check first, on a borrowed dict: a hit is the whole
// fast path, and cloning the dict for it would cost more than the
// comparison it exists to make.
let stamped = self.top_value().with_instance_dict(|dict| {
dict.is_some_and(|d| stamp != 0 && stamp == d.keys_version() as usize)
});
if stamped {
return Ok(None);
}
let Some(dict) = self.top_value().dict() else {
return Ok(None);
};
// Take the stamp before probing so it attests the probed key set.
let stamp = dict.assign_keys_version(vm);
if let Some(value) = dict.get_item_opt(attr_name, vm)? {
Expand Down
32 changes: 32 additions & 0 deletions crates/vm/src/object/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,16 @@ impl InstanceDict {
self.d.read().clone()
}

/// Run `f` on the dict without cloning it.
///
/// For callers that only need to look at the dict — a predicate, a version
/// stamp — this drops the refcount round-trip [`Self::get`] pays. `f` runs
/// under the read guard, so it must not run Python or take this lock again.
#[inline]
pub(crate) fn with<R>(&self, f: impl FnOnce(Option<&Py<crate::builtins::PyDict>>) -> R) -> R {
f(self.d.read().as_deref())
}

#[inline]
pub(crate) fn set(&self, d: Option<PyDictRef>) {
self.replace(d);
Expand Down Expand Up @@ -1638,6 +1648,28 @@ impl PyObject {
self.instance_dict().and_then(|d| d.get())
}

/// Whether this object currently has an instance dict, without cloning it.
///
/// `false` both for an object with no dict slot and for one whose slot is
/// still empty, which is what `dict().is_none()` reports.
#[inline(always)]
pub fn has_instance_dict(&self) -> bool {
self.instance_dict()
.is_some_and(|d| d.with(|dict| dict.is_some()))
}

/// Run `f` on the instance dict without cloning it; see [`InstanceDict::with`].
#[inline(always)]
pub(crate) fn with_instance_dict<R>(
&self,
f: impl FnOnce(Option<&Py<crate::builtins::PyDict>>) -> R,
) -> R {
match self.instance_dict() {
Some(d) => d.with(f),
None => f(None),
}
}

/// Set the dict field. Returns `Err(dict)` if this object does not have a dict field
/// in the first place.
pub fn set_dict(&self, dict: Option<PyDictRef>) -> Result<(), Option<PyDictRef>> {
Expand Down
5 changes: 3 additions & 2 deletions crates/vm/src/protocol/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ impl PyObject {
dict: Option<PyDictRef>,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>> {
let name = name_str.as_wtf8();
let obj_cls = self.class();
let cls_attr_name = vm.ctx.interned_str(name_str);
let cls_attr = match cls_attr_name.and_then(|name| obj_cls.get_attr(name)) {
Expand All @@ -251,7 +250,9 @@ impl PyObject {
let dict = dict.or_else(|| self.dict());

let attr = if let Some(dict) = dict {
dict.get_item_opt(name, vm)?
// `Py<PyStr>` rather than its `&Wtf8`: the key type carries the
// cached hash and compares interned keys by pointer.
dict.get_item_opt(name_str, vm)?
} else {
None
};
Expand Down