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 <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/internal/lsp/diff/diff.go b/internal/lsp/diff/diff.go
index f894b86..a789a7f 100644
--- a/internal/lsp/diff/diff.go
+++ b/internal/lsp/diff/diff.go
@@ -174,6 +174,7 @@
 
 	// 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 @@
 
 			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
 }