Preallocate poolRows

pull/483/head
Jack Christensen 2019-05-04 15:52:59 -05:00
parent d7fdbf1b49
commit 3c7cd51a17
1 changed files with 22 additions and 1 deletions

View File

@ -31,6 +31,9 @@ type Pool struct {
preallocatedPoolRowsMux sync.Mutex
preallocatedPoolRows []poolRow
preallocatedPoolRowssMux sync.Mutex
preallocatedPoolRowss []poolRows
}
// Config is the configuration struct for creating a pool. It is highly recommended to modify a Config returned by
@ -255,6 +258,24 @@ func (p *Pool) getPoolRow(c *Conn, r pgx.Row) *poolRow {
return pr
}
func (p *Pool) getPoolRows(c *Conn, r pgx.Rows) *poolRows {
p.preallocatedPoolRowssMux.Lock()
if len(p.preallocatedPoolRowss) == 0 {
p.preallocatedPoolRowss = make([]poolRows, 128)
}
pr := &p.preallocatedPoolRowss[len(p.preallocatedPoolRowss)-1]
p.preallocatedPoolRowss = p.preallocatedPoolRowss[0 : len(p.preallocatedPoolRowss)-1]
p.preallocatedPoolRowssMux.Unlock()
pr.c = c
pr.r = r
return pr
}
func (p *Pool) Acquire(ctx context.Context) (*Conn, error) {
for {
res, err := p.p.Acquire(ctx)
@ -312,7 +333,7 @@ func (p *Pool) Query(ctx context.Context, sql string, args ...interface{}) (pgx.
return errRows{err: err}, err
}
return &poolRows{r: rows, c: c}, nil
return p.getPoolRows(c, rows), nil
}
func (p *Pool) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {