Skip to content

Commit

Permalink
*: reduce NewRuntimeStatsColl() object allocation (#26164)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Jul 13, 2021
1 parent a3919e3 commit 9abf9d0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion distsql/distsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (s *testSuite) TestSelectResultRuntimeStats(c *C) {
rpcStat: tikv.NewRegionRequestRuntimeStats(),
}
s2 := *s1
stmtStats := execdetails.NewRuntimeStatsColl()
stmtStats := execdetails.NewRuntimeStatsColl(nil)
stmtStats.RegisterStats(1, basic)
stmtStats.RegisterStats(1, s1)
stmtStats.RegisterStats(1, &s2)
Expand Down
2 changes: 1 addition & 1 deletion distsql/select_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *testSuite) TestUpdateCopRuntimeStats(c *C) {
sr.rootPlanID = 1234
sr.updateCopRuntimeStats(context.Background(), &copr.CopRuntimeStats{ExecDetails: execdetails.ExecDetails{CalleeAddress: "a"}}, 0)

ctx.GetSessionVars().StmtCtx.RuntimeStatsColl = execdetails.NewRuntimeStatsColl()
ctx.GetSessionVars().StmtCtx.RuntimeStatsColl = execdetails.NewRuntimeStatsColl(nil)
t := uint64(1)
sr.selectResp = &tipb.SelectResponse{
ExecutionSummaries: []*tipb.ExecutorExecutionSummary{
Expand Down
2 changes: 1 addition & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func (b *executorBuilder) buildExplain(v *plannercore.Explain) Executor {
}
if v.Analyze {
if b.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl == nil {
b.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl = execdetails.NewRuntimeStatsColl()
b.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl = execdetails.NewRuntimeStatsColl(nil)
}
explainExec.analyzeExec = b.build(v.TargetPlan)
}
Expand Down
8 changes: 7 additions & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,13 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
sc.PrevAffectedRows = -1
}
if globalConfig.EnableCollectExecutionInfo {
sc.RuntimeStatsColl = execdetails.NewRuntimeStatsColl()
// In ExplainFor case, RuntimeStatsColl should not be reset for reuse,
// because ExplainFor need to display the last statement information.
reuseObj := vars.StmtCtx.RuntimeStatsColl
if _, ok := s.(*ast.ExplainForStmt); ok {
reuseObj = nil
}
sc.RuntimeStatsColl = execdetails.NewRuntimeStatsColl(reuseObj)
}

sc.TblInfo2UnionScan = make(map[*model.TableInfo]bool)
Expand Down
20 changes: 17 additions & 3 deletions util/execdetails/execdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,23 @@ type RuntimeStatsColl struct {
}

// NewRuntimeStatsColl creates new executor collector.
func NewRuntimeStatsColl() *RuntimeStatsColl {
return &RuntimeStatsColl{rootStats: make(map[int]*RootRuntimeStats),
copStats: make(map[int]*CopRuntimeStats)}
// Reuse the object to reduce allocation when *RuntimeStatsColl is not nil.
func NewRuntimeStatsColl(reuse *RuntimeStatsColl) *RuntimeStatsColl {
if reuse != nil {
// Reuse map is cheaper than create a new map object.
// Go compiler optimize this cleanup code pattern to a clearmap() function.
for k := range reuse.rootStats {
delete(reuse.rootStats, k)
}
for k := range reuse.copStats {
delete(reuse.copStats, k)
}
return reuse
}
return &RuntimeStatsColl{
rootStats: make(map[int]*RootRuntimeStats),
copStats: make(map[int]*CopRuntimeStats),
}
}

// RegisterStats register execStat for a executor.
Expand Down
6 changes: 3 additions & 3 deletions util/execdetails/execdetails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func mockExecutorExecutionSummaryForTiFlash(TimeProcessedNs, NumProducedRows, Nu
}

func TestCopRuntimeStats(t *testing.T) {
stats := NewRuntimeStatsColl()
stats := NewRuntimeStatsColl(nil)
tableScanID := 1
aggID := 2
tableReaderID := 3
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestCopRuntimeStats(t *testing.T) {
}

func TestCopRuntimeStatsForTiFlash(t *testing.T) {
stats := NewRuntimeStatsColl()
stats := NewRuntimeStatsColl(nil)
tableScanID := 1
aggID := 2
tableReaderID := 3
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestRootRuntimeStats(t *testing.T) {
basic1.Record(time.Second, 20)
basic2.Record(time.Second*2, 30)
pid := 1
stmtStats := NewRuntimeStatsColl()
stmtStats := NewRuntimeStatsColl(nil)
stmtStats.RegisterStats(pid, basic1)
stmtStats.RegisterStats(pid, basic2)
concurrency := &RuntimeStatsWithConcurrencyInfo{}
Expand Down

0 comments on commit 9abf9d0

Please sign in to comment.