Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f24691e
specialize: check the member descriptor's type before caching its slo…
youknowone Aug 14, 2026
40e67e1
socket: reserve recv()'s buffer fallibly
youknowone Aug 14, 2026
40a839b
types: count the __call__ and __get__ slot dispatches as recursion
youknowone Aug 14, 2026
bb744d6
typevar: show a ParamSpecArgs origin by its repr
youknowone Aug 14, 2026
7e4b96e
Do not hold a lock across a call back into Python
youknowone Aug 14, 2026
6e47646
Validate memoryview.cast() arguments and export negative strides corr…
youknowone Aug 14, 2026
2432fd7
Charge native recursion to the stack, not to the frame limit
youknowone Aug 14, 2026
02a6193
Report a size that cannot be allocated instead of aborting on it
youknowone Aug 14, 2026
ff52fe0
marshal: answer allow_code where a code object is written or read
youknowone Aug 14, 2026
d7cf971
Do not lock an object while running code that can reach it
youknowone Aug 14, 2026
61b989b
Stop asserting a pbkdf2 message that depends on the width of a C long
youknowone Aug 14, 2026
c9449a6
Publish and read the same pointer for a thread's top frame
youknowone Aug 15, 2026
ee782e0
Decide stop-the-world parking under the thread registry lock
youknowone Aug 15, 2026
fab2b3b
Keep an atexit callback alive while it is being compared
youknowone Aug 15, 2026
e3dcd8e
Hold atexit entries in PyRc rather than Arc
youknowone Aug 15, 2026
773ae23
Do not hold a buffer's storage while waiting for a peer
youknowone Aug 15, 2026
125d5aa
Do not hold a buffer's storage while waiting to hand it over
youknowone Aug 15, 2026
62ed618
Check select()'s descriptor limit while the sequence is walked
youknowone Aug 15, 2026
c65fe42
Bound native recursion where the C stack cannot be measured
youknowone Aug 15, 2026
00b64c3
Report the failure to allocate pbkdf2's key and Take.readinto's scratch
youknowone Aug 15, 2026
7c40cb9
Read the frozen-code tuple length as a signed length
youknowone Aug 15, 2026
ce43008
Make the snippets from the fuzzer sweep assert what they check
youknowone Aug 15, 2026
3d69690
Ask for a marshal container's room instead of assuming it
youknowone Aug 15, 2026
366ef3e
Compare the blocking-buffer snippet against something
youknowone Aug 15, 2026
ab0d4ff
Refuse array.frombytes() a source whose items are not bytes
youknowone Aug 15, 2026
7fad3b0
Tell apart the ways marshal data can be bad
youknowone Aug 15, 2026
a7f7021
Hash the collector's tables by address rather than by SipHash
youknowone Aug 16, 2026
691d6ca
Keep the collection's candidates and their counts in one table
youknowone Aug 16, 2026
3139b95
gc: collect referents into one buffer instead of a vector per object
youknowone Aug 16, 2026
dce42a3
memoryview and struct: match the checks and errors of the reference
youknowone Aug 16, 2026
5a5ba17
io: decide the readinto path by file type, not by seekability
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
Keep the collection's candidates and their counts in one table
A collection built a set of candidates and, beside it, a map from the same
addresses to their reference counts. Both were probed for every edge in
the heap -- membership from the set, the count from the map -- so each
edge paid to hash the same address twice, and each candidate paid to be
inserted twice. The map alone answers both questions.

The candidates also keep a walkable order now, which the reference
subtraction pass needs since it writes the counts while reading the
candidates, and which the unreachable set is built from instead of a
set difference.

Over the 423k-object heap measured in the previous commit: 0.15s to
0.13s live, and 0.79s to 0.49s dead.

Assisted-by: Claude
Assisted-by: Codex:GPT-5
  • Loading branch information
youknowone committed Aug 16, 2026
commit 691d6cad81bc6b7f35ce3eb3449008808a3c401c
58 changes: 25 additions & 33 deletions crates/vm/src/gc_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,14 +594,25 @@ impl GcState {
retired.sort_unstable();
retired
};
let mut collecting: GcSet<GcPtr> = GcSet::default();
// The candidates and their reference counts go in one table, not a set
// beside a map: every edge in the heap is looked up here, and the two
// held the same keys, so a second table only bought a second hash of
// the same address. `candidate_ptrs` keeps them in a walkable order,
// since the counts are written while the candidates are read.
let mut gc_refs: GcMap<GcPtr, usize> = GcMap::default();
let mut candidate_ptrs: Vec<GcPtr> = Vec::new();
for gen_list in &gen_locks {
for obj in gen_list.iter() {
if retired.binary_search(&obj.gc_owner()).is_ok() {
obj.set_gc_owner(GC_NO_OWNER);
}
if obj.strong_count() > 0 && is_owned_by(obj, owner) {
collecting.insert(GcPtr(NonNull::from(obj)));
let strong_count = obj.strong_count();
let ptr = GcPtr(NonNull::from(obj));
if strong_count > 0
&& is_owned_by(obj, owner)
&& gc_refs.insert(ptr, strong_count).is_none()
{
candidate_ptrs.push(ptr);
}
}
}
Expand All @@ -621,7 +632,7 @@ impl GcState {
.retain(|tag| retired.binary_search(tag).is_err());
}

if collecting.is_empty() {
if candidate_ptrs.is_empty() {
// Reset counts for generations whose objects were promoted away.
// For gen2 (oldest), survivors stay in-place so don't reset gen2 count.
let reset_end = if generation >= 2 { 2 } else { generation + 1 };
Expand All @@ -640,26 +651,10 @@ impl GcState {
};
}

let candidates = collecting.len();
let candidates = candidate_ptrs.len();

if debug.contains(GcDebugFlags::STATS) {
eprintln!(
"gc: collecting {} objects from generations 0..={}",
collecting.len(),
generation
);
}

// Step 2: Build gc_refs map (copy reference counts)
let mut gc_refs: GcMap<GcPtr, usize> = GcMap::default();

#[expect(
clippy::iter_over_hash_type,
reason = "Iteration order doesn't matter here"
)]
for &ptr in &collecting {
let obj = unsafe { ptr.0.as_ref() };
gc_refs.insert(ptr, obj.strong_count());
eprintln!("gc: collecting {candidates} objects from generations 0..={generation}");
}

// Step 3: Subtract internal references
Expand All @@ -670,21 +665,14 @@ impl GcState {
// results, causing live objects to be incorrectly collected.
let mut referents_map: GcMap<GcPtr, Vec<NonNull<PyObject>>> = GcMap::default();

#[expect(
clippy::iter_over_hash_type,
reason = "Iteration order doesn't matter here"
)]
for &ptr in &collecting {
for &ptr in &candidate_ptrs {
let obj = unsafe { ptr.0.as_ref() };
if obj.strong_count() == 0 {
continue;
}
let referent_ptrs = unsafe { obj.gc_get_referent_ptrs() };
for &child_ptr in &referent_ptrs {
let gc_ptr = GcPtr(child_ptr);
if collecting.contains(&gc_ptr)
&& let Some(refs) = gc_refs.get_mut(&gc_ptr)
{
if let Some(refs) = gc_refs.get_mut(&GcPtr(child_ptr)) {
*refs = refs.saturating_sub(1);
}
}
Expand Down Expand Up @@ -723,15 +711,19 @@ impl GcState {
};
for &child_ptr in referent_ptrs {
let gc_ptr = GcPtr(child_ptr);
if collecting.contains(&gc_ptr) && reachable.insert(gc_ptr) {
if gc_refs.contains_key(&gc_ptr) && reachable.insert(gc_ptr) {
worklist.push(gc_ptr);
}
}
}
}

// Step 5: Find unreachable objects
let unreachable: Vec<GcPtr> = collecting.difference(&reachable).copied().collect();
let unreachable: Vec<GcPtr> = candidate_ptrs
.iter()
.filter(|ptr| !reachable.contains(ptr))
.copied()
.collect();

// With the world stopped, every frame on any thread's call stack is a
// live root that is externally referenced and must have been
Expand Down