From 3dc0d500ef5d983e4b132cb4b44b2780855dbfa5 Mon Sep 17 00:00:00 2001 From: Vadim Markovtsev Date: Sun, 10 Mar 2019 08:50:40 +0100 Subject: Increase diffmatchcpatch timeout Fixes https://github.com/src-d/go-git/issues/1083 Signed-off-by: Vadim Markovtsev --- utils/diff/diff.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/diff/diff.go b/utils/diff/diff.go index f49ae55..8539230 100644 --- a/utils/diff/diff.go +++ b/utils/diff/diff.go @@ -8,6 +8,7 @@ package diff import ( "bytes" + "time" "github.com/sergi/go-diff/diffmatchpatch" ) @@ -16,6 +17,7 @@ import ( // string into the dst string. func Do(src, dst string) (diffs []diffmatchpatch.Diff) { dmp := diffmatchpatch.New() + dmp.DiffTimeout = time.Hour // the default is time.Second which may be too little under heavy load wSrc, wDst, warray := dmp.DiffLinesToRunes(src, dst) diffs = dmp.DiffMainRunes(wSrc, wDst, false) diffs = dmp.DiffCharsToLines(diffs, warray) -- cgit From a80a9e54409de2b17e4ca2e5a04bf4f0ac4b6fed Mon Sep 17 00:00:00 2001 From: Vadim Markovtsev Date: Wed, 13 Mar 2019 16:35:59 +0100 Subject: Add diff.DoWithTimeout() Signed-off-by: Vadim Markovtsev --- utils/diff/diff.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/utils/diff/diff.go b/utils/diff/diff.go index 8539230..6142ed0 100644 --- a/utils/diff/diff.go +++ b/utils/diff/diff.go @@ -14,10 +14,24 @@ import ( ) // Do computes the (line oriented) modifications needed to turn the src -// string into the dst string. +// string into the dst string. The underlying algorithm is Meyers, +// its complexity is O(N*d) where N is min(lines(src), lines(dst)) and d +// is the size of the diff. func Do(src, dst string) (diffs []diffmatchpatch.Diff) { + // the default timeout is time.Second which may be too small under heavy load + return DoWithTimeout(src, dst, time.Hour) +} + +// DoWithTimeout computes the (line oriented) modifications needed to turn the src +// string into the dst string. The `timeout` argument specifies the maximum +// amount of time it is allowed to spend in this function. If the timeout +// is exceeded, the parts of the strings which were not considered are turned into +// a bulk delete+insert and the half-baked suboptimal result is returned at once. +// The underlying algorithm is Meyers, its complexity is O(N*d) where N is +// min(lines(src), lines(dst)) and d is the size of the diff. +func DoWithTimeout (src, dst string, timeout time.Duration) (diffs []diffmatchpatch.Diff) { dmp := diffmatchpatch.New() - dmp.DiffTimeout = time.Hour // the default is time.Second which may be too little under heavy load + dmp.DiffTimeout = timeout wSrc, wDst, warray := dmp.DiffLinesToRunes(src, dst) diffs = dmp.DiffMainRunes(wSrc, wDst, false) diffs = dmp.DiffCharsToLines(diffs, warray) -- cgit