From c55bc093be16527167262d014d97f15154dfd1d6 Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Tue, 17 Jan 2023 23:33:28 +0700 Subject: [PATCH] feat: use sync.Pool to reuse bytes.Buffer See https://github.com/cosmos/iavl/pull/453/files --- internal/imports/formatter.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/imports/formatter.go b/internal/imports/formatter.go index f2c89e4..2c0293b 100644 --- a/internal/imports/formatter.go +++ b/internal/imports/formatter.go @@ -38,6 +38,13 @@ var ( ErrGoModEmptyModule = errors.New("go mod empty module") ) +// https://pkg.go.dev/sync#Pool +var bufPool = sync.Pool{ + New: func() any { + return new(bytes.Buffer) + }, +} + // stdPackages -> save std packages for later search // // moduleNames -> map path to its go.mod module name @@ -287,12 +294,17 @@ func (ft *Formatter) formatImports( // Second update dstFile.Decls[0].(*dst.GenDecl).Specs = formattedGenSpecs - var buf bytes.Buffer - if err := decorator.Fprint(&buf, dstFile); err != nil { + b := bufPool.Get().(*bytes.Buffer) + b.Reset() + defer bufPool.Put(b) + + if err := decorator.Fprint(b, dstFile); err != nil { return nil, fmt.Errorf("decorator: failed to fprint [%s]: %w", path, err) } - return buf.Bytes(), nil + result := make([]byte, b.Len()) + copy(result, b.Bytes()) + return result, nil } func (ft *Formatter) groupDSTImportSpecs(importSpecs []*dst.ImportSpec, moduleName string) (map[string][]*dst.ImportSpec, error) {