Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
126509: sql: avoid an allocation when running subqueries in some cases r=yuzefovich a=yuzefovich

This commit reuses recently added `DatumAlloc` to the planner when creating the tuple that is the result of a subquery evaluation in order to reduce allocations. In a profile obtained from a customer I observed this allocation to be about 2% of all allocated objects.

Epic: None

Release note: None

Co-authored-by: Yahor Yuzefovich <[email protected]>
  • Loading branch information
craig[bot] and yuzefovich committed Jul 2, 2024
2 parents 757ebff + c23ce43 commit fa8149f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
5 changes: 4 additions & 1 deletion pkg/sql/conn_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,10 @@ func (s *Server) newConnExecutor(
execTestingKnobs: s.GetExecutorConfig().TestingKnobs,
},
memMetrics: memMetrics,
planner: planner{execCfg: s.cfg},
planner: planner{
execCfg: s.cfg,
datumAlloc: &tree.DatumAlloc{},
},

// ctxHolder will be reset at the start of run(). We only define
// it here so that an early call to close() doesn't panic.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/distsql_running.go
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,7 @@ func (dsp *DistSQLPlanner) planAndRunSubquery(
// a single value against a tuple.
toAppend = row[0]
} else {
toAppend = &tree.DTuple{D: row}
toAppend = planner.datumAlloc.NewDTuple(tree.DTuple{D: row})
}
// Perform memory accounting for this datum. We do this in an
// incremental fashion since we might be materializing a lot of data
Expand Down
5 changes: 2 additions & 3 deletions pkg/sql/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ type planner struct {
// checkScanParallelizationIfLocal.
parallelizationChecker localScanParallelizationChecker

// datumAlloc is used when decoding datums and is initialized in
// initPlanner.
// datumAlloc is used when decoding datums and running subqueries.
datumAlloc *tree.DatumAlloc
}

Expand Down Expand Up @@ -416,7 +415,7 @@ func newInternalPlanner(
})
plannerMon.StartNoReserved(ctx, execCfg.RootMemoryMonitor)

p := &planner{execCfg: execCfg}
p := &planner{execCfg: execCfg, datumAlloc: &tree.DatumAlloc{}}
p.resetPlanner(ctx, txn, sd, plannerMon, nil /* sessionMon */)

smi := &sessionDataMutatorIterator{
Expand Down

0 comments on commit fa8149f

Please sign in to comment.