Skip to content

Commit

Permalink
Optimize 'beginSQL' runtime and memory allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
iClaus21 authored and jackc committed Nov 22, 2022
1 parent 174224f commit fbfafb3
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pgx

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit fbfafb3

Please sign in to comment.