From fbfafb3edfc378681c2bad91b1a126e7e6df3f5b Mon Sep 17 00:00:00 2001 From: Petr Evdokimov Date: Mon, 21 Nov 2022 13:50:08 +0300 Subject: [PATCH] Optimize 'beginSQL' runtime and memory allocations --- tx.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tx.go b/tx.go index 28a9af13..840ba045 100644 --- a/tx.go +++ b/tx.go @@ -1,7 +1,6 @@ package pgx import ( - "bytes" "context" "errors" "fmt" @@ -52,19 +51,23 @@ func (txOptions TxOptions) beginSQL() string { if txOptions == emptyTxOptions { return "begin" } - buf := &bytes.Buffer{} - buf.WriteString("begin") + + buf := make([]byte, 0, 64) // 64 - maximum length of string with available options + buf = append(buf, "begin"...) if txOptions.IsoLevel != "" { - fmt.Fprintf(buf, " isolation level %s", txOptions.IsoLevel) + buf = append(buf, " isolation level "...) + buf = append(buf, txOptions.IsoLevel...) } if txOptions.AccessMode != "" { - fmt.Fprintf(buf, " %s", txOptions.AccessMode) + buf = append(buf, ' ') + buf = append(buf, txOptions.AccessMode...) } if txOptions.DeferrableMode != "" { - fmt.Fprintf(buf, " %s", txOptions.DeferrableMode) + buf = append(buf, ' ') + buf = append(buf, txOptions.DeferrableMode...) } - return buf.String() + return string(buf) } var ErrTxClosed = errors.New("tx is closed")