diff options
| author | Daniel Martí <mvdan@mvdan.cc> | 2018-11-25 23:22:11 +0000 |
|---|---|---|
| committer | Daniel Martí <mvdan@mvdan.cc> | 2019-03-02 16:48:05 +0000 |
| commit | aa5165d62cf623230dd820afe2bdba92bd15beeb (patch) | |
| tree | 0da71b97d631fb7d5886b88bb8b2833d32954478 /src/encoding/hex/hex.go | |
| parent | 337a1bde026f227fa9536653cc51efa13970657a (diff) | |
| download | go-aa5165d62cf623230dd820afe2bdba92bd15beeb.tar.xz | |
encoding/hex: simplify decoder arithmetic
Remove all multiplications and divisions from the main decoding loop.
name old time/op new time/op delta
Decode/256-8 323ns ± 0% 293ns ± 0% -9.29% (p=0.000 n=5+4)
Decode/1024-8 1.26µs ± 0% 1.14µs ± 0% -9.48% (p=0.000 n=6+5)
Decode/4096-8 4.99µs ± 0% 4.51µs ± 0% -9.55% (p=0.002 n=6+6)
Decode/16384-8 20.0µs ± 0% 18.1µs ± 0% -9.54% (p=0.002 n=6+6)
name old speed new speed delta
Decode/256-8 791MB/s ± 0% 872MB/s ± 0% +10.34% (p=0.002 n=6+6)
Decode/1024-8 814MB/s ± 0% 899MB/s ± 0% +10.48% (p=0.004 n=6+5)
Decode/4096-8 821MB/s ± 0% 908MB/s ± 0% +10.55% (p=0.002 n=6+6)
Decode/16384-8 821MB/s ± 0% 908MB/s ± 0% +10.54% (p=0.002 n=6+6)
Change-Id: Ie9f91242ce04c130a77c1184379e3b9de38fe713
Reviewed-on: https://go-review.googlesource.com/c/151199
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/encoding/hex/hex.go')
| -rw-r--r-- | src/encoding/hex/hex.go | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/encoding/hex/hex.go b/src/encoding/hex/hex.go index 2bb2b57df9..7675de9bd9 100644 --- a/src/encoding/hex/hex.go +++ b/src/encoding/hex/hex.go @@ -55,23 +55,24 @@ func DecodedLen(x int) int { return x / 2 } // If the input is malformed, Decode returns the number // of bytes decoded before the error. func Decode(dst, src []byte) (int, error) { - var i int - for i = 0; i < len(src)/2; i++ { - a, ok := fromHexChar(src[i*2]) + i, j := 0, 1 + for ; j < len(src); j += 2 { + a, ok := fromHexChar(src[j-1]) if !ok { - return i, InvalidByteError(src[i*2]) + return i, InvalidByteError(src[j-1]) } - b, ok := fromHexChar(src[i*2+1]) + b, ok := fromHexChar(src[j]) if !ok { - return i, InvalidByteError(src[i*2+1]) + return i, InvalidByteError(src[j]) } dst[i] = (a << 4) | b + i++ } if len(src)%2 == 1 { // Check for invalid char before reporting bad length, // since the invalid char (if present) is an earlier problem. - if _, ok := fromHexChar(src[i*2]); !ok { - return i, InvalidByteError(src[i*2]) + if _, ok := fromHexChar(src[j-1]); !ok { + return i, InvalidByteError(src[j-1]) } return i, ErrLength } |
