You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remove unnecessary allocations in HopData encode/decode methods
By removing io.Writer and io.Reader abstraction from encode and decode
methods, the following benchmars:
old:
func BenchmarkHopDataEncode(b *testing.B) {
hd := new(HopData)
for i := 0; i < b.N; i++ {
hd.Encode(ioutil.Discard)
}
}
type nopReader struct{}
func (nopReader) Read(b []byte) (int, error) { return len(b), nil }
func BenchmarkHopDataDecode(b *testing.B) {
hd := new(HopData)
src := nopReader{}
for i := 0; i < b.N; i++ {
hd.Decode(src)
}
}
new:
func BenchmarkHopDataEncode(b *testing.B) {
hd := new(HopData)
dst := make([]byte, hopDataSize)
for i := 0; i < b.N; i++ {
hd.Encode(dst)
}
}
func BenchmarkHopDataDecode(b *testing.B) {
hd := new(HopData)
src := make([]byte, hopDataSize)
for i := 0; i < b.N; i++ {
err := hd.Decode(src)
if err != nil {
panic(err)
}
}
}
show the following insrease in performance:
benchmark old ns/op new ns/op delta
BenchmarkHopDataEncode-4 80.7 12.1 -85.01%
benchmark old allocs new allocs delta
BenchmarkHopDataEncode-4 3 0 -100.00%
benchmark old bytes new bytes delta
BenchmarkHopDataEncode-4 24 0 -100.00%
benchmark old ns/op new ns/op delta
BenchmarkHopDataDecode-4 218 9.77 -95.52%
benchmark old allocs new allocs delta
BenchmarkHopDataDecode-4 4 0 -100.00%
benchmark old bytes new bytes delta
BenchmarkHopDataDecode-4 56 0 -100.00%
0 commit comments