Skip to content

Move the connection's getters to a shared prototype - #1325

Closed
colinhacks wants to merge 2 commits into
porsager:masterfrom
colinhacks:connection-fast-properties
Closed

colinhacks wants to merge 2 commits into
porsager:masterfrom
colinhacks:connection-fast-properties

Conversation

@colinhacks

@colinhacks colinhacks commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

The connection object is an object literal with seven getters over its closure's state: status, inflight, exclusive, copying, closed, closing and notable. V8 creates an object literal that has a getter in dictionary mode, so every property read on a connection is a hash lookup instead of an inline cache hit. The pool reads these for every connection each time it places a query, and with 200 queries in flight its pick() took 29% of the client's busy CPU.

This moves the getters onto a small class, Handle, whose prototype the literal takes with __proto__. Each getter reads the connection's state through a closure the literal holds ($status, $closed, …), and inflight reads the sent and pending arrays directly. The getters return the same values as before.

Results

select $1::int4 against PostgreSQL 18 on the same machine, an M1 Max (load average 3–6), on Node.js 26.11.1. Master (1cc66f4) and this branch alternate, a fresh process per run, 6 rounds:

Queries in flight Queries per second, median Branch ÷ master per round CPU per query, median Branch ÷ master per round
10 70,600 → 73,400 1.04× (1.00–1.06) 16.4 → 15.9 µs 0.97× (0.95–1.00)
50 184,300 → 222,200 1.18× (1.13–1.25) 6.73 → 5.82 µs 0.88× (0.76–1.00)
200 276,900 → 374,700 1.34× (1.09–1.54) 4.21 → 3.18 µs 0.76× (0.59–0.99)
  • With 200 in flight, pick() falls from 29% to 11% of busy CPU (median of three --cpu-prof profiles each).
  • In an 8-round run of the first commit, which also declared the fields the pool adds to a connection, one query at a time, a 10-parameter insert with 200 in flight and a 3-statement transaction with 10 in flight were within noise (CPU 0.97–0.98×, not lower in every round).
  • On Bun 1.4.3, whose JavaScriptCore has no dictionary mode, that run gave CPU per query 0.98× with 200 in flight and 1.00× one at a time, also within noise.

Tests

A unit test in tests/unit/pool.js builds two connections and checks with %HasFastProperties and %HaveSameMap that each has fast properties and that both share one hidden class. It turns --allow-natives-syntax on with v8.setFlagsFromString for those two functions and returns early on Bun and Deno. On master it fails with false false true.

  • tests/unit, the 622 integration tests and tests/simulate.js (2,000 seeds) pass on Node.js 18.20.8, 20.20.2, 22.23.5, 24.22.1 and 26.11.1.
  • The unit and integration tests pass on Bun 1.4.3, and the unit tests on Deno 2.9.8.
  • tests/cf passes in wrangler dev, direct and through the Hyperdrive binding.
  • Lint passes, and there is a CHANGELOG entry.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 17:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@panoply

panoply commented Nov 13, 2026

Copy link
Copy Markdown

This feels off imo. It adds a prototype, $ prefixed closures, pre-declared pool fields and a V8 internals test purely to appease V8s property heuristicss... The gain is limited to heavily pipe-lined workloads on Node with everything else within noise. Engine-specific indirection for that feels like a reach.

@colinhacks

Copy link
Copy Markdown
Contributor Author

Fair on the fields, they don't measurably pay. Dropped them in 470586b, so the change is now the getters moving to a prototype.

It isn't only deep pipelines though. Quieter machine, 6 rounds against master:

Queries in flight Queries/s CPU per query
10 1.04× 0.97×
50 1.18× 0.88×
200 1.34× 0.76×

The pool's pick() reads these getters on every connection for every query it places, so the cost grows with the queries in flight. Dictionary mode is V8's, so Deno and Workers get it too (measured on Node 26 here), and Bun is unchanged.

@porsager

Copy link
Copy Markdown
Owner

This PR was interesting in that it exposed a smell in the original code.. I've gone ahead and did a bit of work in this area to optimize further and have added some changes that go beyond this fix and improves performance a bit more.

Thanks a lot for shining a light on this!
I'm going to close this one out as the other changes supersedes it.

@porsager porsager closed this Nov 14, 2026
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.

4 participants