1
0
mirror of https://github.com/golang/go synced 2024-11-24 05:20:04 -07:00

encoding/base64, encoding/base32: make DecodeString faster

Previously, an input string was stripped of newline
characters at the beginning of DecodeString and then passed
to Decode. Decode again tried to strip newline characters.
That's waste of time.

benchmark                 old MB/s     new MB/s  speedup
BenchmarkDecodeString        38.37        65.20    1.70x

LGTM=dave, bradfitz
R=golang-codereviews, dave, bradfitz
CC=golang-codereviews
https://golang.org/cl/91770051
This commit is contained in:
Rui Ueyama 2014-06-11 11:22:08 -07:00 committed by Brad Fitzpatrick
parent f20e4d5ecb
commit afb7b67ae9
3 changed files with 10 additions and 2 deletions

View File

@ -330,7 +330,7 @@ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
func (enc *Encoding) DecodeString(s string) ([]byte, error) {
s = strings.Map(removeNewlinesMapper, s)
dbuf := make([]byte, enc.DecodedLen(len(s)))
n, err := enc.Decode(dbuf, []byte(s))
n, _, err := enc.decode(dbuf, []byte(s))
return dbuf[:n], err
}

View File

@ -295,7 +295,7 @@ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
func (enc *Encoding) DecodeString(s string) ([]byte, error) {
s = strings.Map(removeNewlinesMapper, s)
dbuf := make([]byte, enc.DecodedLen(len(s)))
n, err := enc.Decode(dbuf, []byte(s))
n, _, err := enc.decode(dbuf, []byte(s))
return dbuf[:n], err
}

View File

@ -342,3 +342,11 @@ func TestDecoderIssue7733(t *testing.T) {
t.Errorf("DecodeString = %q; want abcd", s)
}
}
func BenchmarkDecodeString(b *testing.B) {
data := StdEncoding.EncodeToString(make([]byte, 8192))
b.SetBytes(int64(len(data)))
for i := 0; i < b.N; i++ {
StdEncoding.DecodeString(data)
}
}