Skip to content

gh-92810: Lock-free cache implementation for ABCMeta - #162361

Open
kumaraditya303 wants to merge 5 commits into
python:mainfrom
kumaraditya303:gh-92810-abc-c-tables
Open

kumaraditya303 wants to merge 5 commits into
python:mainfrom
kumaraditya303:gh-92810-abc-c-tables

Conversation

@kumaraditya303

@kumaraditya303 kumaraditya303 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Replace the sets of weak references used for the ABC registry and the positive/negative isinstance caches with hash tables keyed by class identity. Cached isinstance() and issubclass() checks are served without locking or reference count updates on the free-threaded build, and a negative result is no longer propagated into the cache of every subclass of the ABC, which made such checks quadratic for large hierarchies.

Benchmark results on Apple M4 (10 cores), release build:

Single-threaded (default build)

ns per call:

Case main PR Speedup
isinstance() hit, subclass 75.2 49.8 1.5x
isinstance() hit, registered class 172.0 50.4 3.4x
isinstance() miss, cached 81.6 50.4 1.6x
issubclass() hit 68.9 49.5 1.4x
issubclass() miss, cached 76.2 50.3 1.5x
isinstance([], Sequence) 78.4 54.2 1.4x
isinstance(1, Sequence) 86.5 52.5 1.6x
isinstance({}, Mapping) 96.5 68.6 1.4x
uncached hit 150 69 2.2x
uncached miss, 2 subclasses 391 177 2.2x
uncached miss, 100 subclasses 7,063 3,676 1.9x
uncached miss, 1000 subclasses 77,570 36,069 2.2x
re-check after register(), 100 subclasses 27,257 4,924 5.5x
new subclass + first isinstance() 2,841 2,543 1.1x

Multi-threaded (free-threaded build)

Each thread checks its own objects, ns per call per thread:

Case main 1T main 8T PR 1T PR 8T Speedup at 8T
isinstance() hit 92.0 1,374 62.3 105 13x
isinstance() miss, cached 117 1,351 70.3 109 12x
isinstance() hit, registered class 289 3,586 77.5 129 28x
issubclass() hit 105 1,476 62.5 108 14x
isinstance([], Sequence) 121 1,268 70.3 116 11x
isinstance(1, Sequence) 128 1,427 72.4 117 12x
uncached check (new class each time) 3,939 46,160 2,845 33,369 1.4x
non-ABC isinstance() 14.1 26.3 15.8 28.8 –

Cached checks now scale the same as a non-ABC isinstance(). Uncached checks are faster but still don't scale, same as main.

Reproducers from the issue

Time and peak RSS, default build:

Reproducer main PR
original report, 5,000 iterations 1.56 s, 30.8 MiB 0.055 s, 14.8 MiB
original report, 20,000 iterations 3.51 s, 31.0 MiB 0.22 s, 15.2 MiB
Viicos's, 10,000 iterations 8.85 s, 46.0 MiB 0.11 s, 14.5 MiB
dolfinus's, 6,000 objects 11.5 s, 3,796 MiB 0.78 s, 33.0 MiB

Free-threaded build:

Reproducer main PR
original report, 5,000 iterations 0.23 s, 15.0 MiB 0.060 s, 15.6 MiB
original report, 20,000 iterations 0.92 s, 15.0 MiB 0.23 s, 15.6 MiB
Viicos's, 10,000 iterations 0.77 s, 15.6 MiB 0.10 s, 15.3 MiB
dolfinus's, 6,000 objects 11.6 s, 3,880 MiB 0.84 s, 36.3 MiB

The memory usage of @dolfinus's reproducer goes from ~3.8 GiB to ~35 MiB and it is ~14x faster.

Replace the sets of weak references used for the ABC registry and the
positive/negative isinstance caches with hash tables keyed by class
identity. Cached isinstance() and issubclass() checks are served without
locking or reference count updates on the free-threaded build, and a
negative result is no longer propagated into the cache of every subclass
of the ABC, which made such checks quadratic for large hierarchies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant