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