Skip to content

Free threaded scaling issues with @classmethod and @staticmethod #145779

Closed

Description

@colesbury

Bug report

We have reference count contention from:

  1. The classmethod object, which is stored in the MRO cache. This should be pretty easy to fix (enable deferred reference counting when creating the object).

  2. sm_descr_get increfs sm->sm_callable

  3. cm_descr_get creates a new PyMethod_New, which increfs cm->cm_callable and the containing class

ftscalingbench.py

class MyClassMethod:
    @classmethod
    def my_classmethod(cls):
        return cls

    @staticmethod
    def my_staticmethod():
        pass

@register_benchmark
def classmethod_call():
    obj = MyClassMethod()
    for _ in range(1000 * WORK_SCALE):
        obj.my_classmethod()

@register_benchmark
def staticmethod_call():
    obj = MyClassMethod()
    for _ in range(1000 * WORK_SCALE):
        obj.my_staticmethod()

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    performancePerformance or resource usage
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Mar 10, 2026
  2. brijkapadia commented on Mar 11, 2026

    @brijkapadia
    Contributor

    The classmethod and staticmethod objects, which are stored in the MRO cache. This should be pretty easy to fix (enable deferred reference counting when creating the object).

    Don't staticmethod objects already use deferred reference counting?

  3. colesbury commented on Mar 11, 2026

    @colesbury
    ContributorAuthor

    Yes

  4. colesbury commented on Mar 11, 2026

    @colesbury
    ContributorAuthor

    I've edited the issue description

  5. added 10 commits that reference this issue on Mar 11, 2026
  6. khalidsaidi commented on Mar 25, 2026

    @khalidsaidi
  7. added a commit that references this issue on Apr 25, 2026
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

    3.14bugs and security fixes3.15bugs and security fixesperformancePerformance or resource usagetopic-free-threadingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions