Skip to content

Commit

Permalink
Merge pull request cockroachdb#21306 from knz/20180107-syncpool
Browse files Browse the repository at this point in the history
sem/tree: use sync.Pool to ease reuse of FmtCtxWithBuf
  • Loading branch information
knz committed Jan 8, 2018
2 parents 4051ba3 + 9986e8c commit 1f5b9e9
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions pkg/sql/sem/tree/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package tree
import (
"bytes"
"fmt"
"sync"

"github.com/cockroachdb/cockroach/pkg/sql/coltypes"
"github.com/cockroachdb/cockroach/pkg/sql/lex"
Expand Down Expand Up @@ -328,39 +329,27 @@ type FmtCtxWithBuf struct {
buf bytes.Buffer
}

// TODO(nathan/peter): the tests break horribly when the following code is enabled:
//
// var fmtCtxWithBufPool = sync.Pool{
// New: func() interface{} {
// f := &FmtCtxWithBuf{}
// f.FmtCtx.Buffer = &f.buf
// return f
// },
// }
//
// // NewFmtCtxWithBuf returns a FmtCtxWithBuf ready for use.
// func NewFmtCtxWithBuf(f FmtFlags) *FmtCtxWithBuf {
// ctx := fmtCtxWithBufPool.Get().(*FmtCtxWithBuf)
// ctx.FmtCtx.flags = f
// return ctx
// }
//
// // Close releases the FmtCtxWithBuf.
// func (f *FmtCtxWithBuf) Close() {
// f.Reset()
// fmtCtxWithBufPool.Put(f)
// }
var fmtCtxWithBufPool = sync.Pool{
New: func() interface{} {
ctx := &FmtCtxWithBuf{}
ctx.Buffer = &ctx.buf
return ctx
},
}

// NewFmtCtxWithBuf returns a FmtCtxWithBuf ready for use.
func NewFmtCtxWithBuf(f FmtFlags) *FmtCtxWithBuf {
ctx := &FmtCtxWithBuf{}
ctx.FmtCtx.Buffer = &ctx.buf
ctx.FmtCtx.flags = f
ctx := fmtCtxWithBufPool.Get().(*FmtCtxWithBuf)
ctx.flags = f
return ctx
}

// Close releases the FmtCtxWithBuf.
func (f *FmtCtxWithBuf) Close() {}
func (f *FmtCtxWithBuf) Close() {
f.Reset()
f.FmtCtx = FmtCtx{Buffer: &f.buf}
fmtCtxWithBufPool.Put(f)
}

// CloseAndGetString combines Close() and String().
func (f *FmtCtxWithBuf) CloseAndGetString() string {
Expand Down

0 comments on commit 1f5b9e9

Please sign in to comment.