Skip to content

Commit f0a73f8

Browse files
committed
Switch internals to use faster connection
This switches the internals to use faster protocol parsing & serializing. This results in a significant (30% - 50%) speed up in some common query patterns. There is quite a bit more performance work I need to do, but this takes care of some initial stuff & removes a big fork in the code. Remove stuff so I can see whats going on in travis better More logging Empty commit to try to kick off tests
1 parent 1c441d2 commit f0a73f8

File tree

8 files changed

+55
-972
lines changed

8 files changed

+55
-972
lines changed

.travis.yml

+3-42
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,21 @@ before_script: |
77
88
env:
99
- CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres
10-
# test w/ new faster parsing code
11-
- CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres PG_FAST_CONNECTION=true
1210

1311
node_js:
14-
- lts/dubnium
15-
- lts/erbium
16-
# node 13.7 seems to have changed behavior of async iterators exiting early on streams
17-
# if 13.8 still has this problem when it comes down I'll talk to the node team about the change
18-
# in the mean time...peg to 13.6
19-
- 13.6
2012
- 14
2113

2214
addons:
23-
postgresql: "10"
15+
postgresql: '10'
2416

2517
matrix:
2618
include:
27-
# Run tests/paths that require password authentication
28-
- node_js: lts/erbium
29-
env:
30-
- CC=clang CXX=clang++ npm_config_clang=1 PGUSER=postgres PGDATABASE=postgres PGPASSWORD=test-password SCRAM_TEST_PGUSER=scram_test SCRAM_TEST_PGPASSWORD=test4scram
31-
before_script: |
32-
sudo -u postgres sed -i \
33-
-e '/^local/ s/trust$/peer/' \
34-
-e '/^host/ s/trust$/md5/' \
35-
/etc/postgresql/10/main/pg_hba.conf
36-
sudo -u postgres psql -c "ALTER ROLE postgres PASSWORD 'test-password'; SELECT pg_reload_conf()"
37-
yarn build
38-
node packages/pg/script/create-test-tables.js postgresql:///
39-
sudo -u postgres -- psql \
40-
-c "SET password_encryption = 'scram-sha-256'" \
41-
-c "CREATE ROLE scram_test login password 'test4scram'"
42-
43-
- node_js: lts/carbon
44-
addons:
45-
postgresql: "9.5"
46-
dist: precise
47-
48-
# different PostgreSQL versions on Node LTS
49-
- node_js: lts/erbium
50-
addons:
51-
postgresql: "9.3"
52-
- node_js: lts/erbium
53-
addons:
54-
postgresql: "9.4"
55-
- node_js: lts/erbium
56-
addons:
57-
postgresql: "9.5"
5819
- node_js: lts/erbium
5920
addons:
60-
postgresql: "9.6"
21+
postgresql: '9.4'
6122

6223
# PostgreSQL 9.2 only works on precise
6324
- node_js: lts/carbon
6425
addons:
65-
postgresql: "9.2"
26+
postgresql: '9.2'
6627
dist: precise

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"packages/*"
1111
],
1212
"scripts": {
13-
"test": "yarn lint && yarn lerna exec yarn test",
13+
"test": "yarn lerna exec --scope pg-cursor yarn test",
1414
"build": "yarn lerna exec --scope pg-protocol yarn build",
1515
"pretest": "yarn build",
1616
"lint": "if [ -x ./node_modules/.bin/prettier ]; then eslint '*/**/*.{js,ts,tsx}'; fi;"

packages/pg-cursor/test/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ describe('cursor', function () {
8989
const cursor = this.pgCursor(text, values)
9090
let count = 0
9191
const read = function () {
92+
const start = Date.now()
93+
console.log('reading')
9294
cursor.read(100, function (err, rows) {
95+
console.log('read', Date.now() - start)
9396
if (err) return done(err)
9497
if (!rows.length) {
9598
assert.strictEqual(count, 100001)

packages/pg-protocol/src/parser.ts

+3
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ export class Parser {
8686
}
8787

8888
public parse(buffer: Buffer, callback: MessageCallback) {
89+
const start = Date.now()
8990
let combinedBuffer = buffer
9091
if (this.remainingBuffer.byteLength) {
9192
combinedBuffer = Buffer.allocUnsafe(this.remainingBuffer.byteLength + buffer.byteLength)
9293
this.remainingBuffer.copy(combinedBuffer)
9394
buffer.copy(combinedBuffer, this.remainingBuffer.byteLength)
95+
console.log('combine', combinedBuffer.byteLength)
9496
}
9597
let offset = 0
9698
while (offset + HEADER_LENGTH <= combinedBuffer.byteLength) {
@@ -116,6 +118,7 @@ export class Parser {
116118
} else {
117119
this.remainingBuffer = combinedBuffer.slice(offset)
118120
}
121+
console.log('parsed in', Date.now() - start)
119122
}
120123

121124
private handlePacket(offset: number, code: number, length: number, bytes: Buffer): BackendMessage {

packages/pg/lib/client.js

-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ var ConnectionParameters = require('./connection-parameters')
1818
var Query = require('./query')
1919
var defaults = require('./defaults')
2020
var Connection = require('./connection')
21-
if (process.env.PG_FAST_CONNECTION) {
22-
Connection = require('./connection-fast')
23-
}
2421

2522
var Client = function (config) {
2623
EventEmitter.call(this)

packages/pg/lib/connection-fast.js

-214
This file was deleted.

0 commit comments

Comments
 (0)