From aa5165d62cf623230dd820afe2bdba92bd15beeb Mon Sep 17 00:00:00 2001 From: Daniel Martí Date: Sun, 25 Nov 2018 23:22:11 +0000 Subject: encoding/hex: simplify decoder arithmetic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/encoding/hex/hex_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/encoding/hex/hex_test.go') diff --git a/src/encoding/hex/hex_test.go b/src/encoding/hex/hex_test.go index e9f4b3a53a..ba703cf1c1 100644 --- a/src/encoding/hex/hex_test.go +++ b/src/encoding/hex/hex_test.go @@ -249,6 +249,20 @@ func BenchmarkEncode(b *testing.B) { } } +func BenchmarkDecode(b *testing.B) { + for _, size := range []int{256, 1024, 4096, 16384} { + src := bytes.Repeat([]byte{'2', 'b', '7', '4', '4', 'f', 'a', 'a'}, size/8) + sink = make([]byte, size/2) + + b.Run(fmt.Sprintf("%v", size), func(b *testing.B) { + b.SetBytes(int64(size)) + for i := 0; i < b.N; i++ { + Decode(sink, src) + } + }) + } +} + func BenchmarkDump(b *testing.B) { for _, size := range []int{256, 1024, 4096, 16384} { src := bytes.Repeat([]byte{2, 3, 5, 7, 9, 11, 13, 17}, size/8) -- cgit v1.3-5-g9baa