Repository navigation
gh-92810: Lock-free cache implementation for ABCMeta - #162361
Open
kumaraditya303 wants to merge 5 commits into
Open
kumaraditya303 wants to merge 5 commits into
kumaraditya303 wants to merge 5 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
isinstance()hit, subclassisinstance()hit, registered classisinstance()miss, cachedissubclass()hitissubclass()miss, cachedisinstance([], Sequence)isinstance(1, Sequence)isinstance({}, Mapping)register(), 100 subclassesisinstance()Multi-threaded (free-threaded build)
Each thread checks its own objects, ns per call per thread:
isinstance()hitisinstance()miss, cachedisinstance()hit, registered classissubclass()hitisinstance([], Sequence)isinstance(1, Sequence)isinstance()Cached checks now scale the same as a non-ABC
isinstance(). Uncached checks are faster but still don't scale, same asmain.Reproducers from the issue
Time and peak RSS, default build:
Free-threaded build:
The memory usage of @dolfinus's reproducer goes from ~3.8 GiB to ~35 MiB and it is ~14x faster.
_abc._abc_subclasscheckhas very poor performance and (I think) a memory leak #92810