1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Some small parser changes

The small_to_string was actually quite slow
This commit is contained in:
Ruben Bridgewater
2015-09-21 02:40:07 +02:00
parent 6958c1854b
commit b900bd697f
3 changed files with 14 additions and 32 deletions

View File

@@ -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) {

View File

@@ -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";

View File

@@ -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";