Preallocate poolRows
This commit is contained in:
+22
-1
@@ -31,6 +31,9 @@ type Pool struct {
|
|||||||
|
|
||||||
preallocatedPoolRowsMux sync.Mutex
|
preallocatedPoolRowsMux sync.Mutex
|
||||||
preallocatedPoolRows []poolRow
|
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
|
// 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
|
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) {
|
func (p *Pool) Acquire(ctx context.Context) (*Conn, error) {
|
||||||
for {
|
for {
|
||||||
res, err := p.p.Acquire(ctx)
|
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 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 {
|
func (p *Pool) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
|
||||||
|
|||||||
Reference in New Issue
Block a user