Skip to content

Commit da8224e

Browse files
committedFeb 12, 2025·
sqlstats: Reuse sessionphase.Times on stats collector
StatsCollector.Reset shows up in sqlstats section of cpu/mem profiles. This seemed to mostly be allocations of new session.PhaseTimes objects. This commit avoids new allocations of session.PhaseTimes when resetting the stats collector. Epic: none Release note: None

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed
 

‎pkg/sql/sqlstats/sslocal/sslocal_stats_collector.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ type StatsCollector struct {
4343
insightsWriter *insights.ConcurrentBufferIngester
4444

4545
// phaseTimes tracks session-level phase times.
46-
phaseTimes *sessionphase.Times
46+
phaseTimes sessionphase.Times
4747

4848
// previousPhaseTimes tracks the session-level phase times for the previous
4949
// query. This enables the `SHOW LAST QUERY STATISTICS` observer statement.
50-
previousPhaseTimes *sessionphase.Times
50+
previousPhaseTimes sessionphase.Times
5151

5252
// sendInsights is true if we should send statement and transaction stats to
5353
// the insights system for the current transaction. This value is reset for
@@ -96,7 +96,7 @@ func NewStatsCollector(
9696
flushTarget: appStats,
9797
currentTransactionStatementStats: currentTransactionStatementStats,
9898
insightsWriter: insights,
99-
phaseTimes: phaseTime.Clone(),
99+
phaseTimes: *phaseTime,
100100
uniqueServerCounts: uniqueServerCounts,
101101
st: st,
102102
knobs: knobs,
@@ -115,24 +115,22 @@ func (s *StatsCollector) StatementFingerprintID() appstatspb.StmtFingerprintID {
115115
// PhaseTimes returns the sessionphase.Times that this StatsCollector is
116116
// currently tracking.
117117
func (s *StatsCollector) PhaseTimes() *sessionphase.Times {
118-
return s.phaseTimes
118+
return &s.phaseTimes
119119
}
120120

121121
// PreviousPhaseTimes returns the sessionphase.Times that this StatsCollector
122122
// was previously tracking before being Reset.
123123
func (s *StatsCollector) PreviousPhaseTimes() *sessionphase.Times {
124-
return s.previousPhaseTimes
124+
return &s.previousPhaseTimes
125125
}
126126

127127
// Reset resets the StatsCollector with a new flushTarget and a new copy
128128
// of the sessionphase.Times.
129129
func (s *StatsCollector) Reset(appStats *ssmemstorage.Container, phaseTime *sessionphase.Times) {
130-
previousPhaseTime := s.phaseTimes
131130
s.flushTarget = appStats
132-
133-
s.previousPhaseTimes = previousPhaseTime
134-
s.phaseTimes = phaseTime.Clone()
135131
s.stmtFingerprintID = 0
132+
s.previousPhaseTimes = s.phaseTimes
133+
s.phaseTimes = *phaseTime
136134
}
137135

138136
// Close frees any local memory used by the stats collector and

0 commit comments

Comments
 (0)
Please sign in to comment.