Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: optimize the parameterization(parser.Restore) performance for non-prep plan cache #43018

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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