Switch from string to []byte slices

The way I'm using the base58 required me to do a lot conversions between
[]byte slices and strings. Not anymore.

Also switching to []byte slices allowed some nice performance
improvements.

Before, with strings:

    BenchmarkEncode-4        3000000               511 ns/op
    BenchmarkDecode-4        5000000               300 ns/op

After, with []byte slices:

    BenchmarkEncode-4        5000000               256 ns/op
    BenchmarkDecode-4       20000000               107 ns/op
This commit is contained in:
2016-07-03 11:14:38 +01:00
parent 785190bf71
commit f8ea86748a
2 changed files with 28 additions and 16 deletions

View File

@@ -186,26 +186,28 @@ type Base58Suite struct {
// Tests
func (s *Base58Suite) TestAlphabet() {
expected := "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"
s.Equal(expected, Alphabet)
s.Equal(
[]byte("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"),
Alphabet,
)
}
func (s *Base58Suite) TestEncode() {
for str, num := range examples {
result := Encode(num)
s.Equal(str, result)
s.Equal([]byte(str), result)
}
}
func (s *Base58Suite) TestDecode() {
for str, num := range examples {
result, _ := Decode(str)
result, _ := Decode([]byte(str))
s.Equal(num, result)
}
}
func (s *Base58Suite) TestDecodeError() {
result, err := Decode("invalid@base58.string")
result, err := Decode([]byte("invalid@base58.string"))
s.Equal(-1, result)
s.Equal("invalid base58", err.Error())
@@ -227,6 +229,6 @@ func BenchmarkEncode(b *testing.B) {
func BenchmarkDecode(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = Decode("6hKMCS")
_, _ = Decode([]byte("6hKMCS"))
}
}