You've already forked node-redis
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:
4
index.js
4
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
|
// 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.
|
// converts to Strings if the input arguments are not Buffers.
|
||||||
this.reply_parser = new this.parser_module.Parser({
|
this.reply_parser = new this.parser_module.Parser(self.options.return_buffers || self.options.detect_buffers || false);
|
||||||
return_buffers: self.options.return_buffers || self.options.detect_buffers || false
|
|
||||||
});
|
|
||||||
// Important: Only send results / errors async.
|
// Important: Only send results / errors async.
|
||||||
// That way the result / error won't stay in a try catch block and catch user things
|
// That way the result / error won't stay in a try catch block and catch user things
|
||||||
this.reply_parser.send_error = function (data) {
|
this.reply_parser.send_error = function (data) {
|
||||||
|
@@ -2,19 +2,15 @@
|
|||||||
|
|
||||||
var hiredis = require("hiredis");
|
var hiredis = require("hiredis");
|
||||||
|
|
||||||
exports.name = "hiredis";
|
function HiredisReplyParser(return_buffers) {
|
||||||
|
|
||||||
function HiredisReplyParser(options) {
|
|
||||||
this.name = exports.name;
|
this.name = exports.name;
|
||||||
this.options = options;
|
this.return_buffers = return_buffers;
|
||||||
this.reset();
|
this.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.Parser = HiredisReplyParser;
|
|
||||||
|
|
||||||
HiredisReplyParser.prototype.reset = function () {
|
HiredisReplyParser.prototype.reset = function () {
|
||||||
this.reader = new hiredis.Reader({
|
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";
|
||||||
|
@@ -7,37 +7,21 @@ function Packet(type, size) {
|
|||||||
this.size = +size;
|
this.size = +size;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.name = "javascript";
|
function ReplyParser(return_buffers) {
|
||||||
|
|
||||||
function ReplyParser(options) {
|
|
||||||
this.name = exports.name;
|
this.name = exports.name;
|
||||||
this.options = options;
|
this.return_buffers = return_buffers;
|
||||||
|
|
||||||
this._buffer = null;
|
this._buffer = null;
|
||||||
this._offset = 0;
|
this._offset = 0;
|
||||||
this._encoding = "utf-8";
|
this._encoding = "utf-8";
|
||||||
this._reply_type = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.Parser = ReplyParser;
|
|
||||||
|
|
||||||
function IncompleteReadBuffer(message) {
|
function IncompleteReadBuffer(message) {
|
||||||
this.name = "IncompleteReadBuffer";
|
this.name = "IncompleteReadBuffer";
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
util.inherits(IncompleteReadBuffer, Error);
|
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) {
|
ReplyParser.prototype._parseResult = function (type) {
|
||||||
var start, end, offset, packetHeader;
|
var start, end, offset, packetHeader;
|
||||||
|
|
||||||
@@ -56,10 +40,8 @@ ReplyParser.prototype._parseResult = function (type) {
|
|||||||
|
|
||||||
if (type === 45) {
|
if (type === 45) {
|
||||||
return new Error(this._buffer.toString(this._encoding, start, end));
|
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);
|
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);
|
return this._buffer.toString(this._encoding, start, end);
|
||||||
} else if (type === 58) { // :
|
} else if (type === 58) { // :
|
||||||
@@ -100,7 +82,7 @@ ReplyParser.prototype._parseResult = function (type) {
|
|||||||
throw new IncompleteReadBuffer("Wait for more data.");
|
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.slice(start, end);
|
||||||
}
|
}
|
||||||
return this._buffer.toString(this._encoding, start, end);
|
return this._buffer.toString(this._encoding, start, end);
|
||||||
@@ -234,3 +216,6 @@ ReplyParser.prototype._packetEndOffset = function () {
|
|||||||
ReplyParser.prototype._bytesRemaining = function () {
|
ReplyParser.prototype._bytesRemaining = function () {
|
||||||
return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset);
|
return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.Parser = ReplyParser;
|
||||||
|
exports.name = "javascript";
|
||||||
|
Reference in New Issue
Block a user