Skip to content

Avoid redundant garbage collector traversals #159437

Open

Description

@mfalco-oai

This issue tracks a series of GC optimizations that we have made in OpenAI’s CPython fork and would like to bring upstream. They target the garbage collector in GIL-enabled builds and reduce repeated walks over the same objects during collection.

The work includes:

  • Combining reference initialization and subtraction for full collections, with the permanent-generation bookkeeping needed to preserve gc.freeze() and gc.unfreeze() behavior.
  • Skipping finalizer scans and late weakref cleanup when earlier collection phases establish they are unnecessary.
  • Clearing unreachable flags and counting unreachable objects during existing passes.
  • Skipping reachability traversal for surviving exact tuples that can be untracked immediately.

In production at OpenAI, we've observed roughly a one-third reduction in GC time with these combined improvements.

The changes will be proposed in focused PRs against main, with correctness and performance measurements for each.

Activity

  1. added
    performancePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.16new features, bugs and security fixes
    on Oct 14, 2026
  2. brandt-oai commented on Oct 14, 2026

    @brandt-oai
  3. picnixz commented on Oct 14, 2026

    @picnixz
    Member
  4. pablogsal commented on Oct 14, 2026

    @pablogsal
    Member

    Parented to #159137 (see previous work there)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions