Jelajahi Sumber

Merge pull request #49 from klauspost/faster-overlapping-copies

Faster overlapping copies
Nigel Tao 6 tahun lalu
induk
melakukan
5610373d2f
1 mengubah file dengan 9 tambahan dan 2 penghapusan
  1. 9 2
      decode_other.go

+ 9 - 2
decode_other.go

@@ -90,9 +90,16 @@ func decode(dst, src []byte) int {
 		// forwards, even if the slices overlap. Conceptually, this is:
 		//
 		// d += forwardCopy(dst[d:d+length], dst[d-offset:])
-		for end := d + length; d != end; d++ {
-			dst[d] = dst[d-offset]
+		//
+		// We align the slices into a and b and show the compiler they are the same size.
+		// This allows the loop to run without bounds checks.
+		a := dst[d : d+length]
+		b := dst[d-offset:]
+		b = b[:len(a)]
+		for i := range a {
+			a[i] = b[i]
 		}
+		d += length
 	}
 	if d != len(dst) {
 		return decodeErrCodeCorrupt