Skip to content

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

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

kumaraditya303 wants to merge 7 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.
@dolfinus

Copy link
Copy Markdown

Should py_abc implementation be updated as well?

@ngoldbaum

Copy link
Copy Markdown
Contributor

Should py_abc implementation be updated as well?

I wouldn't for now given the direction this discussion is heading: https://discuss.python.org/t/does-anyone-have-a-hard-dependency-on-pyio-proposing-pep-399-exception/110202/7

@nascheme nascheme left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice optimization, especially the multi-thread scaling. Additional comments:

  • Could note in NEWS that __subclasshook__ defined on a subclass ABC is now called again when that subclass is queried directly after a parent query. I'm not sure anyone cares though.
  • _py_abc.py still has the quadratic behaviour. Given DPO discussion on these pure-Python alternatives, perhaps we don't care and we just leave it.
  • Could use a unit test for the reload of abc. Optionally, a new test that catches the stale negative result caching bug (if it's not too expensive to trigger that race).

Comment thread Modules/_abc.c Resolved
Comment thread Modules/_abc.c Resolved
Comment thread Modules/_abc.c Resolved
Comment thread Modules/_abc.c Resolved
Comment thread Modules/_abc.c Resolved
Comment thread Modules/_abc.c Resolved
Comment thread Modules/_abc.c Resolved
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.

4 participants