Commit 24f8919a authored by Rui Ueyama's avatar Rui Ueyama

encoding/base64, encoding/base32: speed up Encode

Avoid unnecessary bitwise-OR operations.

benchmark                      old MB/s     new MB/s  speedup
BenchmarkEncodeToStringBase64  179.02       205.74    1.15x
BenchmarkEncodeToStringBase32  155.86       167.82    1.08x

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/109090043
parent 1ca10de3
......@@ -79,26 +79,26 @@ func (enc *Encoding) Encode(dst, src []byte) {
// destination quantum
switch len(src) {
default:
b7 |= src[4] & 0x1F
b6 |= src[4] >> 5
b7 = src[4] & 0x1F
b6 = src[4] >> 5
fallthrough
case 4:
b6 |= (src[3] << 3) & 0x1F
b5 |= (src[3] >> 2) & 0x1F
b4 |= src[3] >> 7
b5 = (src[3] >> 2) & 0x1F
b4 = src[3] >> 7
fallthrough
case 3:
b4 |= (src[2] << 1) & 0x1F
b3 |= (src[2] >> 4) & 0x1F
b3 = (src[2] >> 4) & 0x1F
fallthrough
case 2:
b3 |= (src[1] << 4) & 0x1F
b2 |= (src[1] >> 1) & 0x1F
b1 |= (src[1] >> 6) & 0x1F
b2 = (src[1] >> 1) & 0x1F
b1 = (src[1] >> 6) & 0x1F
fallthrough
case 1:
b1 |= (src[0] << 2) & 0x1F
b0 |= src[0] >> 3
b0 = src[0] >> 3
}
// Encode 5-bit blocks using the base32 alphabet
......
......@@ -80,16 +80,16 @@ func (enc *Encoding) Encode(dst, src []byte) {
// destination quantum
switch len(src) {
default:
b3 |= src[2] & 0x3F
b2 |= src[2] >> 6
b3 = src[2] & 0x3F
b2 = src[2] >> 6
fallthrough
case 2:
b2 |= (src[1] << 2) & 0x3F
b1 |= src[1] >> 4
b1 = src[1] >> 4
fallthrough
case 1:
b1 |= (src[0] << 4) & 0x3F
b0 |= src[0] >> 2
b0 = src[0] >> 2
}
// Encode 6-bit blocks using the base64 alphabet
......
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