Skip to content

Free-threading scalability issue in ABC isinstance checking #157526

Open

Description

@itamarst

Bug report

Bug description:

This is a self-contained reproducer of a real issue I encountered (see below):

import numbers
from concurrent.futures import ThreadPoolExecutor
from time import time

mylist = [1.0] * 100_000

def check(_):
    for x in mylist:
        isinstance(x, numbers.Integral)

for cores in [1, 2, 4, 8]:
    start = time()
    with ThreadPoolExecutor(cores) as pool:
        list(pool.map(check, range(cores)))
    print(cores, time() - start)

I ran this on my computer, which has 12 cores. I would expect this to the same amount of time regardless of number of threads, since they ought to be run in parallel. In fact, the output looks like this:

Python 3.14t:

1 0.030498981475830078
2 0.07532048225402832
4 0.17853212356567383
8 0.5576419830322266

Python 3.15t (3.15rc2):

1 0.032598018646240234
2 0.05810952186584473
4 0.14814376831054688
8 0.31418490409851074

Original real-world issue

I discovered this issue while benchmarking some scikit-learn code. In particular it's caused by code that uses https://github.com/scikit-learn/scikit-learn/blob/d6f188097e255822d99633f13f4d6304cc76a7c8/sklearn/utils/_missing.py#L40 on all the values in a data structure.

In practice I have optimized away much of the usage of this function, so in future versions of scikit-learn (post-1.9) it hopefully won't be a bottleneck in practice. But it's definitely a real bottleneck in sklearn 1.9, and presumably other people may encounter it in other code.

Potential source of bottleneck

Looking at the profile output of samply suggests _in_weak_set's critical section, maybe (thanks to @ngoldbaum for the link:

int incache = _in_weak_set(impl, &impl->_abc_cache, subclass);
).

Image

CPython versions tested on:

3.14, 3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bug
    on Sep 14, 2026
  2. added
    performance
    on Sep 14, 2026
  3. ngoldbaum commented on Sep 14, 2026

    @ngoldbaum
    Contributor

    @mpage @kumaraditya303 do you think it might make sense to add a lock-free fast path to the ABC implementation's cache lookups? Maybe just replacing the sets with dicts would be enough?

  4. kumaraditya303 commented on Sep 15, 2026

    @kumaraditya303
    Contributor

    do you think it might make sense to add a lock-free fast path to the ABC implementation's cache lookups?

    I looked at this with help of Claude and there are several contention spots causing the slowdown:

    Every cached check goes through the following shared-state accesses, so
    throughput drops as threads are added (roughly 0.1x per-thread throughput
    at 4 threads, 0.03x at 8 threads, compared to a single thread):

    1. _PyObject_LookupSpecial(cls, "__instancecheck__") in
      PyObject_IsInstance() returns a strong reference to the shared
      ABCMeta.__instancecheck__ function object, an atomic increment and
      decrement on one cache line shared by all threads. The same applies to
      __subclasscheck__ in PyObject_IsSubclass().

    2. Bound method creation. The lookup binds the function to the class,
      creating a PyMethodObject that increfs both the function and the ABC
      class again, then decrefs them when the temporary method is released.

    3. PyObject_GetAttr(cls, "_abc_impl") in _abc.c takes a strong
      reference to the shared _abc_data object for the duration of the call.

    4. Py_BEGIN_CRITICAL_SECTION(impl) in _in_weak_set() is taken by
      every reader just to load the cache set pointer, serializing all
      readers of the same ABC on its mutex.

    5. PyWeakref_NewRef(subclass, NULL) on every lookup takes the
      striped weakref-list lock of the class being checked and increfs its
      shared basic weakref object.

    6. instance.__class__ goes through the object.__class__ getset
      descriptor, which returns Py_NewRef(Py_TYPE(instance)), an atomic
      on the shared reference count of the instance's class. For instances
      of Python classes this alone is enough to collapse scaling.

  5. kumaraditya303 commented on Sep 15, 2026

    @kumaraditya303
    Contributor

    The implementation currently uses weakrefs heavily which is slow even on GIL-enabled builds and much more slower on free-threading. This is why in 3.14 I removed weakrefs completely from asyncio and instead use C linked-list with borrowed reference which scales linearly with threads. abc needs something similar for better performance and scaling, and I think that would also fix #92810.

  6. removed
    type-bug
    on Sep 15, 2026
  7. added
    3.16
    on Sep 15, 2026
  8. added a commit that references this issue on Sep 22, 2026
  9. kumaraditya303 commented on Nov 21, 2026

    @kumaraditya303
    Contributor

    I created #162361 to make the cache lookup lock-free for better scaling.

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

Metadata

Metadata

Labels

3.16performancetopic-free-threading

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions