Conversation
This was referenced Oct 4, 2026
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.
isinstance(x, complex)wasn't specialized, so every call looked upbuiltins.complexby name and went throughPyObject_IsInstance. Because the tuple fast path from #19949 only applies when every member is a primitive type,isinstance(x, (float, complex))was worse still: it built a new tuple on every call, andfloatlost its fast path too. This is an issue I have run into in my personal projects.This adds
isinstanceprimitives forcomplex,type,range,sliceandmemoryview(PyComplex_Check,PyType_Check, etc.), plus direct type object loads forcomplex,sliceandmemoryviewso things liketype(x) is complexdon't go throughbuiltinseither. Tuples such as(float, complex)and(bytes, bytearray, memoryview)now use the existing chained checks.Also fixes a typo in the name of the
isinstance(obj, int)primitive (builtints.isinstance).Benchmarks in ns, Python 3.12:
| | master | this PR |
|
isinstance(x, complex)| 43.0 | 5.9 ||
isinstance(x, (float, complex))| 106.0 | 7.1 ||
isinstance(x, (bytes, bytearray, memoryview))| 101.8 | 5.4 ||
type(x) is complex| 22.2 | 4.3 |