mirror of https://github.com/jackc/pgx.git
Only create RawConn.Write callback once
This saves an allocation on every call. https://github.com/jackc/pgx/issues/1481pull/1490/head
parent
42a47194a2
commit
2c7d86a543
|
@ -74,6 +74,7 @@ type NetConn struct {
|
||||||
readFlushLock sync.Mutex
|
readFlushLock sync.Mutex
|
||||||
// non-blocking writes with syscall.RawConn are done with a callback function. By using these fields instead of the
|
// non-blocking writes with syscall.RawConn are done with a callback function. By using these fields instead of the
|
||||||
// callback functions closure to pass the buf argument and receive the n and err results we avoid some allocations.
|
// callback functions closure to pass the buf argument and receive the n and err results we avoid some allocations.
|
||||||
|
nonblockWriteFunc func(fd uintptr) (done bool)
|
||||||
nonblockWriteBuf []byte
|
nonblockWriteBuf []byte
|
||||||
nonblockWriteErr error
|
nonblockWriteErr error
|
||||||
nonblockWriteN int
|
nonblockWriteN int
|
||||||
|
|
|
@ -12,13 +12,17 @@ import (
|
||||||
|
|
||||||
// realNonblockingWrite does a non-blocking write. readFlushLock must already be held.
|
// realNonblockingWrite does a non-blocking write. readFlushLock must already be held.
|
||||||
func (c *NetConn) realNonblockingWrite(b []byte) (n int, err error) {
|
func (c *NetConn) realNonblockingWrite(b []byte) (n int, err error) {
|
||||||
|
if c.nonblockWriteFunc == nil {
|
||||||
|
c.nonblockWriteFunc = func(fd uintptr) (done bool) {
|
||||||
|
c.nonblockWriteN, c.nonblockWriteErr = syscall.Write(int(fd), c.nonblockWriteBuf)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
c.nonblockWriteBuf = b
|
c.nonblockWriteBuf = b
|
||||||
c.nonblockWriteN = 0
|
c.nonblockWriteN = 0
|
||||||
c.nonblockWriteErr = nil
|
c.nonblockWriteErr = nil
|
||||||
err = c.rawConn.Write(func(fd uintptr) (done bool) {
|
|
||||||
c.nonblockWriteN, c.nonblockWriteErr = syscall.Write(int(fd), c.nonblockWriteBuf)
|
err = c.rawConn.Write(c.nonblockWriteFunc)
|
||||||
return true
|
|
||||||
})
|
|
||||||
n = c.nonblockWriteN
|
n = c.nonblockWriteN
|
||||||
if err == nil && c.nonblockWriteErr != nil {
|
if err == nil && c.nonblockWriteErr != nil {
|
||||||
if errors.Is(c.nonblockWriteErr, syscall.EWOULDBLOCK) {
|
if errors.Is(c.nonblockWriteErr, syscall.EWOULDBLOCK) {
|
||||||
|
|
Loading…
Reference in New Issue