Skip to content

Commit a457da8

Browse files
committed
Unpin extendedQueryBuilder memory immediately after use
refs #1110
1 parent 851091f commit a457da8

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

conn.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ func (c *Conn) execParams(ctx context.Context, sd *pgconn.StatementDescription,
521521
}
522522

523523
result := c.pgConn.ExecParams(ctx, sd.SQL, c.eqb.paramValues, sd.ParamOIDs, c.eqb.paramFormats, c.eqb.resultFormats).Read()
524+
c.eqb.Reset() // Allow c.eqb internal memory to be GC'ed as soon as possible.
524525
return result.CommandTag, result.Err
525526
}
526527

@@ -531,6 +532,7 @@ func (c *Conn) execPrepared(ctx context.Context, sd *pgconn.StatementDescription
531532
}
532533

533534
result := c.pgConn.ExecPrepared(ctx, sd.Name, c.eqb.paramValues, c.eqb.paramFormats, c.eqb.resultFormats).Read()
535+
c.eqb.Reset() // Allow c.eqb internal memory to be GC'ed as soon as possible.
534536
return result.CommandTag, result.Err
535537
}
536538

@@ -678,6 +680,8 @@ optionLoop:
678680
rows.resultReader = c.pgConn.ExecPrepared(ctx, sd.Name, c.eqb.paramValues, c.eqb.paramFormats, resultFormats)
679681
}
680682

683+
c.eqb.Reset() // Allow c.eqb internal memory to be GC'ed as soon as possible.
684+
681685
return rows, rows.err
682686
}
683687

@@ -825,6 +829,8 @@ func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults {
825829
}
826830
}
827831

832+
c.eqb.Reset() // Allow c.eqb internal memory to be GC'ed as soon as possible.
833+
828834
mrr := c.pgConn.ExecBatch(ctx, batch)
829835

830836
return &batchResults{

extended_query_builder.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ type extendedQueryBuilder struct {
1313
paramValueBytes []byte
1414
paramFormats []int16
1515
resultFormats []int16
16-
17-
resetCount int
1816
}
1917

2018
func (eqb *extendedQueryBuilder) AppendParam(ci *pgtype.ConnInfo, oid uint32, arg interface{}) error {
@@ -34,32 +32,27 @@ func (eqb *extendedQueryBuilder) AppendResultFormat(f int16) {
3432
eqb.resultFormats = append(eqb.resultFormats, f)
3533
}
3634

35+
// Reset readies eqb to build another query.
3736
func (eqb *extendedQueryBuilder) Reset() {
3837
eqb.paramValues = eqb.paramValues[0:0]
3938
eqb.paramValueBytes = eqb.paramValueBytes[0:0]
4039
eqb.paramFormats = eqb.paramFormats[0:0]
4140
eqb.resultFormats = eqb.resultFormats[0:0]
4241

43-
eqb.resetCount++
44-
45-
// Every so often shrink our reserved memory if it is abnormally high
46-
if eqb.resetCount%128 == 0 {
47-
if cap(eqb.paramValues) > 64 {
48-
eqb.paramValues = make([][]byte, 0, cap(eqb.paramValues)/2)
49-
}
50-
51-
if cap(eqb.paramValueBytes) > 256 {
52-
eqb.paramValueBytes = make([]byte, 0, cap(eqb.paramValueBytes)/2)
53-
}
42+
if cap(eqb.paramValues) > 64 {
43+
eqb.paramValues = make([][]byte, 0, 64)
44+
}
5445

55-
if cap(eqb.paramFormats) > 64 {
56-
eqb.paramFormats = make([]int16, 0, cap(eqb.paramFormats)/2)
57-
}
58-
if cap(eqb.resultFormats) > 64 {
59-
eqb.resultFormats = make([]int16, 0, cap(eqb.resultFormats)/2)
60-
}
46+
if cap(eqb.paramValueBytes) > 256 {
47+
eqb.paramValueBytes = make([]byte, 0, 256)
6148
}
6249

50+
if cap(eqb.paramFormats) > 64 {
51+
eqb.paramFormats = make([]int16, 0, 64)
52+
}
53+
if cap(eqb.resultFormats) > 64 {
54+
eqb.resultFormats = make([]int16, 0, 64)
55+
}
6356
}
6457

6558
func (eqb *extendedQueryBuilder) encodeExtendedParamValue(ci *pgtype.ConnInfo, oid uint32, formatCode int16, arg interface{}) ([]byte, error) {

0 commit comments

Comments
 (0)