Skip to content

Commit 350cbd1

Browse files
authored
session: tiny changes to reduce object allocations (pingcap#26244)
1 parent 8f51a40 commit 350cbd1

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

executor/prepared.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ func (e *DeallocateExec) Next(ctx context.Context, req *chunk.Chunk) error {
315315

316316
// CompileExecutePreparedStmt compiles a session Execute command to a stmt.Statement.
317317
func CompileExecutePreparedStmt(ctx context.Context, sctx sessionctx.Context,
318-
ID uint32, is infoschema.InfoSchema, snapshotTS uint64, args []types.Datum) (sqlexec.Statement, bool, bool, error) {
318+
ID uint32, is infoschema.InfoSchema, snapshotTS uint64, args []types.Datum) (*ExecStmt, bool, bool, error) {
319319
startTime := time.Now()
320320
defer func() {
321321
sctx.GetSessionVars().DurationCompile = time.Since(startTime)

session/bootstrap_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ package session
1616
import (
1717
"context"
1818
"fmt"
19-
"sync"
2019

2120
. "github.com/pingcap/check"
22-
"github.com/pingcap/parser"
2321
"github.com/pingcap/parser/auth"
2422
"github.com/pingcap/tidb/config"
2523
"github.com/pingcap/tidb/domain"
@@ -119,7 +117,6 @@ func globalVarsCount() int64 {
119117
func (s *testBootstrapSuite) bootstrapWithOnlyDDLWork(store kv.Storage, c *C) {
120118
ss := &session{
121119
store: store,
122-
parserPool: &sync.Pool{New: func() interface{} { return parser.New() }},
123120
sessionVars: variable.NewSessionVars(),
124121
}
125122
ss.txn.init()

session/schema_amender_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ import (
1818
"context"
1919
"sort"
2020
"strconv"
21-
"sync"
2221

2322
. "github.com/pingcap/check"
2423
"github.com/pingcap/kvproto/pkg/kvrpcpb"
25-
"github.com/pingcap/parser"
2624
"github.com/pingcap/parser/model"
2725
"github.com/pingcap/parser/mysql"
2826
"github.com/pingcap/tidb/kv"
@@ -255,7 +253,6 @@ func (s *testSchemaAmenderSuite) TestAmendCollectAndGenMutations(c *C) {
255253
defer store.Close()
256254
se := &session{
257255
store: store,
258-
parserPool: &sync.Pool{New: func() interface{} { return parser.New() }},
259256
sessionVars: variable.NewSessionVars(),
260257
}
261258
startStates := []model.SchemaState{model.StateNone, model.StateDeleteOnly, model.StateWriteOnly, model.StateWriteReorganization}

session/session.go

+26-11
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ type session struct {
205205

206206
store kv.Storage
207207

208-
parserPool *sync.Pool
209-
210208
preparedPlanCache *kvcache.SimpleLRUCache
211209

212210
sessionVars *variable.SessionVars
@@ -225,8 +223,12 @@ type session struct {
225223

226224
// indexUsageCollector collects index usage information.
227225
idxUsageCollector *handle.SessionIndexUsageCollector
226+
227+
cache [1]ast.StmtNode
228228
}
229229

230+
var parserPool = &sync.Pool{New: func() interface{} { return parser.New() }}
231+
230232
// AddTableLock adds table lock to the session lock map.
231233
func (s *session) AddTableLock(locks []model.TableLockTpInfo) {
232234
for _, l := range locks {
@@ -1241,11 +1243,19 @@ func (s *session) ParseSQL(ctx context.Context, sql, charset, collation string)
12411243
}
12421244
defer trace.StartRegion(ctx, "ParseSQL").End()
12431245

1244-
p := s.parserPool.Get().(*parser.Parser)
1245-
defer s.parserPool.Put(p)
1246+
p := parserPool.Get().(*parser.Parser)
1247+
defer parserPool.Put(p)
12461248
p.SetSQLMode(s.sessionVars.SQLMode)
12471249
p.SetParserConfig(s.sessionVars.BuildParserConfig())
1248-
return p.Parse(sql, charset, collation)
1250+
tmp, warn, err := p.Parse(sql, charset, collation)
1251+
// The []ast.StmtNode is referenced by the parser, to reuse the parser, make a copy of the result.
1252+
if len(tmp) == 1 {
1253+
s.cache[0] = tmp[0]
1254+
return s.cache[:], warn, err
1255+
}
1256+
res := make([]ast.StmtNode, len(tmp))
1257+
copy(res, tmp)
1258+
return res, warn, err
12491259
}
12501260

12511261
func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecutionTime uint64) {
@@ -1861,7 +1871,7 @@ func (s *session) preparedStmtExec(ctx context.Context,
18611871
}
18621872
}
18631873
sessionExecuteCompileDurationGeneral.Observe(time.Since(s.sessionVars.StartTime).Seconds())
1864-
logQuery(st.OriginText(), s)
1874+
logGeneralQuery(st, s, true)
18651875
return runStmt(ctx, s, st)
18661876
}
18671877

@@ -1901,7 +1911,7 @@ func (s *session) cachedPlanExec(ctx context.Context,
19011911
stmtCtx.OriginalSQL = stmt.Text
19021912
stmtCtx.InitSQLDigest(prepareStmt.NormalizedSQL, prepareStmt.SQLDigest)
19031913
stmtCtx.SetPlanDigest(prepareStmt.NormalizedPlan, prepareStmt.PlanDigest)
1904-
logQuery(stmt.GetTextToLog(), s)
1914+
logGeneralQuery(stmt, s, false)
19051915

19061916
if !s.isInternal() && config.GetGlobalConfig().EnableTelemetry {
19071917
telemetry.CurrentExecuteCount.Inc()
@@ -2637,7 +2647,6 @@ func createSessionWithOpt(store kv.Storage, opt *Opt) (*session, error) {
26372647
}
26382648
s := &session{
26392649
store: store,
2640-
parserPool: &sync.Pool{New: func() interface{} { return parser.New() }},
26412650
sessionVars: variable.NewSessionVars(),
26422651
ddlOwnerChecker: dom.DDL().OwnerManager(),
26432652
client: store.GetClient(),
@@ -2671,7 +2680,6 @@ func createSessionWithOpt(store kv.Storage, opt *Opt) (*session, error) {
26712680
func CreateSessionWithDomain(store kv.Storage, dom *domain.Domain) (*session, error) {
26722681
s := &session{
26732682
store: store,
2674-
parserPool: &sync.Pool{New: func() interface{} { return parser.New() }},
26752683
sessionVars: variable.NewSessionVars(),
26762684
client: store.GetClient(),
26772685
mppClient: store.GetMPPClient(),
@@ -2894,13 +2902,20 @@ func logStmt(execStmt *executor.ExecStmt, s *session) {
28942902
zap.Stringer("user", user))
28952903
}
28962904
default:
2897-
logQuery(execStmt.GetTextToLog(), s)
2905+
logGeneralQuery(execStmt, s, false)
28982906
}
28992907
}
29002908

2901-
func logQuery(query string, s *session) {
2909+
func logGeneralQuery(execStmt *executor.ExecStmt, s *session, isPrepared bool) {
29022910
vars := s.GetSessionVars()
29032911
if variable.ProcessGeneralLog.Load() && !vars.InRestrictedSQL {
2912+
var query string
2913+
if isPrepared {
2914+
query = execStmt.OriginText()
2915+
} else {
2916+
query = execStmt.GetTextToLog()
2917+
}
2918+
29042919
query = executor.QueryReplacer.Replace(query)
29052920
if !vars.EnableRedactLog {
29062921
query += vars.PreparedParams.String()

0 commit comments

Comments
 (0)