Skip to content

Commit 6889c36

Browse files
committed
sql: reduce allocations in rowContainerIterator
In #111318 a shallow copy of rows was added to `rowContainerIterator.Next`. This copy is only necessary for one usage of the iterator within `plpgsqlCursorHelper`. This commit moves the copy to the cursor helper which reduces allocations for all other usages of the iterator. Informs #117546 Release note: None
1 parent ef369f2 commit 6889c36

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

pkg/sql/buffer_util.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,7 @@ func (i *rowContainerIterator) Next() (tree.Datums, error) {
191191
// All rows have been exhausted.
192192
return nil, nil
193193
}
194-
origRow, err := i.iter.Row()
195-
if err != nil {
196-
return nil, err
197-
}
198-
// Shallow-copy the row to ensure that it is safe to hold on to after Next()
199-
// and Close() calls - see the RowIterator interface.
200-
row := make(tree.Datums, len(origRow))
201-
copy(row, origRow)
202-
return row, nil
194+
return i.iter.Row()
203195
}
204196

205197
func (i *rowContainerIterator) Close() {

pkg/sql/routine.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,16 @@ var _ isql.Rows = &plpgsqlCursorHelper{}
540540

541541
// Next implements the isql.Rows interface.
542542
func (h *plpgsqlCursorHelper) Next(_ context.Context) (bool, error) {
543-
var err error
544-
h.lastRow, err = h.iter.Next()
545-
if err != nil {
543+
row, err := h.iter.Next()
544+
if err != nil || row == nil {
546545
return false, err
547546
}
548-
if h.lastRow != nil {
549-
h.rowsAffected++
550-
}
551-
return h.lastRow != nil, nil
547+
// Shallow-copy the row to ensure that it is safe to hold on to after Next()
548+
// and Close() calls - see the isql.Rows interface.
549+
h.lastRow = make(tree.Datums, len(row))
550+
copy(h.lastRow, row)
551+
h.rowsAffected++
552+
return true, nil
552553
}
553554

554555
// Cur implements the isql.Rows interface.

0 commit comments

Comments
 (0)