From b900bd697fcf27a3564e57628dcbdbfb25c55ed8 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 21 Sep 2015 02:40:07 +0200 Subject: [PATCH] Some small parser changes The small_to_string was actually quite slow --- index.js | 4 +--- lib/parser/hiredis.js | 13 ++++++------- lib/parser/javascript.js | 29 +++++++---------------------- 3 files changed, 14 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index ac9a1759e01..cd3ccf6846a 100644 --- a/index.js +++ b/index.js @@ -271,9 +271,7 @@ RedisClient.prototype.init_parser = function () { // return_buffers sends back Buffers from parser to callback. detect_buffers sends back Buffers from parser, but // converts to Strings if the input arguments are not Buffers. - this.reply_parser = new this.parser_module.Parser({ - return_buffers: self.options.return_buffers || self.options.detect_buffers || false - }); + this.reply_parser = new this.parser_module.Parser(self.options.return_buffers || self.options.detect_buffers || false); // Important: Only send results / errors async. // That way the result / error won't stay in a try catch block and catch user things this.reply_parser.send_error = function (data) { diff --git a/lib/parser/hiredis.js b/lib/parser/hiredis.js index 57a1cfb4aba..765304bec53 100644 --- a/lib/parser/hiredis.js +++ b/lib/parser/hiredis.js @@ -2,19 +2,15 @@ var hiredis = require("hiredis"); -exports.name = "hiredis"; - -function HiredisReplyParser(options) { +function HiredisReplyParser(return_buffers) { this.name = exports.name; - this.options = options; + this.return_buffers = return_buffers; this.reset(); } -exports.Parser = HiredisReplyParser; - HiredisReplyParser.prototype.reset = function () { this.reader = new hiredis.Reader({ - return_buffers: this.options.return_buffers || false + return_buffers: this.return_buffers || false }); }; @@ -35,3 +31,6 @@ HiredisReplyParser.prototype.execute = function (data) { } } }; + +exports.Parser = HiredisReplyParser; +exports.name = "hiredis"; diff --git a/lib/parser/javascript.js b/lib/parser/javascript.js index d979ec3cbef..4ce4168c43f 100644 --- a/lib/parser/javascript.js +++ b/lib/parser/javascript.js @@ -7,37 +7,21 @@ function Packet(type, size) { this.size = +size; } -exports.name = "javascript"; - -function ReplyParser(options) { +function ReplyParser(return_buffers) { this.name = exports.name; - this.options = options; + this.return_buffers = return_buffers; this._buffer = null; this._offset = 0; this._encoding = "utf-8"; - this._reply_type = null; } -exports.Parser = ReplyParser; - function IncompleteReadBuffer(message) { this.name = "IncompleteReadBuffer"; this.message = message; } util.inherits(IncompleteReadBuffer, Error); -// Buffer.toString() is quite slow for small strings -function small_toString(buf, start, end) { - var tmp = "", i; - - for (i = start; i < end; i++) { - tmp += String.fromCharCode(buf[i]); - } - - return tmp; -} - ReplyParser.prototype._parseResult = function (type) { var start, end, offset, packetHeader; @@ -56,10 +40,8 @@ ReplyParser.prototype._parseResult = function (type) { if (type === 45) { return new Error(this._buffer.toString(this._encoding, start, end)); - } else if (this.options.return_buffers) { + } else if (this.return_buffers) { return this._buffer.slice(start, end); - } else if (end - start < 65536) { // completely arbitrary - return small_toString(this._buffer, start, end); } return this._buffer.toString(this._encoding, start, end); } else if (type === 58) { // : @@ -100,7 +82,7 @@ ReplyParser.prototype._parseResult = function (type) { throw new IncompleteReadBuffer("Wait for more data."); } - if (this.options.return_buffers) { + if (this.return_buffers) { return this._buffer.slice(start, end); } return this._buffer.toString(this._encoding, start, end); @@ -234,3 +216,6 @@ ReplyParser.prototype._packetEndOffset = function () { ReplyParser.prototype._bytesRemaining = function () { return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset); }; + +exports.Parser = ReplyParser; +exports.name = "javascript";