Conversation
Member
|
Thanks for all the mypyc PRs! I will probably leave them all to @JukkaL and @p-sawicki (or maybe I will take one-two if they are busy). |
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.
Fixes mypyc/mypyc#1227
This is just an idea for how to improve performance, for an issue that was exposed to me because of the issues in PR #22097 .
isinstance(x, (A, B))is specialized when every class in the tuple is a primitive type (#19949) or every class is a native class (mypyc/mypyc#413). Anything else falls back to the generic path, which builds a new tuple and callsPyObject_IsInstanceon every call, so the primitive types in the tuple lose their fast checks as well. That covers:isinstance(x, (float, MyNativeClass))isinstance(x, (float, Decimal))isinstance(x, (int, (float, str)))The chained checks added in #19949 could be generalized: flatten nested tuples, use the fast checks for primitive types and native classes, and check any other class with its own
PyObject_IsInstancecall, so no tuple is built. In a quick microbenchmark this takesisinstance(x, (float, MyNativeClass))from about 50 ns to about 5 ns per call.Relatedly,
isinstance(f(), ())currently returnsFalsewithout callingf().