Commit 8b2f8439 authored by Ilya Tocar's avatar Ilya Tocar

encoding/base32: improve performance in common case

Unroll loop to improve perfromance back to 1.8 level.
name              old time/op    new time/op    delta
EncodeToString-6    63.0µs ± 3%    51.7µs ± 2%  -17.94%  (p=0.000 n=10+10)

name              old speed      new speed      delta
EncodeToString-6   130MB/s ± 3%   159MB/s ± 2%  +21.83%  (p=0.000 n=10+10)

Vs 1.8:
EncodeToString-6    54.9µs ± 2%    51.7µs ± 2%   -5.95%  (p=0.000 n=10+10)

name              old speed      new speed      delta
EncodeToString-6   149MB/s ± 2%   159MB/s ± 2%   +6.32%  (p=0.000 n=10+10)

Fixes #21262

Change-Id: I41bf7e1f61041781386d16d573bffe1a7173c0c3
Reviewed-on: https://go-review.googlesource.com/52510
Run-TryBot: Ilya Tocar <ilya.tocar@intel.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarKeith Randall <khr@golang.org>
parent 1f8433c6
......@@ -130,8 +130,19 @@ func (enc *Encoding) Encode(dst, src []byte) {
}
// Encode 5-bit blocks using the base32 alphabet
for i := 0; i < 8; i++ {
if len(dst) > i {
size := len(dst)
if size >= 8 {
// Common case, unrolled for extra performance
dst[0] = enc.encode[b[0]]
dst[1] = enc.encode[b[1]]
dst[2] = enc.encode[b[2]]
dst[3] = enc.encode[b[3]]
dst[4] = enc.encode[b[4]]
dst[5] = enc.encode[b[5]]
dst[6] = enc.encode[b[6]]
dst[7] = enc.encode[b[7]]
} else {
for i := 0; i < size; i++ {
dst[i] = enc.encode[b[i]]
}
}
......
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