Skip to content

Commit 47bc2e7

Browse files
committed
chore: use Buffer.allocUnsafe if available
This prevents a parser performance regression with Node.js v.8
1 parent d90fd06 commit 47bc2e7

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

changelog.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v.2.6.0 - 03 Apr, 2017
2+
3+
Internals
4+
5+
- Use Buffer.allocUnsafe instead of new Buffer() with modern Node.js versions
6+
17
## v.2.5.0 - 11 Mar, 2017
28

39
Features
@@ -13,7 +19,7 @@ Bugfixes
1319

1420
Bugfixes
1521

16-
- Fixed minimal memory consumtion overhead for chunked buffers
22+
- Fixed minimal memory consumption overhead for chunked buffers
1723

1824
## v.2.4.0 - 25 Jan, 2017
1925

@@ -94,7 +100,7 @@ Bugfixes
94100

95101
## v.2.0.0 - 29 May, 2016
96102

97-
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.
103+
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.
98104
Therefore the hiredis parser was deprecated and should only be used for testing purposes and benchmarking comparison.
99105

100106
All Errors returned by the parser are from now on of class ReplyError

lib/parser.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@ var StringDecoder = require('string_decoder').StringDecoder
44
var decoder = new StringDecoder()
55
var ReplyError = require('./replyError')
66
var ParserError = require('./parserError')
7-
var bufferPool = new Buffer(32 * 1024)
7+
var bufferPool = bufferAlloc(32 * 1024)
88
var bufferOffset = 0
99
var interval = null
1010
var counter = 0
1111
var notDecreased = 0
12+
var isModern = typeof Buffer.allocUnsafe === 'function'
13+
14+
/**
15+
* For backwards compatibility
16+
* @param len
17+
* @returns {Buffer}
18+
*/
19+
20+
function bufferAlloc (len) {
21+
return isModern ? Buffer.allocUnsafe(len) : new Buffer(len)
22+
}
1223

1324
/**
1425
* Used for lengths and numbers only, faster perf on arrays / bulks
@@ -428,7 +439,7 @@ function resizeBuffer (length) {
428439
if (bufferOffset > 1024 * 1024 * 111) {
429440
bufferOffset = 1024 * 1024 * 50
430441
}
431-
bufferPool = new Buffer(length * multiplier + bufferOffset)
442+
bufferPool = bufferAlloc(length * multiplier + bufferOffset)
432443
bufferOffset = 0
433444
counter++
434445
if (interval === null) {
@@ -513,7 +524,7 @@ JavascriptRedisParser.prototype.execute = function execute (buffer) {
513524
} else if (this.bigStrSize === 0) {
514525
var oldLength = this.buffer.length
515526
var remainingLength = oldLength - this.offset
516-
var newBuffer = new Buffer(remainingLength + buffer.length)
527+
var newBuffer = bufferAlloc(remainingLength + buffer.length)
517528
this.buffer.copy(newBuffer, 0, this.offset, oldLength)
518529
buffer.copy(newBuffer, remainingLength, 0, buffer.length)
519530
this.buffer = newBuffer

0 commit comments

Comments
 (0)