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

Conversation

dominiquelefevre
Copy link

For a simple query like SELECT * FROM t WHERE id = $1, QueryContext() used to make 6 allocations on average. Reduce that to 4 by avoiding two allocations that
are immediately discarded:

// the variadic arguments of c.conn.Query() escape,
// so args is heap-allocated
args := []any{databaseSQLResultFormats}
// append() reallocates args to add enough space +
// an array returned by namedValueToInterface()
// is immediately discarded
args = append(args, namedValueToInterface(argsV)...)

For a simple query like `SELECT * FROM t WHERE id = $1`,
QueryContext() used to make 6 allocations on average.
Reduce that to 4 by avoiding two allocations that
are immediately discarded:

  // the variadic arguments of c.conn.Query() escape,
  // so args is heap-allocated
  args := []any{databaseSQLResultFormats}
  // append() reallocates args to add enough space +
  // an array returned by namedValueToInterface()
  // is immediately discarded
  args = append(args, namedValueToInterface(argsV)...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant