mirror of https://github.com/jackc/pgx.git
Reduce allocations for row objects
parent
acd9c01bd6
commit
e5ec5851e5
1
conn.go
1
conn.go
|
@ -66,6 +66,7 @@ type Conn struct {
|
||||||
pgsql_af_inet6 byte
|
pgsql_af_inet6 byte
|
||||||
busy bool
|
busy bool
|
||||||
poolResetCount int
|
poolResetCount int
|
||||||
|
preallocatedRows []Rows
|
||||||
}
|
}
|
||||||
|
|
||||||
// PreparedStatement is a description of a prepared statement
|
// PreparedStatement is a description of a prepared statement
|
||||||
|
|
22
query.go
22
query.go
|
@ -428,7 +428,8 @@ func (rows *Rows) AfterClose(f func(*Rows)) {
|
||||||
// from Query and handle it in *Rows.
|
// from Query and handle it in *Rows.
|
||||||
func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error) {
|
func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error) {
|
||||||
c.lastActivityTime = time.Now()
|
c.lastActivityTime = time.Now()
|
||||||
rows := &Rows{conn: c, startTime: c.lastActivityTime, sql: sql, args: args, log: c.log, shouldLog: c.shouldLog}
|
|
||||||
|
rows := c.getRows(sql, args)
|
||||||
|
|
||||||
if err := c.lock(); err != nil {
|
if err := c.lock(); err != nil {
|
||||||
rows.abort(err)
|
rows.abort(err)
|
||||||
|
@ -454,6 +455,25 @@ func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error) {
|
||||||
return rows, rows.err
|
return rows, rows.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Conn) getRows(sql string, args []interface{}) *Rows {
|
||||||
|
if len(c.preallocatedRows) == 0 {
|
||||||
|
c.preallocatedRows = make([]Rows, 64)
|
||||||
|
return c.getRows(sql, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
r := &c.preallocatedRows[len(c.preallocatedRows)-1]
|
||||||
|
c.preallocatedRows = c.preallocatedRows[0 : len(c.preallocatedRows)-1]
|
||||||
|
|
||||||
|
r.conn = c
|
||||||
|
r.startTime = c.lastActivityTime
|
||||||
|
r.sql = sql
|
||||||
|
r.args = args
|
||||||
|
r.log = c.log
|
||||||
|
r.shouldLog = c.shouldLog
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
// QueryRow is a convenience wrapper over Query. Any error that occurs while
|
// QueryRow is a convenience wrapper over Query. Any error that occurs while
|
||||||
// querying is deferred until calling Scan on the returned *Row. That *Row will
|
// querying is deferred until calling Scan on the returned *Row. That *Row will
|
||||||
// error with ErrNoRows if no rows are returned.
|
// error with ErrNoRows if no rows are returned.
|
||||||
|
|
Loading…
Reference in New Issue