FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fixtures: fix catastrophic performance problem in `reorder_items` by bluetech · Pull Request #12409 · pytest-dev/pytest · GitHub

fixtures: fix catastrophic performance problem in reorder_items - #12409

Merged
bluetech merged 2 commits into
pytest-dev:mainfrom
bluetech:reorder-items-perf
Jun 4, 2024
Merged

fixtures: fix catastrophic performance problem in reorder_items#12409
bluetech merged 2 commits into
pytest-dev:mainfrom
bluetech:reorder-items-perf

Conversation

bluetech commented Jun 2, 2024

Copy link
Copy Markdown
Member

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.

nicoddemus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread changelog/12355.bugfix.rst Outdated
@@ -0,0 +1 @@
Fix possible catasrophic performance slowdown on a certain parametrization pattern involving many higher-scoped parameters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
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 force-pushed the reorder-items-perf branch from 7a230b0 to e89d23b Compare June 4, 2024 07:15
bluetech merged commit d4dbe77 into pytest-dev:main Jun 4, 2024
bluetech deleted the reorder-items-perf branch June 4, 2024 07:16
Comment thread src/_pytest/fixtures.py
other_scoped_items_by_argkey = items_by_argkey[other_scope]
for argkey in argkeys_by_item[other_scope].get(i, ()):
other_scoped_items_by_argkey[argkey][i] = None
other_scoped_items_by_argkey[argkey].move_to_end(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@bluetech I've hit some corner case on PyPy where move_to_end() seems to be raising a KeyError and crashing the item collection entirely...

webknjaz Mar 19, 2025
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Oh, nevermind. This is a regression of v8.2.2 and this PR only hit release in v8.3.0.

Nope, it was backported into 8.2.2: #13312

webknjaz added a commit to webknjaz/multidict that referenced this pull request Mar 19, 2025
This patch temporarily restricts pytest version below 8.2.2 under PyPy
due to a discovered regression that it introduced [[1]].

The regression has been observed on at least `pypy3.9-7.3.16`,
`pypy3.10-7.3.19` and `pypy3.11-7.3.19`.

It can be triggered by running the following in affected runtimes:

  pytest --collect-only --no-cov tests/test_abc.py tests/test_copy.py tests/test_incorrect_args.py tests/test_multidict.py tests/test_mypy.py tests/test_pickle.py tests/test_types.py tests/test_update.py tests/test_version.py

[1]: pytest-dev/pytest#13312
[2]: pytest-dev/pytest#12414
[3]: pytest-dev/pytest#12409
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

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

3 participants


Back | FazBrowse Home | New Git URL