Repository navigation
Move the connection's getters to a shared prototype - #1325
colinhacks wants to merge 2 commits into
Conversation
|
This feels off imo. It adds a prototype, |
|
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:
The pool's |
|
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! |
The connection object is an object literal with seven getters over its closure's state:
status,inflight,exclusive,copying,closed,closingandnotable. 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 itspick()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, …), andinflightreads thesentandpendingarrays directly. The getters return the same values as before.Results
select $1::int4against 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:pick()falls from 29% to 11% of busy CPU (median of three--cpu-profprofiles each).Tests
A unit test in
tests/unit/pool.jsbuilds two connections and checks with%HasFastPropertiesand%HaveSameMapthat each has fast properties and that both share one hidden class. It turns--allow-natives-syntaxon withv8.setFlagsFromStringfor those two functions and returns early on Bun and Deno. On master it fails withfalse false true.tests/unit, the 622 integration tests andtests/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.tests/cfpasses inwrangler dev, direct and through the Hyperdrive binding.