Skip to content

Commit 8e896be

Browse files
committed
better buffer utilization in pg
1 parent ef08869 commit 8e896be

File tree

6 files changed

+12
-11
lines changed

6 files changed

+12
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const emptyBuffer = Buffer.allocUnsafe(0)
1+
const emptyBuffer = Buffer.allocUnsafeSlow(0)
22

33
export class BufferReader {
44
private buffer: Buffer = emptyBuffer

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export class Writer {
55
private offset: number = 5
66
private headerPosition: number = 0
77
constructor(private size = 256) {
8-
this.buffer = Buffer.allocUnsafe(size)
8+
this.buffer = Buffer.allocUnsafeSlow(size)
99
}
1010

1111
private ensure(size: number): void {
@@ -15,7 +15,7 @@ export class Writer {
1515
// exponential growth factor of around ~ 1.5
1616
// https://stackoverflow.com/questions/2269063/buffer-growth-strategy
1717
var newSize = oldBuffer.length + (oldBuffer.length >> 1) + size
18-
this.buffer = Buffer.allocUnsafe(newSize)
18+
this.buffer = Buffer.allocUnsafeSlow(newSize)
1919
oldBuffer.copy(this.buffer)
2020
}
2121
}
@@ -79,7 +79,7 @@ export class Writer {
7979
var result = this.join(code)
8080
this.offset = 5
8181
this.headerPosition = 0
82-
this.buffer = Buffer.allocUnsafe(this.size)
82+
this.buffer = Buffer.allocUnsafeSlow(this.size)
8383
return result
8484
}
8585
}

packages/pg-protocol/src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class Parser {
134134
while (newLength >= newBufferLength) {
135135
newBufferLength *= 2
136136
}
137-
newBuffer = Buffer.allocUnsafe(newBufferLength)
137+
newBuffer = Buffer.allocUnsafeSlow(newBufferLength)
138138
}
139139
// Move the remaining buffer to the new one
140140
this.buffer.copy(newBuffer, 0, this.bufferOffset, this.bufferOffset + this.bufferLength)

packages/pg-protocol/src/serializer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const startup = (opts: Record<string, string>): Buffer => {
3636
}
3737

3838
const requestSsl = (): Buffer => {
39-
const response = Buffer.allocUnsafe(8)
39+
const response = Buffer.allocUnsafeSlow(8)
4040
response.writeInt32BE(8, 0)
4141
response.writeInt32BE(80877103, 4)
4242
return response
@@ -183,7 +183,7 @@ const execute = (config?: ExecOpts): Buffer => {
183183
const portalLength = Buffer.byteLength(portal)
184184
const len = 4 + portalLength + 1 + 4
185185
// one extra bit for code
186-
const buff = Buffer.allocUnsafe(1 + len)
186+
const buff = Buffer.allocUnsafeSlow(1 + len)
187187
buff[0] = code.execute
188188
buff.writeInt32BE(len, 1)
189189
buff.write(portal, 5, 'utf-8')
@@ -193,7 +193,7 @@ const execute = (config?: ExecOpts): Buffer => {
193193
}
194194

195195
const cancel = (processID: number, secretKey: number): Buffer => {
196-
const buffer = Buffer.allocUnsafe(16)
196+
const buffer = Buffer.allocUnsafeSlow(16)
197197
buffer.writeInt32BE(16, 0)
198198
buffer.writeInt16BE(1234, 4)
199199
buffer.writeInt16BE(5678, 6)
@@ -211,7 +211,7 @@ const cstringMessage = (code: code, string: string): Buffer => {
211211
const stringLen = Buffer.byteLength(string)
212212
const len = 4 + stringLen + 1
213213
// one extra bit for code
214-
const buffer = Buffer.allocUnsafe(1 + len)
214+
const buffer = Buffer.allocUnsafeSlow(1 + len)
215215
buffer[0] = code
216216
buffer.writeInt32BE(len, 1)
217217
buffer.write(string, 5, 'utf-8')

packages/pg/lib/connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Connection extends EventEmitter {
4040

4141
this._connecting = true
4242
this.stream.setNoDelay(true)
43-
this.stream.connect(port, host)
43+
this.stream.connect({port, host})
4444

4545
this.stream.once('connect', function () {
4646
if (self._keepAlive) {

packages/pg/lib/stream.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module.exports = {
2020
function getNodejsStreamFuncs() {
2121
function getStream(ssl) {
2222
const net = require('net')
23-
return new net.Socket()
23+
const socket = new net.Socket({onread:{buffer: Buffer.allocUnsafeSlow(2048), callback: (b, n)=>socket.emit('data', b.slice(0, n))}})
24+
return socket
2425
}
2526

2627
function getSecureStream(options) {

0 commit comments

Comments
 (0)