ast/interpreter: Use match statements#15737
Open
dcbaker wants to merge 1 commit intomesonbuild:masterfrom
Open
ast/interpreter: Use match statements#15737dcbaker wants to merge 1 commit intomesonbuild:masterfrom
dcbaker wants to merge 1 commit intomesonbuild:masterfrom
Conversation
The slightly increases the lines of code (adding the `match` statement itself), but generally makes the code easier to follow (and potentially faster), since we can replace things like `if struct.attribute == x:` with `case x:` and `isinstance(struct, Foo)` with `case Foo()`. I have not converted every if/elif tree into match statements, but have tried to be a bit conservative of only converting the big ones. This also doesn't make use of any super powerful match features, like unpacking.
417b87d to
55a7e90
Compare
Contributor
|
Bytecode-wise I see instead of where assert(PyTuple_CheckExact(kwargs));
// First, an isinstance check:
if (PyObject_IsInstance(subject, type) <= 0) {
return NULL;
}
// Short circuit if there aren't any arguments:
Py_ssize_t nkwargs = PyTuple_GET_SIZE(kwargs);
Py_ssize_t nattrs = nargs + nkwargs;
if (!nattrs) {
return PyTuple_New(0);
}So there is no call (though in the end you pay anyway the overhead of |
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.
The slightly increases the lines of code (adding the
matchstatement itself), but generally makes the code easier to follow (and potentially faster), since we can replace things likeif struct.attribute == x:withcase x:andisinstance(struct, Foo)withcase Foo().I have not converted every if/elif tree into match statements, but have tried to be a bit conservative of only converting the big ones. This also doesn't make use of any super powerful match features, like unpacking.