Skip to content

Unexpected scaling bottleneck in _PyType_Lookup / _PyType_LookupStackRefAndVersion #145685

Description

@colesbury

Bug report

There are scaling bottlenecks in the slow paths of _PyType_LookupStackRefAndVersion(), and it turns out that these can be a major bottleneck in larger applications.

For context, I've been testing Zulip with 3.14t and multithreading. The problem is basically:

  1. There are a lot of attribute loads that are not monomorphic (so specialization is of limited help)
  2. There are lots of unique (type, attr) lookups, so there are frequent collisions in the global MRO cache

The slow path for this case acquires the global type lock.

We should avoid acquiring the global type lock if the type already has a valid version tag. We'll also want to avoid reference count contention on the MRO tuple.

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    3.14bugs and security fixes
    3.15bugs and security fixes
    performancePerformance or resource usage
    on Mar 9, 2026
  2. added 8 commits that reference this issue on Mar 9, 2026
  3. added a commit that references this issue on Mar 10, 2026
  4. added a commit that references this issue on Mar 10, 2026
  5. 7 remaining items

  6. added a commit that references this issue on Mar 12, 2026
  7. added a commit that references this issue on Mar 12, 2026
  8. khalidsaidi commented on Mar 25, 2026

    @khalidsaidi
  9. added 4 commits that reference this issue on Apr 25, 2026
  10. added a commit that references this issue on Jul 21, 2026
  11. maurycy commented on Aug 5, 2026

    @maurycy
    Contributor

    @colesbury @kumaraditya303 After daf09e1 _PySeqLock is not used anywhere anymore:

    maurycy@gimel cpython (main 9ccd5bb) % rg _PySeqLock
    Objects/typeobject.c
    11:#include "pycore_lock.h"          // _PySeqLock_*
    
    Include/internal/pycore_lock.h
    221:} _PySeqLock;
    224:PyAPI_FUNC(void) _PySeqLock_LockWrite(_PySeqLock *seqlock);
    227:PyAPI_FUNC(void) _PySeqLock_UnlockWrite(_PySeqLock *seqlock);
    231:PyAPI_FUNC(void) _PySeqLock_AbandonWrite(_PySeqLock *seqlock);
    234:PyAPI_FUNC(uint32_t) _PySeqLock_BeginRead(_PySeqLock *seqlock);
    238:PyAPI_FUNC(int) _PySeqLock_EndRead(_PySeqLock *seqlock, uint32_t previous);
    242:PyAPI_FUNC(int) _PySeqLock_AfterFork(_PySeqLock *seqlock);
    
    Python/lock.c
    579:void _PySeqLock_LockWrite(_PySeqLock *seqlock)
    600:void _PySeqLock_AbandonWrite(_PySeqLock *seqlock)
    607:void _PySeqLock_UnlockWrite(_PySeqLock *seqlock)
    614:uint32_t _PySeqLock_BeginRead(_PySeqLock *seqlock)
    625:int _PySeqLock_EndRead(_PySeqLock *seqlock, uint32_t previous)
    640:int _PySeqLock_AfterFork(_PySeqLock *seqlock)

    Shall we remove it?

  12. colesbury commented on Aug 5, 2026

    @colesbury
    ContributorAuthor
  13. maurycy commented on Aug 5, 2026

    @maurycy
    Contributor
  14. added a commit that references this issue on Aug 6, 2026
  15. added a commit that references this issue on Aug 14, 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