Skip to content

Commit 6646b42

Browse files
committed
common/bitutil: using subtle.XORBytes for optimization
1 parent 5d51208 commit 6646b42

File tree

1 file changed

+2
-26
lines changed

1 file changed

+2
-26
lines changed

common/bitutil/bitutil.go

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package bitutil
99

1010
import (
11+
"crypto/subtle"
1112
"runtime"
1213
"unsafe"
1314
)
@@ -18,32 +19,7 @@ const supportsUnaligned = runtime.GOARCH == "386" || runtime.GOARCH == "amd64" |
1819
// XORBytes xors the bytes in a and b. The destination is assumed to have enough
1920
// space. Returns the number of bytes xor'd.
2021
func XORBytes(dst, a, b []byte) int {
21-
if supportsUnaligned {
22-
return fastXORBytes(dst, a, b)
23-
}
24-
return safeXORBytes(dst, a, b)
25-
}
26-
27-
// fastXORBytes xors in bulk. It only works on architectures that support
28-
// unaligned read/writes.
29-
func fastXORBytes(dst, a, b []byte) int {
30-
n := len(a)
31-
if len(b) < n {
32-
n = len(b)
33-
}
34-
w := n / wordSize
35-
if w > 0 {
36-
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
37-
aw := *(*[]uintptr)(unsafe.Pointer(&a))
38-
bw := *(*[]uintptr)(unsafe.Pointer(&b))
39-
for i := 0; i < w; i++ {
40-
dw[i] = aw[i] ^ bw[i]
41-
}
42-
}
43-
for i := n - n%wordSize; i < n; i++ {
44-
dst[i] = a[i] ^ b[i]
45-
}
46-
return n
22+
return subtle.XORBytes(dst, a, b)
4723
}
4824

4925
// safeXORBytes xors one by one. It works on all architectures, independent if

0 commit comments

Comments
 (0)