Skip to content

Make fewer allocations in QueryContext(). #2349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions stdlib/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ func (c *Conn) ExecContext(ctx context.Context, query string, argsV []driver.Nam
return nil, driver.ErrBadConn
}

args := namedValueToInterface(argsV)
args := make([]any, len(argsV))
convertNamedArguments(args, argsV)

commandTag, err := c.conn.Exec(ctx, query, args...)
// if we got a network error before we had a chance to send the query, retry
Expand All @@ -488,8 +489,9 @@ func (c *Conn) QueryContext(ctx context.Context, query string, argsV []driver.Na
return nil, driver.ErrBadConn
}

args := []any{databaseSQLResultFormats}
args = append(args, namedValueToInterface(argsV)...)
args := make([]any, 1+len(argsV))
args[0] = databaseSQLResultFormats
convertNamedArguments(args[1:], argsV)

rows, err := c.conn.Query(ctx, query, args...)
if err != nil {
Expand Down Expand Up @@ -848,28 +850,14 @@ func (r *Rows) Next(dest []driver.Value) error {
return nil
}

func valueToInterface(argsV []driver.Value) []any {
args := make([]any, 0, len(argsV))
for _, v := range argsV {
if v != nil {
args = append(args, v.(any))
} else {
args = append(args, nil)
}
}
return args
}

func namedValueToInterface(argsV []driver.NamedValue) []any {
args := make([]any, 0, len(argsV))
for _, v := range argsV {
func convertNamedArguments(args []any, argsV []driver.NamedValue) {
for i, v := range argsV {
if v.Value != nil {
args = append(args, v.Value.(any))
args[i] = v.Value.(any)
} else {
args = append(args, nil)
args[i] = nil
}
}
return args
}

type wrapTx struct {
Expand Down