mirror of https://github.com/jackc/pgx.git
perf(tx): use strings.Builder to avoid the overhead of []byte -> string conversion
parent
fbfafb3edf
commit
8eb062f588
21
tx.go
21
tx.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgconn"
|
"github.com/jackc/pgx/v5/pgconn"
|
||||||
)
|
)
|
||||||
|
@ -52,22 +53,24 @@ func (txOptions TxOptions) beginSQL() string {
|
||||||
return "begin"
|
return "begin"
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, 0, 64) // 64 - maximum length of string with available options
|
var buf strings.Builder
|
||||||
buf = append(buf, "begin"...)
|
buf.Grow(64) // 64 - maximum length of string with available options
|
||||||
|
buf.WriteString("begin")
|
||||||
|
|
||||||
if txOptions.IsoLevel != "" {
|
if txOptions.IsoLevel != "" {
|
||||||
buf = append(buf, " isolation level "...)
|
buf.WriteString(" isolation level ")
|
||||||
buf = append(buf, txOptions.IsoLevel...)
|
buf.WriteString(string(txOptions.IsoLevel))
|
||||||
}
|
}
|
||||||
if txOptions.AccessMode != "" {
|
if txOptions.AccessMode != "" {
|
||||||
buf = append(buf, ' ')
|
buf.WriteByte(' ')
|
||||||
buf = append(buf, txOptions.AccessMode...)
|
buf.WriteString(string(txOptions.AccessMode))
|
||||||
}
|
}
|
||||||
if txOptions.DeferrableMode != "" {
|
if txOptions.DeferrableMode != "" {
|
||||||
buf = append(buf, ' ')
|
buf.WriteByte(' ')
|
||||||
buf = append(buf, txOptions.DeferrableMode...)
|
buf.WriteString(string(txOptions.DeferrableMode))
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(buf)
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrTxClosed = errors.New("tx is closed")
|
var ErrTxClosed = errors.New("tx is closed")
|
||||||
|
|
Loading…
Reference in New Issue