transport: skip the backlog if it's empty

This provides a modest reduction in allocation count.

Benchmarked using https://github.com/cockroachdb/rpc-bench:

```
name                 old time/op    new time/op    delta
GRPCServeHTTP_1K-4      181µs ± 3%     178µs ± 2%    ~     (p=0.310 n=5+5)
GRPCServeHTTP_64K-4    1.31ms ± 4%    1.30ms ± 3%    ~     (p=0.548 n=5+5)

name                 old speed      new speed      delta
GRPCServeHTTP_1K-4   11.3MB/s ± 3%  11.5MB/s ± 2%    ~     (p=0.246 n=5+5)
GRPCServeHTTP_64K-4   100MB/s ± 4%   100MB/s ± 2%    ~     (p=0.548 n=5+5)

name                 old alloc/op   new alloc/op   delta
GRPCServeHTTP_1K-4     88.5kB ± 0%    88.5kB ± 0%  -0.09%  (p=0.008 n=5+5)
GRPCServeHTTP_64K-4     801kB ± 0%     801kB ± 0%  -0.05%  (p=0.008 n=5+5)

name                 old allocs/op  new allocs/op  delta
GRPCServeHTTP_1K-4        167 ± 0%       162 ± 0%  -2.99%  (p=0.008 n=5+5)
GRPCServeHTTP_64K-4       672 ± 0%       645 ± 0%  -4.08%  (p=0.008 n=5+5)
```
This commit is contained in:
Tamir Duberstein
2016-03-06 16:35:07 -05:00
parent 9e3a674ceb
commit 54ac7579a7

View File

@ -89,12 +89,14 @@ func newRecvBuffer() *recvBuffer {
func (b *recvBuffer) put(r item) {
b.mu.Lock()
defer b.mu.Unlock()
b.backlog = append(b.backlog, r)
select {
case b.c <- b.backlog[0]:
b.backlog = b.backlog[1:]
default:
if len(b.backlog) == 0 {
select {
case b.c <- r:
return
default:
}
}
b.backlog = append(b.backlog, r)
}
func (b *recvBuffer) load() {