-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
Changes Remove duplicated code from result
@@ -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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, whileBuffer.allocUnsafe(size).fill(fill)
will use the internal Buffer pool if size is less than or equal to halfBuffer.poolSize
. The difference is subtle but can be important when an application requires the additional performance that Buffer.allocUnsafe() provides.
Move from 3 loops (prepareValue, check for buffers, write param types, write param values) to a single loop. This speeds up the insert benchmark by around 100 queries per second. Performance improvement depends on number of parameters being bound.