Explorar o código

Merge pull request #48 from klauspost/use-copy-for-non-overlapping

Use faster copy when not overlapping
Nigel Tao %!s(int64=6) %!d(string=hai) anos
pai
achega
c9879f99e6
Modificáronse 1 ficheiros con 9 adicións e 2 borrados
  1. 9 2
      decode_other.go

+ 9 - 2
decode_other.go

@@ -85,8 +85,15 @@ func decode(dst, src []byte) int {
 		if offset <= 0 || d < offset || length > len(dst)-d {
 			return decodeErrCodeCorrupt
 		}
-		// Copy from an earlier sub-slice of dst to a later sub-slice. Unlike
-		// the built-in copy function, this byte-by-byte copy always runs
+		// Copy from an earlier sub-slice of dst to a later sub-slice.
+		// If no overlap, use the built-in copy:
+		if offset >= length {
+			copy(dst[d:d+length], dst[d-offset:])
+			d += length
+			continue
+		}
+
+		// Unlike the built-in copy function, this byte-by-byte copy always runs
 		// forwards, even if the slices overlap. Conceptually, this is:
 		//
 		// d += forwardCopy(dst[d:d+length], dst[d-offset:])