Skip to content

Commit

Permalink
planner: optimize the parameterization(parser.Restore) performance fo…
Browse files Browse the repository at this point in the history
…r non-prep plan cache (#43018)

ref #36598
  • Loading branch information
qw4990 committed Apr 13, 2023
1 parent 2002ca1 commit 8a1e0c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
18 changes: 14 additions & 4 deletions parser/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,22 @@ func (rf RestoreFlags) HasRestoreForNonPrepPlanCache() bool {
return rf.has(RestoreForNonPrepPlanCache)
}

// RestoreWriter is the interface for `Restore` to write.
type RestoreWriter interface {
io.Writer
io.StringWriter
}

// RestoreCtx is `Restore` context to hold flags and writer.
type RestoreCtx struct {
Flags RestoreFlags
In io.Writer
In RestoreWriter
DefaultDB string
CTERestorer
}

// NewRestoreCtx returns a new `RestoreCtx`.
func NewRestoreCtx(flags RestoreFlags, in io.Writer) *RestoreCtx {
func NewRestoreCtx(flags RestoreFlags, in RestoreWriter) *RestoreCtx {
return &RestoreCtx{Flags: flags, In: in, DefaultDB: ""}
}

Expand Down Expand Up @@ -427,12 +433,16 @@ func (ctx *RestoreCtx) WriteName(name string) {
name = strings.Replace(name, "`", "``", -1)
quotes = "`"
}
fmt.Fprint(ctx.In, quotes, name, quotes)

// use `WriteString` directly instead of `fmt.Fprint` to get a better performance.
ctx.In.WriteString(quotes)
ctx.In.WriteString(name)
ctx.In.WriteString(quotes)
}

// WritePlain writes the plain text into writer without any handling.
func (ctx *RestoreCtx) WritePlain(plainText string) {
fmt.Fprint(ctx.In, plainText)
ctx.In.WriteString(plainText)
}

// WritePlainf write the plain text into writer without any handling.
Expand Down
9 changes: 4 additions & 5 deletions planner/core/plan_cache_param.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package core

import (
"bytes"
"context"
"errors"
"strings"
"sync"

"github.com/pingcap/tidb/expression"
Expand All @@ -41,8 +41,7 @@ var (
return pr
}}
paramCtxPool = sync.Pool{New: func() interface{} {
buf := new(strings.Builder)
buf.Reset()
buf := new(bytes.Buffer)
restoreCtx := format.NewRestoreCtx(format.RestoreForNonPrepPlanCache|format.RestoreStringWithoutCharset|format.RestoreStringSingleQuotes|format.RestoreNameBackQuotes, buf)
return restoreCtx
}}
Expand Down Expand Up @@ -106,14 +105,14 @@ func ParameterizeAST(ctx context.Context, sctx sessionctx.Context, stmt ast.Stmt
defer func() {
pr.Reset()
paramReplacerPool.Put(pr)
pCtx.In.(*strings.Builder).Reset()
pCtx.In.(*bytes.Buffer).Reset()
paramCtxPool.Put(pCtx)
}()
stmt.Accept(pr)
if err := stmt.Restore(pCtx); err != nil {
return "", nil, err
}
paramSQL, params = pCtx.In.(*strings.Builder).String(), pr.params
paramSQL, params = pCtx.In.(*bytes.Buffer).String(), pr.params
return
}

Expand Down

0 comments on commit 8a1e0c4

Please sign in to comment.