Skip to content

Commit 8329eeb

Browse files
committed
testscript: workaround quardratic behaviour of github.com/pkg/diff
The current version of github.com/pkg/diff.Diff has quadratic behaviour. This means that when we attempt a diff between relatively modest sized files, it's easy to find yourself out of memory. Therefore, when we see a diff between two large files (large defined in terms of number of lines), tersely report that as if the two were binary files, i.e. do not try to render the diff. When github.com/pkg/diff or similar supports a linear algorithm: golang/go#45200 (comment) we can revert this change.
1 parent 8ef1273 commit 8329eeb

File tree

3 files changed

+2081
-1
lines changed

3 files changed

+2081
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/rogpeppe/go-internal
22

3-
go 1.11
3+
go 1.15
44

55
require (
66
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e

testscript/cmd.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ func (ts *TestScript) doCmdCmp(args []string, env bool) {
141141
// update the script.
142142
}
143143

144+
// pkg/diff is quadratic at the moment.
145+
// If the product of the number of lines in the inputs is too large,
146+
// don't call pkg.Diff at all as it might take tons of memory or time.
147+
// We found one million to be reasonable for an average laptop.
148+
const maxLineDiff = 1_000_000
149+
if strings.Count(text1, "\n")*strings.Count(text2, "\n") > maxLineDiff {
150+
ts.Fatalf("large files %s and %s differ", name1, name2)
151+
return
152+
}
153+
144154
var sb strings.Builder
145155
if err := diff.Text(name1, name2, text1, text2, &sb); err != nil {
146156
ts.Check(err)

0 commit comments

Comments
 (0)