Commit b768d82d authored by Niek Sanders's avatar Niek Sanders Committed by Brad Fitzpatrick

encoding/base32: eliminate alphabet bounds check

name              old time/op   new time/op   delta
EncodeToString-4   35.5µs ± 7%   33.3µs ± 6%  -6.27%   (p=0.008 n=10+9)
DecodeString-4      120µs ± 7%    113µs ± 8%  -5.88%  (p=0.011 n=10+10)

name              old speed     new speed     delta
EncodeToString-4  231MB/s ± 8%  247MB/s ± 5%  +6.55%   (p=0.008 n=10+9)
DecodeString-4    109MB/s ± 7%  116MB/s ± 8%  +6.27%  (p=0.011 n=10+10)

Change-Id: I60bf962464179e35b1711617adbc45a822eaece5
Reviewed-on: https://go-review.googlesource.com/45876Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 3885e864
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
// introduced for SASL GSSAPI and standardized in RFC 4648. // introduced for SASL GSSAPI and standardized in RFC 4648.
// The alternate "base32hex" encoding is used in DNSSEC. // The alternate "base32hex" encoding is used in DNSSEC.
type Encoding struct { type Encoding struct {
encode string encode [32]byte
decodeMap [256]byte decodeMap [256]byte
padChar rune padChar rune
} }
...@@ -37,8 +37,12 @@ const encodeHex = "0123456789ABCDEFGHIJKLMNOPQRSTUV" ...@@ -37,8 +37,12 @@ const encodeHex = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
// NewEncoding returns a new Encoding defined by the given alphabet, // NewEncoding returns a new Encoding defined by the given alphabet,
// which must be a 32-byte string. // which must be a 32-byte string.
func NewEncoding(encoder string) *Encoding { func NewEncoding(encoder string) *Encoding {
if len(encoder) != 32 {
panic("encoding alphabet is not 32-bytes long")
}
e := new(Encoding) e := new(Encoding)
e.encode = encoder copy(e.encode[:], encoder)
e.padChar = StdPadding e.padChar = StdPadding
for i := 0; i < len(e.decodeMap); i++ { for i := 0; i < len(e.decodeMap); i++ {
...@@ -129,17 +133,17 @@ func (enc *Encoding) Encode(dst, src []byte) { ...@@ -129,17 +133,17 @@ func (enc *Encoding) Encode(dst, src []byte) {
size := len(dst) size := len(dst)
if size >= 8 { if size >= 8 {
// Common case, unrolled for extra performance // Common case, unrolled for extra performance
dst[0] = enc.encode[b[0]] dst[0] = enc.encode[b[0]&31]
dst[1] = enc.encode[b[1]] dst[1] = enc.encode[b[1]&31]
dst[2] = enc.encode[b[2]] dst[2] = enc.encode[b[2]&31]
dst[3] = enc.encode[b[3]] dst[3] = enc.encode[b[3]&31]
dst[4] = enc.encode[b[4]] dst[4] = enc.encode[b[4]&31]
dst[5] = enc.encode[b[5]] dst[5] = enc.encode[b[5]&31]
dst[6] = enc.encode[b[6]] dst[6] = enc.encode[b[6]&31]
dst[7] = enc.encode[b[7]] dst[7] = enc.encode[b[7]&31]
} else { } else {
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
dst[i] = enc.encode[b[i]] dst[i] = enc.encode[b[i]&31]
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment