Skip to content

Commit

Permalink
sql: avoid net.Error allocation on each readTimeoutConn.Read call
Browse files Browse the repository at this point in the history
This commit updates `readTimeoutConn.Read` to only call `errors.As` if the error
from the Read of the wrapped `net.Conn` was non-nil. This avoids a heap
allocation of a `net.Error` on each call to `readTimeoutConn.Read`, which was
originally introduced in a8ae1bf.

```
name                   old time/op    new time/op    delta
KV/Scan/SQL/rows=1-10    93.9µs ± 6%    94.6µs ± 7%    ~     (p=0.631 n=10+10)

name                   old alloc/op   new alloc/op   delta
KV/Scan/SQL/rows=1-10    20.1kB ± 0%    20.1kB ± 0%    ~     (p=0.197 n=10+10)

name                   old allocs/op  new allocs/op  delta
KV/Scan/SQL/rows=1-10       245 ± 0%       244 ± 0%  -0.41%  (p=0.000 n=10+10)
```
  • Loading branch information
nvanbenschoten committed Dec 30, 2021
1 parent cc22c45 commit 840ac21
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pkg/sql/pgwire/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1759,9 +1759,11 @@ func (c *readTimeoutConn) Read(b []byte) (int, error) {
return 0, err
}
n, err := c.Conn.Read(b)
// Continue if the error is due to timing out.
if ne := (net.Error)(nil); errors.As(err, &ne) && ne.Timeout() {
continue
if err != nil {
// Continue if the error is due to timing out.
if ne := (net.Error)(nil); errors.As(err, &ne) && ne.Timeout() {
continue
}
}
return n, err
}
Expand Down

0 comments on commit 840ac21

Please sign in to comment.