fixtures: fix quadratic behavior in the number of autouse fixtures#7931
Merged
bluetech merged 3 commits intopytest-dev:masterfrom Oct 24, 2020
Merged
fixtures: fix quadratic behavior in the number of autouse fixtures#7931bluetech merged 3 commits intopytest-dev:masterfrom
bluetech merged 3 commits intopytest-dev:masterfrom
Conversation
nicoddemus
approved these changes
Oct 24, 2020
Member
nicoddemus
left a comment
There was a problem hiding this comment.
Awesome work @bluetech!
Left two minor comments, feel free to ignore them. 👍
src/_pytest/nodes.py
Outdated
| @lru_cache(maxsize=None) | ||
| def _splitnode(nodeid: str) -> Tuple[str, ...]: | ||
| """Split a nodeid into constituent 'parts'. | ||
| def getparentnodeids(nodeid: str) -> Iterator[str]: |
Member
There was a problem hiding this comment.
Nice function and docs!
Did you try to measure with @lru_cache being applied to it?
Member
Author
There was a problem hiding this comment.
No, but I think it shouldn't be a hot spot anymore, and shouldn't be called with the same nodeid a million times like before. But if it does show up in a profile again it might make sense.
ischildnode can be quite hot in some cases involving many fixtures. However it is always used in a way that the nodeid is constant and the baseid is iterated. So we can save work by pre-computing the parents of the nodeid and use a simple containment test. The `_getautousenames` function has the same stuff open-coded, so change it to use the new function as well.
This reads better.
It turns out all autouse fixtures are kept in a global list, and thinned out for a particular node using a linear scan of the entire list each time. Change the list to a dict, and only take the nodes we need.
5dd72a3 to
470ea50
Compare
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 #4824. This contains 3 commits which optimize the slow parts of xunit/autouse fixtes, the last commit fixes the remaining quadratic behavior (continuation of #7929). Please see the commit messages for details.
On the bench/xunit.py benchmark,
--collect-only, before:After:
which is the usual slowness :)