hugelgupf / p9

Idiomatic Go 9P2000.L client and server, extracted from gVisor for general use
Apache License 2.0
87 stars 19 forks source link

p9: remove unnecessary allocations when using sync.Pool #44

Closed fhs closed 4 years ago

fhs commented 4 years ago

This issue was found by staticcheck. Per https://staticcheck.io/docs/checks#SA6002, we must store in the pool a pointer to the slice (instead of storing the slice directly) to avoid extra allocations.

The send/receive benchmark shows we avoid 2 allocations:

name           old time/op    new time/op    delta
SendRecvTCP-4    36.0µs ± 5%    36.4µs ± 2%    ~     (p=0.548 n=5+5)

name           old alloc/op   new alloc/op   delta
SendRecvTCP-4    5.37kB ± 5%    5.38kB ± 1%    ~     (p=0.421 n=5+5)

name           old allocs/op  new allocs/op  delta
SendRecvTCP-4      62.0 ± 0%      60.0 ± 0%  -3.23%  (p=0.008 n=5+5)

Signed-off-by: Fazlul Shahriar fshahriar@gmail.com

hugelgupf commented 4 years ago

Well, I'll be damned. Good catch!

hugelgupf commented 4 years ago

I'm porting your change at https://github.com/google/gvisor/pull/3582 as well.