From c23ce436e4be29762dff33fd6aecd61d6a6b7698 Mon Sep 17 00:00:00 2001 From: Yahor Yuzefovich Date: Mon, 1 Jul 2024 11:21:07 -0700 Subject: [PATCH] sql: avoid an allocation when running subqueries in some cases 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. Release note: None --- pkg/sql/conn_executor.go | 5 ++++- pkg/sql/distsql_running.go | 2 +- pkg/sql/planner.go | 5 ++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/sql/conn_executor.go b/pkg/sql/conn_executor.go index 3712a6a06f1b..8e07688619fa 100644 --- a/pkg/sql/conn_executor.go +++ b/pkg/sql/conn_executor.go @@ -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. diff --git a/pkg/sql/distsql_running.go b/pkg/sql/distsql_running.go index 07f4a53abfb6..3b86ec4e5107 100644 --- a/pkg/sql/distsql_running.go +++ b/pkg/sql/distsql_running.go @@ -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 diff --git a/pkg/sql/planner.go b/pkg/sql/planner.go index e90f743f7e49..b800b34ac5b2 100644 --- a/pkg/sql/planner.go +++ b/pkg/sql/planner.go @@ -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 } @@ -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{