Skip to content

Commit

Permalink
execinfra: reuse output row allocation in the helper
Browse files Browse the repository at this point in the history
This commit makes it so that we reuse the output row in the
ProcOutputHelper if possible. Additionally, it removes a row alloc
object since it is not helpful and only leeds to wasteful allocations.
It also removes a redundant integer for the number of "internal
columns".

Release note: None
  • Loading branch information
yuzefovich committed Sep 28, 2022
1 parent c70b4f9 commit 1328362
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions pkg/sql/execinfra/processorsbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ type DoesNotUseTxn interface {
// ProcOutputHelper is a helper type that performs filtering and projection on
// the output of a processor.
type ProcOutputHelper struct {
numInternalCols int
RowAlloc rowenc.EncDatumRowAlloc
// renderExprs has length > 0 if we have a rendering. Only one of renderExprs
// and outputCols can be set.
renderExprs []execinfrapb.ExprHelper
Expand Down Expand Up @@ -97,13 +95,18 @@ type ProcOutputHelper struct {

// Reset resets this ProcOutputHelper, retaining allocated memory in its slices.
func (h *ProcOutputHelper) Reset() {
// Deeply reset the render expressions. Note that we don't bother deeply
// resetting the types slice since the types are small objects.
// Deeply reset the render expressions and the output row. Note that we
// don't bother deeply resetting the types slice since the types are small
// objects.
for i := range h.renderExprs {
h.renderExprs[i] = execinfrapb.ExprHelper{}
}
for i := range h.outputRow {
h.outputRow[i] = rowenc.EncDatum{}
}
*h = ProcOutputHelper{
renderExprs: h.renderExprs[:0],
outputRow: h.outputRow[:0],
OutputTypes: h.OutputTypes[:0],
}
}
Expand All @@ -125,11 +128,10 @@ func (h *ProcOutputHelper) Init(
if post.Projection && len(post.RenderExprs) > 0 {
return errors.Errorf("post-processing has both projection and rendering: %s", post)
}
h.numInternalCols = len(coreOutputTypes)
if post.Projection {
for _, col := range post.OutputColumns {
if int(col) >= h.numInternalCols {
return errors.Errorf("invalid output column %d (only %d available)", col, h.numInternalCols)
if int(col) >= len(coreOutputTypes) {
return errors.Errorf("invalid output column %d (only %d available)", col, len(coreOutputTypes))
}
}
h.outputCols = post.OutputColumns
Expand Down Expand Up @@ -174,7 +176,14 @@ func (h *ProcOutputHelper) Init(
}
if h.outputCols != nil || len(h.renderExprs) > 0 {
// We're rendering or projecting, so allocate an output row.
h.outputRow = h.RowAlloc.AllocRow(len(h.OutputTypes))
if h.outputRow != nil && cap(h.outputRow) >= len(h.OutputTypes) {
// In some cases we might have no output columns, so nil outputRow
// would have sufficient width, yet nil row is a special value, so
// we can only reuse the old outputRow if it's non-nil.
h.outputRow = h.outputRow[:len(h.OutputTypes)]
} else {
h.outputRow = make(rowenc.EncDatumRow, len(h.OutputTypes))
}
}

h.offset = post.Offset
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/rowexec/distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func newDistinct(
}); err != nil {
return nil, err
}
d.lastGroupKey = d.OutputHelper.RowAlloc.AllocRow(len(d.types))
d.lastGroupKey = make(rowenc.EncDatumRow, len(d.types))
d.haveLastGroupKey = false
// If we set up the arena when d is created, the pointer to the memAcc
// will be changed because the sortedDistinct case makes a copy of d.
Expand Down

0 comments on commit 1328362

Please sign in to comment.