Free-threaded builds can experience heavy contention on PyType_IsSubtype #116868
Copy link
Copy link
Closed
Labels
performancePerformance or resource usagePerformance or resource usagetopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Mar 15, 2024 - addedperformancePerformance or resource usagePerformance or resource usage
on Mar 15, 2024 - added a commit that references this issue
on Mar 15, 2024
Metadata
Metadata
Assignees
Labels
performancePerformance or resource usagePerformance or resource usagetopic-free-threadingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Bug report
Bug description:
PyType_IsSubtypetakes the type lock to ensure that MRO is being accessed in a thread-safe manner as it needs to read and incref it. This can result in heavy contention in some pretty common scenarios (e.g. whensys.setprofileis used we experience heavy contention across threads doingPyCFunction_Check, profiling simple workloads can be nearly 50% slower in some cases). We can avoid this by usingPy_TryIncrefand only fallback to the lock when the MRO isn't yet shared.CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs