Conversation
Changes Remove duplicated code from result
| 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.
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.
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.