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

diff: add package, make testscript use it #205

Merged
merged 2 commits into from
Mar 22, 2023

Conversation

mvdan
Copy link
Collaborator

@mvdan mvdan commented Mar 22, 2023

(see commit messages - please do not squash)

Fixes #157.

From Go tip as of March 21st 2023,
at commit 5f1a0320b92a60ee1283522135e00bff540ea115.

The only change is to replace the internal/txtar dependency
with our own txtar package.
It seems like upstream has its own tiny copy of x/tools/txtar,
presumably so that even low level packages can use txtar in tests.

Fixes rogpeppe#157.
The main reason to prefer a copy of Go's internal/diff over pkg/diff is
that internal/diff is much more efficient in both time and memory usage.
In particular, pkg/diff required quadratic space in memory,
which could easily cause "out of memory" errors in Go tests
per pkg/diff#26.

Beyond making the `cmp` command better able to handle large files,
this also moves us back to having zero external dependencies,
which is always a nice to have.

The long_diff test still appears to work well;
the output is changed since the new package produces a shorter,
but still entirely correct, diff.

It also seems like the new package includes a leading "diff" line to
show the two filenames. That seems like a harmless change.
@mvdan mvdan requested review from rogpeppe and myitcv March 22, 2023 17:16
Copy link
Collaborator

@myitcv myitcv left a comment

Choose a reason for hiding this comment

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

LGTM - but would also ask @rogpeppe to confirm the change in package "API" surface.

@mvdan
Copy link
Collaborator Author

mvdan commented Mar 22, 2023

CC @thepudds

@mvdan
Copy link
Collaborator Author

mvdan commented Mar 22, 2023

Roger agreed in #157 (comment), and again in voice, so I'm merging :)

@mvdan mvdan merged commit 50a1440 into rogpeppe:master Mar 22, 2023
@thepudds
Copy link
Contributor

Hi @mvdan , looks great, and thanks! (Sorry, this was still on my to do list, but not my "Burning Must Do Now" list 😅 ).

Would it make sense to remove or modify as well (currently still there on master):

// pkg/diff is quadratic at the moment.
// If the product of the number of lines in the inputs is too large,
// don't call pkg.Diff at all as it might take tons of memory or time.
// We found one million to be reasonable for an average laptop.
const maxLineDiff = 1_000_000
if strings.Count(text1, "\n")*strings.Count(text2, "\n") > maxLineDiff {
ts.Fatalf("large files %s and %s differ", name1, name2)
return
}

@mvdan
Copy link
Collaborator Author

mvdan commented Mar 22, 2023

Oh my, how did I miss that? Happy to approve and merge a PR if you send one, otherwise I'll send one in a bit and you can approve.

@mvdan mvdan deleted the diff-testscript branch March 27, 2023 09:36
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.

diff: add new diff package based on Go 1.19 patience diff implementation
3 participants