Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up bind functionality #2286

Merged
merged 8 commits into from Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/pg-protocol/src/buffer-writer.ts
Expand Up @@ -5,7 +5,7 @@ export class Writer {
private offset: number = 5
private headerPosition: number = 0
constructor(private size = 256) {
this.buffer = Buffer.alloc(size)
this.buffer = Buffer.allocUnsafe(size)
Copy link

Choose a reason for hiding this comment

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

Curious -- is changing to allocUnsafe here also contributes to performance improvements? The original PR comment doesn't mention anything about this change.
Also how certain we are that this doesn't cause any nasty side-effects of being, well, unsafe?

Choose a reason for hiding this comment

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

Here's a link to the NodeJS documentation on Buffer.allocUnsafe().

I'm not an expert on Buffers, though the documentation's last sentenance says this can increase a performance (subtle) over Buffer.alloc().

Allocates a new Buffer of size bytes. If size is larger than buffer.constants.MAX_LENGTH or smaller than 0, ERR_INVALID_OPT_VALUE is thrown.

The underlying memory for Buffer instances created in this way is not initialized. The contents of the newly created Buffer are unknown and may contain sensitive data. Use Buffer.alloc() instead to initialize Buffer instances with zeroes.

[...]

Use of this pre-allocated internal memory pool is a key difference between calling Buffer.alloc(size, fill) vs. Buffer.allocUnsafe(size).fill(fill). Specifically, Buffer.alloc(size, fill) will never use the internal Buffer pool, while Buffer.allocUnsafe(size).fill(fill) will use the internal Buffer pool if size is less than or equal to half Buffer.poolSize. The difference is subtle but can be important when an application requires the additional performance that Buffer.allocUnsafe() provides.

}

private ensure(size: number): void {
Expand All @@ -15,7 +15,7 @@ export class Writer {
// exponential growth factor of around ~ 1.5
// https://stackoverflow.com/questions/2269063/buffer-growth-strategy
var newSize = oldBuffer.length + (oldBuffer.length >> 1) + size
this.buffer = Buffer.alloc(newSize)
this.buffer = Buffer.allocUnsafe(newSize)
oldBuffer.copy(this.buffer)
}
}
Expand Down
67 changes: 35 additions & 32 deletions packages/pg-protocol/src/serializer.ts
Expand Up @@ -106,50 +106,53 @@ type BindOpts = {
binary?: boolean
statement?: string
values?: any[]
// optional map from JS value to postgres value per parameter
valueMap?: (param: any) => any
brianc marked this conversation as resolved.
Show resolved Hide resolved
}

const bind = (config: BindOpts = {}): Buffer => {
// normalize config
const portal = config.portal || ''
const statement = config.statement || ''
const binary = config.binary || false
var values = config.values || emptyArray
var len = values.length
const paramWriter = new Writer()

var useBinary = false
// TODO(bmc): all the loops in here aren't nice, we can do better
for (var j = 0; j < len; j++) {
useBinary = useBinary || values[j] instanceof Buffer
}

var buffer = writer.addCString(portal).addCString(statement)
if (!useBinary) {
buffer.addInt16(0)
} else {
buffer.addInt16(len)
for (j = 0; j < len; j++) {
buffer.addInt16(values[j] instanceof Buffer ? 1 : 0)
}
}
buffer.addInt16(len)
for (var i = 0; i < len; i++) {
const writeValues = function (values: any[], valueMap?: (val: any) => any): void {
for (let i = 0; i < values.length; i++) {
var val = values[i]
if (val === null || typeof val === 'undefined') {
buffer.addInt32(-1)
writer.addInt16(0)
paramWriter.addInt32(-1)
} else if (val instanceof Buffer) {
buffer.addInt32(val.length)
buffer.add(val)
writer.addInt16(1)
const mappedVal = valueMap ? valueMap(val) : val
paramWriter.addInt32(mappedVal.length)
paramWriter.add(mappedVal)
} else {
buffer.addInt32(Buffer.byteLength(val))
buffer.addString(val)
writer.addInt16(0)
const mappedVal = valueMap ? valueMap(val) : val
paramWriter.addInt32(Buffer.byteLength(mappedVal))
paramWriter.addString(mappedVal)
brianc marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

const bind = (config: BindOpts = {}): Buffer => {
// normalize config
const portal = config.portal || ''
const statement = config.statement || ''
const binary = config.binary || false
const values = config.values || emptyArray
const len = values.length

writer.addCString(portal).addCString(statement)
writer.addInt16(len)

writeValues(values, config.valueMap)

writer.addInt16(len)
writer.add(paramWriter.flush())

if (binary) {
buffer.addInt16(1) // format codes to use binary
buffer.addInt16(1)
writer.addInt16(1) // format codes to use binary
writer.addInt16(1)
} else {
buffer.addInt16(0) // format codes to use text
writer.addInt16(0) // format codes to use text
}
return writer.flush(code.bind)
}
Expand Down