-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Fix pathologically slow assertion diffs for large inputs (#8998) #14543
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
Open
kirilklein
wants to merge
4
commits into
pytest-dev:main
Choose a base branch
from
kirilklein:fix-8998-large-diff-perf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e232573
Fix pathologically slow assertion diffs for large inputs (#8998)
kirilklein 88ba6e1
Address review: keep detailed (fancy) diff and cheaper heuristic (#8998)
kirilklein 53a6d26
Merge branch 'main' into fix-8998-large-diff-perf
kirilklein 76143eb
Cover _bounded_prefix edge branches (#8998)
kirilklein 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Assertion failures comparing very large strings, lists, or dataclasses no longer hang for a long time (sometimes minutes) while building the diff. | ||
|
|
||
| When the inputs are large enough that :func:`difflib.ndiff` would be pathologically slow, pytest now runs it over a bounded prefix of the input instead, so the detailed (character-level) diff is kept for the part shown while the rest is truncated with a note. |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterator | ||
| from collections.abc import Sequence | ||
| from itertools import chain | ||
|
|
||
| from _pytest.assertion._typing import _HighlightFunc | ||
|
|
||
|
|
||
| # Past these limits ``difflib.ndiff`` becomes pathologically slow: its | ||
| # character-level "fancy replace" step compares every pair of similar lines in a | ||
| # differing block, so its cost grows with the *product* of the line count and | ||
| # the character count. A few hundred similar lines can already take seconds, and | ||
| # the pretty-printed form of a large list/dataclass takes minutes (see issue | ||
| # #8998). The limits below keep ``ndiff`` under roughly a second in the worst | ||
| # case. Above them we still run ``ndiff`` -- so the detailed diff is kept -- but | ||
| # only over a bounded prefix of the input. | ||
| NDIFF_MAX_INPUT_SIZE = 10_000 # characters (left + right) | ||
| DIFF_MAX_LINES = 100 # lines (left + right) | ||
|
|
||
|
|
||
| def ndiff_too_slow_for_text(left: str, right: str) -> bool: | ||
| """Whether ``ndiff`` would be pathologically slow for these strings. | ||
|
|
||
| Counts line separators instead of splitting into lines, so the check stays | ||
| cheap even for huge inputs. | ||
| """ | ||
| if left.count("\n") + right.count("\n") > DIFF_MAX_LINES: | ||
| return True | ||
| return len(left) + len(right) > NDIFF_MAX_INPUT_SIZE | ||
|
|
||
|
|
||
| def ndiff_too_slow_for_lines( | ||
| left_lines: Sequence[str], right_lines: Sequence[str] | ||
| ) -> bool: | ||
| """Whether ``ndiff`` would be pathologically slow for these lines. | ||
|
|
||
| Exits as soon as a limit is exceeded instead of measuring the whole input. | ||
| """ | ||
| if len(left_lines) + len(right_lines) > DIFF_MAX_LINES: | ||
| return True | ||
| size = 0 | ||
| for line in chain(left_lines, right_lines): | ||
| size += len(line) | ||
| if size > NDIFF_MAX_INPUT_SIZE: | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def truncated_ndiff( | ||
| left_lines: Sequence[str], | ||
| right_lines: Sequence[str], | ||
| highlighter: _HighlightFunc, | ||
| ) -> Iterator[str]: | ||
| """Yield an ``ndiff`` over a bounded prefix of the input (issue #8998). | ||
|
|
||
| The character-level diff is kept, but only for a slice small enough to | ||
| compute quickly; the rest of the input is dropped. | ||
| """ | ||
| from difflib import ndiff | ||
|
|
||
| left = _bounded_prefix(left_lines, DIFF_MAX_LINES // 2, NDIFF_MAX_INPUT_SIZE // 2) | ||
| right = _bounded_prefix(right_lines, DIFF_MAX_LINES // 2, NDIFF_MAX_INPUT_SIZE // 2) | ||
| yield ( | ||
| f"Diff too large to show in full (over {NDIFF_MAX_INPUT_SIZE} characters " | ||
| f"or {DIFF_MAX_LINES} lines); showing a truncated diff:" | ||
| ) | ||
| # "right" is the expected base against which we compare "left", | ||
| # see https://github.com/pytest-dev/pytest/issues/3333 | ||
| yield from highlighter( | ||
| "\n".join(line.rstrip("\n") for line in ndiff(right, left)), | ||
| lexer="diff", | ||
| ).splitlines() | ||
|
|
||
|
|
||
| def _bounded_prefix(lines: Sequence[str], max_lines: int, max_chars: int) -> list[str]: | ||
| """Return the longest prefix of ``lines`` within both limits. | ||
|
|
||
| The line that would cross the character limit is included truncated, so a | ||
| single huge line still yields some (bounded) output. | ||
| """ | ||
| kept: list[str] = [] | ||
| chars = 0 | ||
| for line in lines: | ||
| if len(kept) >= max_lines: | ||
| break | ||
| room = max_chars - chars | ||
| if len(line) > room: | ||
| if room > 0: | ||
| kept.append(line[:room]) | ||
| break | ||
| kept.append(line) | ||
| chars += len(line) | ||
| return kept | ||
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
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
Oops, something went wrong.
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.
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.
Message is wrong here, could be either too many line or too many chars.