lbcwallet/internal/zero/benchmark_test.go
Josh Rickmar 4d9c43593d Consolidate and optimize zero functions.
This introduce a new internal package to deal with the explicit
clearing of data (such as private keys) in byte slices, byte arrays
(32 and 64-bytes long), and multi-precision "big" integers.

Benchmarks from a xeon e3 (Xor is the zeroing funcion which Bytes
replaces):

BenchmarkXor32  30000000                52.1 ns/op
BenchmarkXor64  20000000                91.5 ns/op
BenchmarkRange32        50000000                31.8 ns/op
BenchmarkRange64        30000000                49.5 ns/op
BenchmarkBytes32        200000000               10.1 ns/op
BenchmarkBytes64        100000000               15.4 ns/op
BenchmarkBytea32        1000000000               2.24 ns/op
BenchmarkBytea64        300000000                4.46 ns/op

Removes an XXX from the votingpool package.
2015-03-05 21:32:33 -05:00

83 lines
1.6 KiB
Go

package zero_test
import (
"testing"
. "github.com/btcsuite/btcwallet/internal/zero"
)
var (
bytes32 = make([]byte, 32) // typical key size
bytes64 = make([]byte, 64) // passphrase hash size
bytea32 = new([32]byte)
bytea64 = new([64]byte)
)
// xor is the "slow" byte zeroing implementation which this package
// originally replaced. If this function benchmarks faster than the
// functions exported by this package in a future Go version (perhaps
// by calling runtime.memclr), replace the "optimized" versions with
// this.
func xor(b []byte) {
for i := range b {
b[i] ^= b[i]
}
}
// zrange is an alternative zero implementation that, while currently
// slower than the functions provided by this package, may be faster
// in a future Go release. Switch to this or the xor implementation
// if they ever become faster.
func zrange(b []byte) {
for i := range b {
b[i] = 0
}
}
func BenchmarkXor32(b *testing.B) {
for i := 0; i < b.N; i++ {
xor(bytes32)
}
}
func BenchmarkXor64(b *testing.B) {
for i := 0; i < b.N; i++ {
xor(bytes64)
}
}
func BenchmarkRange32(b *testing.B) {
for i := 0; i < b.N; i++ {
zrange(bytes32)
}
}
func BenchmarkRange64(b *testing.B) {
for i := 0; i < b.N; i++ {
zrange(bytes64)
}
}
func BenchmarkBytes32(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytes(bytes32)
}
}
func BenchmarkBytes64(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytes(bytes64)
}
}
func BenchmarkBytea32(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytea32(bytea32)
}
}
func BenchmarkBytea64(b *testing.B) {
for i := 0; i < b.N; i++ {
Bytea64(bytea64)
}
}