]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/base64: slight decoding speed-up
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 18 May 2018 17:31:05 +0000 (18:31 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 22 Aug 2018 19:08:03 +0000 (19:08 +0000)
First, use a dummy slice access on decode64 and decode32 to ensure that
there is a single bounds check for src.

Second, move the PutUint64/PutUint32 calls out of these functions,
meaning that they are simpler and smaller. This may also open the door
to inlineability in the future, but for now, they both go past the
budget.

While at it, get rid of the ilen and olen variables, which have no
impact whatsoever on performance. At least, not measurable by any of the
benchmarks.

name                 old time/op    new time/op    delta
DecodeString/2-4       54.3ns ± 1%    55.2ns ± 2%   +1.60%  (p=0.017 n=5+6)
DecodeString/4-4       66.6ns ± 1%    66.8ns ± 2%     ~     (p=0.903 n=6+6)
DecodeString/8-4       79.3ns ± 2%    79.6ns ± 1%     ~     (p=0.448 n=6+6)
DecodeString/64-4       300ns ± 1%     281ns ± 3%   -6.54%  (p=0.002 n=6+6)
DecodeString/8192-4    27.4µs ± 1%    23.7µs ± 2%  -13.47%  (p=0.002 n=6+6)

name                 old speed      new speed      delta
DecodeString/2-4     73.7MB/s ± 1%  72.5MB/s ± 2%   -1.55%  (p=0.026 n=5+6)
DecodeString/4-4      120MB/s ± 1%   120MB/s ± 2%     ~     (p=0.851 n=6+6)
DecodeString/8-4      151MB/s ± 2%   151MB/s ± 1%     ~     (p=0.485 n=6+6)
DecodeString/64-4     292MB/s ± 1%   313MB/s ± 3%   +7.03%  (p=0.002 n=6+6)
DecodeString/8192-4   399MB/s ± 1%   461MB/s ± 2%  +15.58%  (p=0.002 n=6+6)

For #19636.

Change-Id: I0dfbdafa2a41dc4c582f63aef94b90b8e473731c
Reviewed-on: https://go-review.googlesource.com/113776
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/encoding/base64/base64.go

index 9a99370f1e53082f3efbde3b888deadb0e721228..e8afc48859182c437d4571fd5e169da2ea976503 100644 (file)
@@ -465,10 +465,9 @@ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
        }
 
        si := 0
-       ilen := len(src)
-       olen := len(dst)
-       for strconv.IntSize >= 64 && ilen-si >= 8 && olen-n >= 8 {
-               if ok := enc.decode64(dst[n:], src[si:]); ok {
+       for strconv.IntSize >= 64 && len(src)-si >= 8 && len(dst)-n >= 8 {
+               if dn, ok := enc.decode64(src[si:]); ok {
+                       binary.BigEndian.PutUint64(dst[n:], dn)
                        n += 6
                        si += 8
                } else {
@@ -481,8 +480,9 @@ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
                }
        }
 
-       for ilen-si >= 4 && olen-n >= 4 {
-               if ok := enc.decode32(dst[n:], src[si:]); ok {
+       for len(src)-si >= 4 && len(dst)-n >= 4 {
+               if dn, ok := enc.decode32(src[si:]); ok {
+                       binary.BigEndian.PutUint32(dst[n:], dn)
                        n += 3
                        si += 4
                } else {
@@ -506,72 +506,70 @@ func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
        return n, err
 }
 
-// decode32 tries to decode 4 base64 char into 3 bytes.
-// len(dst) and len(src) must both be >= 4.
-// Returns true if decode succeeded.
-func (enc *Encoding) decode32(dst, src []byte) bool {
-       var dn, n uint32
+// decode32 tries to decode 4 base64 characters into 3 bytes, and returns those
+// bytes. len(src) must be >= 4.
+// Returns (0, false) if decoding failed.
+func (enc *Encoding) decode32(src []byte) (dn uint32, ok bool) {
+       var n uint32
+       _ = src[3]
        if n = uint32(enc.decodeMap[src[0]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 26
        if n = uint32(enc.decodeMap[src[1]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 20
        if n = uint32(enc.decodeMap[src[2]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 14
        if n = uint32(enc.decodeMap[src[3]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 8
-
-       binary.BigEndian.PutUint32(dst, dn)
-       return true
+       return dn, true
 }
 
-// decode64 tries to decode 8 base64 char into 6 bytes.
-// len(dst) and len(src) must both be >= 8.
-// Returns true if decode succeeded.
-func (enc *Encoding) decode64(dst, src []byte) bool {
-       var dn, n uint64
+// decode64 tries to decode 8 base64 characters into 6 bytes, and returns those
+// bytes. len(src) must be >= 8.
+// Returns (0, false) if decoding failed.
+func (enc *Encoding) decode64(src []byte) (dn uint64, ok bool) {
+       var n uint64
+       _ = src[7]
        if n = uint64(enc.decodeMap[src[0]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 58
        if n = uint64(enc.decodeMap[src[1]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 52
        if n = uint64(enc.decodeMap[src[2]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 46
        if n = uint64(enc.decodeMap[src[3]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 40
        if n = uint64(enc.decodeMap[src[4]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 34
        if n = uint64(enc.decodeMap[src[5]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 28
        if n = uint64(enc.decodeMap[src[6]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 22
        if n = uint64(enc.decodeMap[src[7]]); n == 0xff {
-               return false
+               return 0, false
        }
        dn |= n << 16
-
-       binary.BigEndian.PutUint64(dst, dn)
-       return true
+       return dn, true
 }
 
 type newlineFilteringReader struct {