]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/hex: change lookup table from string to array
authorAlberto Donizetti <alb.donizetti@gmail.com>
Wed, 17 Aug 2016 18:34:09 +0000 (20:34 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 17 Aug 2016 23:35:32 +0000 (23:35 +0000)
name            old time/op  new time/op  delta
Encode/256-4     431ns ± 2%   391ns ± 2%   -9.36%  (p=0.000 n=8+8)
Encode/1024-4   1.68µs ± 0%  1.51µs ± 0%   -9.91%  (p=0.001 n=7+7)
Encode/4096-4   6.68µs ± 0%  6.03µs ± 1%   -9.69%  (p=0.000 n=8+8)
Encode/16384-4  27.0µs ± 1%  24.0µs ± 0%  -11.03%  (p=0.000 n=8+7)

Change-Id: I6994e02f77797349c4e188377d84f97dffe98399
Reviewed-on: https://go-review.googlesource.com/27254
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/hex/hex.go
src/encoding/hex/hex_test.go

index 73a25034be12c7167ad85dd1acff5008b19e8978..0211d23a1848d53ae4733af722d5ac6b91ac7117 100644 (file)
@@ -12,7 +12,10 @@ import (
        "io"
 )
 
-const hextable = "0123456789abcdef"
+var hextable = [16]byte{
+       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+       'a', 'b', 'c', 'd', 'e', 'f',
+}
 
 // EncodedLen returns the length of an encoding of n source bytes.
 func EncodedLen(n int) int { return n * 2 }
index b969636cd5e6a330df08f03964f06df2fb1024db..64dabbd10a20af21e4cd681f09d5cb676b6d8c5d 100644 (file)
@@ -6,6 +6,7 @@ package hex
 
 import (
        "bytes"
+       "fmt"
        "testing"
 )
 
@@ -151,3 +152,18 @@ var expectedHexDump = []byte(`00000000  1e 1f 20 21 22 23 24 25  26 27 28 29 2a
 00000010  2e 2f 30 31 32 33 34 35  36 37 38 39 3a 3b 3c 3d  |./0123456789:;<=|
 00000020  3e 3f 40 41 42 43 44 45                           |>?@ABCDE|
 `)
+
+var sink []byte
+
+func BenchmarkEncode(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)
+               sink = make([]byte, 2*size)
+
+               b.Run(fmt.Sprintf("%v", size), func(b *testing.B) {
+                       for i := 0; i < b.N; i++ {
+                               Encode(sink, src)
+                       }
+               })
+       }
+}