Skip to content

Commit

Permalink
Some small parser changes
Browse files Browse the repository at this point in the history
The small_to_string was actually quite slow
  • Loading branch information
Ruben Bridgewater committed Sep 21, 2015
1 parent 6958c18 commit b900bd6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 32 deletions.
4 changes: 1 addition & 3 deletions index.js
Expand Up @@ -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) {
Expand Down
13 changes: 6 additions & 7 deletions lib/parser/hiredis.js
Expand Up @@ -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
});
};

Expand All @@ -35,3 +31,6 @@ HiredisReplyParser.prototype.execute = function (data) {
}
}
};

exports.Parser = HiredisReplyParser;
exports.name = "hiredis";
29 changes: 7 additions & 22 deletions lib/parser/javascript.js
Expand Up @@ -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;

Expand All @@ -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) { // :
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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";

0 comments on commit b900bd6

Please sign in to comment.