Commit b1eb2d0
committed
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++ {
hd.Decode(src)
}
}
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%1 parent c861375 commit b1eb2d0
1 file changed
+26
-71
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | | - | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
166 | 153 | | |
167 | 154 | | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
175 | 161 | | |
176 | 162 | | |
177 | 163 | | |
178 | 164 | | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
200 | 169 | | |
201 | 170 | | |
202 | | - | |
203 | | - | |
204 | | - | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
205 | 176 | | |
206 | 177 | | |
207 | 178 | | |
| |||
292 | 263 | | |
293 | 264 | | |
294 | 265 | | |
295 | | - | |
296 | | - | |
297 | | - | |
| 266 | + | |
| 267 | + | |
298 | 268 | | |
299 | 269 | | |
300 | 270 | | |
| |||
317 | 287 | | |
318 | 288 | | |
319 | 289 | | |
320 | | - | |
| 290 | + | |
321 | 291 | | |
322 | | - | |
| 292 | + | |
323 | 293 | | |
324 | 294 | | |
325 | 295 | | |
326 | 296 | | |
327 | | - | |
| 297 | + | |
328 | 298 | | |
329 | 299 | | |
330 | | - | |
331 | 300 | | |
332 | 301 | | |
333 | 302 | | |
| |||
346 | 315 | | |
347 | 316 | | |
348 | 317 | | |
349 | | - | |
350 | | - | |
351 | 318 | | |
352 | 319 | | |
353 | 320 | | |
| |||
358 | 325 | | |
359 | 326 | | |
360 | 327 | | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | 328 | | |
374 | 329 | | |
375 | 330 | | |
| |||
763 | 718 | | |
764 | 719 | | |
765 | 720 | | |
766 | | - | |
| 721 | + | |
767 | 722 | | |
768 | 723 | | |
769 | 724 | | |
| |||
0 commit comments