-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-47131: Speedup AST comparisons in test_unparse by using node traversal #32132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you moved missing inside the function definition, now you may move the function definition at the class level (method) of the module level (function, you should pass the testcase as an argument in this case), to avoid the cost of defining a new function at each assertASTEqual() call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are concerned about performance, using an inner function is faster than calling a method. True, the setup cost is greater, but that time is more than recovered due to attribute lookup after a few hundred comparisons. Global (module level) functions suffer the same fate as well but require more comparisons before getting overtaken.
After instrumenting test_unparse to get the number of calls to assertASTEqual and compares, I whipped up a reproducible minimal test script to show the timings. The script is here for you to run yourself.
Using main on Win10:
100 -> inner: 29 usec method: 27.4 usec
200 -> inner: 56.8 usec method: 48 usec
300 -> inner: 76.3 usec method: 86.4 usec
CosmeticTestCase (14 compares)
inner: 88.9 usec
method: 77.9 usec
DirectoryTestCase (400 compares)
inner: 74.1 msec
method: 79.5 msec
UnparseTestCase (400 compares)
inner: 91 msec
method: 97.6 msec
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I wrote previously, if there is a good reason to define a "local" (nested?) function, please add a comment here for future readers. Otherwise, someone who doesn't like nested functions can move the function (during a random refactoring) without knowing that you ran benchmarks and the code is faster when written like that. It's very easy to lose information when code is read/modified 5, 10 or 20 years later. And yes, it happens to me often to read 20 years old Python code and I have to dig into Git history (with unpleasant issues about CVS, SVN and HG commit numbers) and very old bug tracker issues to attempt to rebuild the rationale for the existing code before feeling safe to change it.
The performance doesn't make sense (method/module level code) to me. It should be as fast or faster. But my only god are benchmarks: I only trust benchmarks numbers :-)