Skip to content

perf(pg): skip per-query config copy and unused array-mode row object - #3871

Open
mcollina wants to merge 1 commit into
brianc:masterfrom
mcollina:perf/query-config-array-fields
Open

mcollina wants to merge 1 commit into
brianc:masterfrom
mcollina:perf/query-config-array-fields

Conversation

@mcollina

Copy link
Copy Markdown
Contributor

This removes two allocations that every query pays for and doesn't need. Both are visible in CPU profiles of workloads that run many small queries, especially with rowMode: 'array'.

The Query and NativeQuery constructors copied the caller's config with Object.getOwnPropertyDescriptors and Object.defineProperties. That copy exists so that normalizeQueryConfig can write values and callback without changing the caller's object (#3720), and it costs about 0.65 µs per query. The constructors now read the config as given. values and a callback passed as arguments take precedence, using the same rules as normalizeQueryConfig. The caller's config is still never written to, and getters and prototype properties are read the same way as before. normalizeQueryConfig itself is unchanged, since it is reachable through pg/lib/utils.

Result.addFields built _prebuiltEmptyResultObject, an object keyed by column name, for every result set. Only object rows use it, so in rowMode: 'array' it is now skipped.

The table shows CPU time per operation: Node 24.18 on Apple Silicon, median of 5 interleaved runs against master. The result benchmarks use six columns and one row with the default type parsers. Rows marked "unchanged" are controls whose code paths this PR doesn't touch.

Benchmark master (µs) this PR (µs)
new Query(config) 0.690 0.041
new Query(config, undefined, callback) 0.692 0.036
new Query(text, values, callback), unchanged 0.038 0.038
new Result('array'), addFields, parseRow 0.672 0.124
new Result(), addFields, parseRow, unchanged 0.715 0.723

Every query paid for two allocations it did not need:

- The Query and NativeQuery constructors copied the caller's config with
  Object.getOwnPropertyDescriptors and Object.defineProperties, so that
  normalizeQueryConfig could write values and callback onto the copy
  without touching the caller's object (brianc#3720). The constructors now read
  the config as given and let values and a callback passed as arguments
  take precedence, with the same precedence as normalizeQueryConfig. The
  caller's config is still never written to, and getters and prototype
  properties are read as before. normalizeQueryConfig itself is unchanged.
- Result.addFields built _prebuiltEmptyResultObject, an object keyed by
  column name, for every result set. Only object rows use it, so rowMode
  'array' now skips it.
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