1 package zero_test
2 3 import (
4 "testing"
5 6 . "github.com/p9c/p9/pkg/util/zero"
7 )
8 9 var (
10 bytes32 = make([]byte, 32) // typical key size
11 bytes64 = make([]byte, 64) // passphrase hash size
12 bytea32 = new([32]byte)
13 bytea64 = new([64]byte)
14 )
15 16 // xor is the "slow" byte zeroing implementation which this package originally replaced. If this function benchmarks
17 // faster than the functions exported by this package in a future Go version (perhaps by calling runtime.memclr),
18 // replace the "optimized" versions with this.
19 func xor(b []byte) {
20 for i := range b {
21 b[i] ^= b[i]
22 }
23 }
24 25 // zrange is an alternative zero implementation that, while currently slower than the functions provided by this
26 // package, may be faster in a future Go release. Switch to this or the xor implementation if they ever become faster.
27 func zrange(b []byte) {
28 for i := range b {
29 b[i] = 0
30 }
31 }
32 func BenchmarkXor32(b *testing.B) {
33 for i := 0; i < b.N; i++ {
34 xor(bytes32)
35 }
36 }
37 func BenchmarkXor64(b *testing.B) {
38 for i := 0; i < b.N; i++ {
39 xor(bytes64)
40 }
41 }
42 func BenchmarkRange32(b *testing.B) {
43 for i := 0; i < b.N; i++ {
44 zrange(bytes32)
45 }
46 }
47 func BenchmarkRange64(b *testing.B) {
48 for i := 0; i < b.N; i++ {
49 zrange(bytes64)
50 }
51 }
52 func BenchmarkBytes32(b *testing.B) {
53 for i := 0; i < b.N; i++ {
54 Bytes(bytes32)
55 }
56 }
57 func BenchmarkBytes64(b *testing.B) {
58 for i := 0; i < b.N; i++ {
59 Bytes(bytes64)
60 }
61 }
62 func BenchmarkBytea32(b *testing.B) {
63 for i := 0; i < b.N; i++ {
64 Bytea32(bytea32)
65 }
66 }
67 func BenchmarkBytea64(b *testing.B) {
68 for i := 0; i < b.N; i++ {
69 Bytea64(bytea64)
70 }
71 }
72