Skip to content

Commit

Permalink
chore: use Buffer.allocUnsafe if available
Browse files Browse the repository at this point in the history
This prevents a parser performance regression with Node.js v.8
  • Loading branch information
BridgeAR committed Apr 3, 2017
1 parent d90fd06 commit 47bc2e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
10 changes: 8 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v.2.6.0 - 03 Apr, 2017

Internals

- Use Buffer.allocUnsafe instead of new Buffer() with modern Node.js versions

## v.2.5.0 - 11 Mar, 2017

Features
Expand All @@ -13,7 +19,7 @@ Bugfixes

Bugfixes

- Fixed minimal memory consumtion overhead for chunked buffers
- Fixed minimal memory consumption overhead for chunked buffers

## v.2.4.0 - 25 Jan, 2017

Expand Down Expand Up @@ -94,7 +100,7 @@ Bugfixes

## v.2.0.0 - 29 May, 2016

The javascript parser got completly rewritten by [Michael Diarmid](https://github.com/Salakar) and [Ruben Bridgewater](https://github.com/BridgeAR) and is now a lot faster than the hiredis parser.
The javascript parser got completely rewritten by [Michael Diarmid](https://github.com/Salakar) and [Ruben Bridgewater](https://github.com/BridgeAR) and is now a lot faster than the hiredis parser.
Therefore the hiredis parser was deprecated and should only be used for testing purposes and benchmarking comparison.

All Errors returned by the parser are from now on of class ReplyError
Expand Down
17 changes: 14 additions & 3 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ var StringDecoder = require('string_decoder').StringDecoder
var decoder = new StringDecoder()
var ReplyError = require('./replyError')
var ParserError = require('./parserError')
var bufferPool = new Buffer(32 * 1024)
var bufferPool = bufferAlloc(32 * 1024)
var bufferOffset = 0
var interval = null
var counter = 0
var notDecreased = 0
var isModern = typeof Buffer.allocUnsafe === 'function'

/**
* For backwards compatibility
* @param len
* @returns {Buffer}
*/

function bufferAlloc (len) {
return isModern ? Buffer.allocUnsafe(len) : new Buffer(len)
}

/**
* Used for lengths and numbers only, faster perf on arrays / bulks
Expand Down Expand Up @@ -428,7 +439,7 @@ function resizeBuffer (length) {
if (bufferOffset > 1024 * 1024 * 111) {
bufferOffset = 1024 * 1024 * 50
}
bufferPool = new Buffer(length * multiplier + bufferOffset)
bufferPool = bufferAlloc(length * multiplier + bufferOffset)
bufferOffset = 0
counter++
if (interval === null) {
Expand Down Expand Up @@ -513,7 +524,7 @@ JavascriptRedisParser.prototype.execute = function execute (buffer) {
} else if (this.bigStrSize === 0) {
var oldLength = this.buffer.length
var remainingLength = oldLength - this.offset
var newBuffer = new Buffer(remainingLength + buffer.length)
var newBuffer = bufferAlloc(remainingLength + buffer.length)
this.buffer.copy(newBuffer, 0, this.offset, oldLength)
buffer.copy(newBuffer, remainingLength, 0, buffer.length)
this.buffer = newBuffer
Expand Down

0 comments on commit 47bc2e7

Please sign in to comment.