From 47bc2e74e89fbc6e7ed5924fdb7a5c6c7b1959eb Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 3 Apr 2017 20:27:00 -0300 Subject: [PATCH] chore: use Buffer.allocUnsafe if available This prevents a parser performance regression with Node.js v.8 --- changelog.md | 10 ++++++++-- lib/parser.js | 17 ++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/changelog.md b/changelog.md index 06847fa..5cd9470 100644 --- a/changelog.md +++ b/changelog.md @@ -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 @@ -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 @@ -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 diff --git a/lib/parser.js b/lib/parser.js index ed134fb..f4a4005 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -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 @@ -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) { @@ -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