-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(encoding): speed up EncodeVarint with io.ByteWriter+hand rolled …
- Loading branch information
1 parent
ca1d22d
commit b4fcc41
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package encoding | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
"math" | ||
"testing" | ||
) | ||
|
||
var encValues = []int64{ | ||
-1, -100, -1 << 32, | ||
0, 1, 100, 1 << 32, | ||
-1 << 52, 1 << 52, 17, | ||
19, 28, 37, 388888888, | ||
-99999999999, 99999999999, | ||
math.MaxInt64, math.MinInt64, | ||
} | ||
|
||
// This tests that the results from directly invoking binary.PutVarint match | ||
// exactly those that we get from invoking EncodeVarint and its internals. | ||
func TestEncodeVarintParity(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
var board [binary.MaxVarintLen64]byte | ||
|
||
for _, val := range encValues { | ||
val := val | ||
name := fmt.Sprintf("%d", val) | ||
|
||
buf.Reset() | ||
t.Run(name, func(t *testing.T) { | ||
if err := EncodeVarint(buf, val); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
n := binary.PutVarint(board[:], val) | ||
got := buf.Bytes() | ||
want := board[:n] | ||
if !bytes.Equal(got, want) { | ||
t.Fatalf("Result mismatch\n\tGot: %d\n\tWant: %d", got, want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkEncodeVarint(b *testing.B) { | ||
buf := new(bytes.Buffer) | ||
b.ReportAllocs() | ||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
for _, val := range encValues { | ||
if err := EncodeVarint(buf, val); err != nil { | ||
b.Fatal(err) | ||
} | ||
buf.Reset() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters