Skip to content
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

fixtures: fix catastrophic performance problem in reorder_items #12409

Merged
merged 2 commits into from
Jun 4, 2024

Conversation

bluetech
Copy link
Member

@bluetech bluetech commented Jun 2, 2024

First commit is a cleanup commit (no functional changes intended).

The second commit fixes #12355.

In the issue, it was reported that the reorder_items has quadratic (or worse...) behavior with certain simple parametrizations. After some debugging I found that the problem happens because the "Fix items_by_argkey order" loop keeps adding the same item to the deque, and it reaches epic sizes which causes the slowdown.

I don't claim to understand how the reorder_items algorithm works, but if as far as I understand, if an item already exists in the deque, the correct thing to do is to move it to the front. Since a deque doesn't have such an (efficient) operation, this switches to OrderedDict which can efficiently append from both sides, deduplicate and move to front.

This makes some minor clarity and performance improvements to the code.
Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for taking the time to look into this!

@@ -0,0 +1 @@
Fix possible catasrophic performance slowdown on a certain parametrization pattern involving many higher-scoped parameters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Fix possible catasrophic performance slowdown on a certain parametrization pattern involving many higher-scoped parameters.
Fix possible catastrophic performance slowdown on a certain parametrization pattern involving many higher-scoped parameters.

Fix pytest-dev#12355.

In the issue, it was reported that the `reorder_items` has quadratic (or
worse...) behavior with certain simple parametrizations. After some
debugging I found that the problem happens because the "Fix
items_by_argkey order" loop keeps adding the same item to the deque,
and it reaches epic sizes which causes the slowdown.

I don't claim to understand how the `reorder_items` algorithm works, but
if as far as I understand, if an item already exists in the deque, the
correct thing to do is to move it to the front. Since a deque doesn't
have such an (efficient) operation, this switches to `OrderedDict` which
can efficiently append from both sides, deduplicate and move to front.
@bluetech bluetech merged commit d4dbe77 into pytest-dev:main Jun 4, 2024
2 of 3 checks passed
@bluetech bluetech deleted the reorder-items-perf branch June 4, 2024 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Collecting" hangs forever if mark.parametrize is used with scope session or module
2 participants