Skip to content

Optimize _PyObject_IS_GC in free-threaded build #114733

Closed

Description

@colesbury

Feature or enhancement

@nascheme writes the following suggestion (from #114564 (comment)):

_PyObject_IS_GC() could be optimized in the nogil build. That function is a bit expensive since it has to check for tp_is_gc() and possibly call it. Now that all objects have ob_gc_bits, we could store the "IS GC" status as another bit there, when the object is allocated.

I think this might require a bit of refactoring, but worth doing eventually. At a glance:

  • _PyObject_GC_New() (and related functions) could initialize ob_gc_bits with the "IS GC" set
  • PyType_GenericAlloc() would also need to initialize ob_gc_bits with the "IS GC" set if the type is a GC type.
  • _Py_NewReference() requires some care. It. currently initializes ob_gc_bits to zero and it's used after "allocating" objects from freelists.

Linked PRs

Activity

  1. added
    performancePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Sep 5, 2026
  2. corona10 commented on Oct 19, 2026

    @corona10
    Member

    I will re-evaluate this optimization, my last year's experiment was not worth doing that.
    Let's rewrite the patch and if it does not change signifacnt changes, lets' close the issue.
    cc @mpage @nascheme

  3. corona10 commented on Oct 19, 2026

    @corona10
    Member

    Let me get metrics first based on pyperformance.

  4. corona10 commented on Oct 19, 2026

    @corona10
    Member
    Image Image

    sample_gc_collect.txt

    sample_gc_traversal.txt

    Benchmarks based on gc_collect and gc_traversal show this is too small a portion for optimization; I would like to close this issue and revisit later if we have to.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions