From 97de5656fd0abb4da8799a4c7235e596f1ae41e3 Mon Sep 17 00:00:00 2001 From: linguohua Date: Fri, 14 Jun 2019 22:42:14 +0000 Subject: [PATCH] internal/lsp/diff: remove redundant memory allocate and copy operations in function 'shortestEditSequence'. These redundant operations can cause more memory and cpu consumption. Change-Id: I54e0e23a8d1079c7991f55c897c441566c5fb2d8 GitHub-Last-Rev: 13162aa1e88ff272738c6a6675010edd2140c8a4 GitHub-Pull-Request: golang/tools#120 Reviewed-on: https://go-review.googlesource.com/c/tools/+/182478 Reviewed-by: Rebecca Stambler Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot --- internal/lsp/diff/diff.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/lsp/diff/diff.go b/internal/lsp/diff/diff.go index f894b867..a789a7f0 100644 --- a/internal/lsp/diff/diff.go +++ b/internal/lsp/diff/diff.go @@ -174,6 +174,7 @@ func shortestEditSequence(a, b []string) ([][]int, int) { // Iterate through the maximum possible length of the SES (N+M). for d := 0; d <= N+M; d++ { + copyV := make([]int, len(V)) // k lines are represented by the equation y = x - k. We move in // increments of 2 because end points for even d are on even k lines. for k := -d; k <= d; k += 2 { @@ -197,16 +198,19 @@ func shortestEditSequence(a, b []string) ([][]int, int) { V[k+offset] = x - // Save the state of the array. - copyV := make([]int, len(V)) - copy(copyV, V) - trace[d] = copyV - // Return if we've exceeded the maximum values. if x == M && y == N { + // Save the state of the array, and exit function + copy(copyV, V) + trace[d] = copyV + return trace, offset } } + + // Save the state of the array, and continue loop + copy(copyV, V) + trace[d] = copyV } return nil, 0 }