Skip to content

Commit ae5dae4

Browse files
authored
Make several small speed tweaks for binary reading & writing (brianc#2158)
1 parent 0a90e01 commit ae5dae4

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

packages/pg-protocol/src/b.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22

33
import { Writer } from './buffer-writer'
44
import { serialize } from './index'
5+
import { BufferReader } from './buffer-reader'
56

67
const LOOPS = 1000
78
let count = 0
89
let start = Date.now()
910
const writer = new Writer()
1011

12+
const reader = new BufferReader()
13+
const buffer = Buffer.from([33, 33, 33, 33, 33, 33, 33, 0])
14+
1115
const run = () => {
1216
if (count > LOOPS) {
1317
console.log(Date.now() - start)
1418
return;
1519
}
1620
count++
1721
for(let i = 0; i < LOOPS; i++) {
18-
serialize.describe({ type: 'P'})
19-
serialize.describe({ type: 'S'})
22+
reader.setBuffer(0, buffer)
23+
reader.cstring()
2024
}
2125
setImmediate(run)
2226
}

packages/pg-protocol/src/buffer-reader.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,44 @@ export class BufferReader {
88

99
constructor(private offset: number = 0) {
1010
}
11+
1112
public setBuffer(offset: number, buffer: Buffer): void {
1213
this.offset = offset;
1314
this.buffer = buffer;
1415
}
15-
public int16() {
16+
17+
public int16(): number {
1618
const result = this.buffer.readInt16BE(this.offset);
1719
this.offset += 2;
1820
return result;
1921
}
20-
public byte() {
22+
23+
public byte(): number {
2124
const result = this.buffer[this.offset];
2225
this.offset++;
2326
return result;
2427
}
25-
public int32() {
28+
29+
public int32(): number {
2630
const result = this.buffer.readInt32BE(this.offset);
2731
this.offset += 4;
2832
return result;
2933
}
34+
3035
public string(length: number): string {
3136
const result = this.buffer.toString(this.encoding, this.offset, this.offset + length);
3237
this.offset += length;
3338
return result;
3439
}
40+
3541
public cstring(): string {
36-
var start = this.offset;
37-
var end = this.buffer.indexOf(0, start);
38-
this.offset = end + 1;
39-
return this.buffer.toString(this.encoding, start, end);
42+
const start = this.offset;
43+
let end = start
44+
while(this.buffer[end++] !== 0) { };
45+
this.offset = end;
46+
return this.buffer.toString(this.encoding, start, end - 1);
4047
}
48+
4149
public bytes(length: number): Buffer {
4250
const result = this.buffer.slice(this.offset, this.offset + length);
4351
this.offset += length;

packages/pg-protocol/src/parser.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,8 @@ export class Parser {
214214
const fields: any[] = new Array(fieldCount);
215215
for (let i = 0; i < fieldCount; i++) {
216216
const len = this.reader.int32();
217-
if (len === -1) {
218-
fields[i] = null
219-
} else if (this.mode === 'text') {
220-
fields[i] = this.reader.string(len)
221-
}
217+
// a -1 for length means the value of the field is null
218+
fields[i] = len === -1 ? null : this.reader.string(len)
222219
}
223220
return new DataRowMessage(length, fields);
224221
}
@@ -290,8 +287,8 @@ export class Parser {
290287

291288
private parseErrorMessage(offset: number, length: number, bytes: Buffer, name: MessageName) {
292289
this.reader.setBuffer(offset, bytes);
293-
var fields: Record<string, string> = {}
294-
var fieldType = this.reader.string(1)
290+
const fields: Record<string, string> = {}
291+
let fieldType = this.reader.string(1)
295292
while (fieldType !== '\0') {
296293
fields[fieldType] = this.reader.cstring()
297294
fieldType = this.reader.string(1)

packages/pg/bench.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const run = async () => {
5454
queries = await bench(client, seq, seconds * 1000);
5555
console.log("sequence queries:", queries);
5656
console.log("qps", queries / seconds);
57-
console.log("on my laptop best so far seen 1209 qps")
57+
console.log("on my laptop best so far seen 1309 qps")
5858

5959
console.log('')
6060
queries = await bench(client, insert, seconds * 1000);

0 commit comments

Comments
 (0)