1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

JavaScript parser passes all tests when returning strings.

JS is still way too slow for large mb replies.  Hiredis is fast for strings of large  replies,
but slow for buffers.
This commit is contained in:
Matt Ranney
2010-12-07 00:23:31 -08:00
parent b907364573
commit 36c40ee03d
3 changed files with 84 additions and 36 deletions

View File

@@ -18,10 +18,10 @@ util.inherits(RedisReplyParser, events.EventEmitter);
exports.Parser = RedisReplyParser;
// Buffer.toString() is quite slow for small strings
function small_toString(buf) {
var tmp = "", i, il;
function small_toString(buf, len) {
var tmp = "", i;
for (i = 0, il = buf.end; i < il; i += 1) {
for (i = 0; i < len; i += 1) {
tmp += String.fromCharCode(buf[i]);
}
@@ -33,7 +33,8 @@ RedisReplyParser.prototype.reset = function () {
this.state = "type";
this.return_buffer = new Buffer(16384); // for holding replies, might grow
this.tmp_buffer = new Buffer(128); // for holding size fields
this.return_string = "";
this.tmp_string = ""; // for holding size fields
this.multi_bulk_length = 0;
this.multi_bulk_replies = null;
@@ -64,22 +65,25 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
case 43: // +
this.state = "single line";
this.return_buffer.end = 0;
this.return_string = "";
break;
case 42: // *
this.state = "multi bulk count";
this.tmp_buffer.end = 0;
this.tmp_string = "";
break;
case 58: // :
this.state = "integer line";
this.return_buffer.end = 0;
this.return_string = "";
break;
case 36: // $
this.state = "bulk length";
this.tmp_buffer.end = 0;
this.tmp_string = "";
break;
case 45: // -
this.state = "error line";
this.return_buffer.end = 0;
this.return_string = "";
break;
default:
this.state = "unknown type";
@@ -87,7 +91,7 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
break;
case "integer line":
if (incoming_buf[pos] === 13) {
this.send_reply(+small_toString(this.return_buffer));
this.send_reply(+small_toString(this.return_buffer, this.return_buffer.end));
this.state = "final lf";
} else {
this.return_buffer[this.return_buffer.end] = incoming_buf[pos];
@@ -107,18 +111,10 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
break;
case "single line":
if (incoming_buf[pos] === 13) {
if (this.return_buffer.end > 10) {
bd_str = this.return_buffer.toString("utf8", 0, this.return_buffer.end);
} else {
bd_str = small_toString(this.return_buffer);
}
this.send_reply(bd_str);
this.send_reply(this.return_string);
this.state = "final lf";
} else {
this.return_buffer[this.return_buffer.end] = incoming_buf[pos];
this.return_buffer.end += 1;
// TODO - check for return_buffer overflow and then grow, copy, continue, and drink.
this.return_string += String.fromCharCode(incoming_buf[pos]);
}
pos += 1;
break;
@@ -126,8 +122,7 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
if (incoming_buf[pos] === 13) { // \r
this.state = "multi bulk count lf";
} else {
this.tmp_buffer[this.tmp_buffer.end] = incoming_buf[pos];
this.tmp_buffer.end += 1;
this.tmp_string += String.fromCharCode(incoming_buf[pos]);
}
pos += 1;
break;
@@ -137,7 +132,7 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
this.multi_bulk_nested_length = this.multi_bulk_length;
this.multi_bulk_nested_replies = this.multi_bulk_replies;
}
this.multi_bulk_length = +small_toString(this.tmp_buffer);
this.multi_bulk_length = +this.tmp_string;
this.multi_bulk_replies = [];
this.state = "type";
if (this.multi_bulk_length < 0) {
@@ -157,14 +152,13 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
if (incoming_buf[pos] === 13) { // \r
this.state = "bulk lf";
} else {
this.tmp_buffer[this.tmp_buffer.end] = incoming_buf[pos];
this.tmp_buffer.end += 1;
this.tmp_string += String.fromCharCode(incoming_buf[pos]);
}
pos += 1;
break;
case "bulk lf":
if (incoming_buf[pos] === 10) { // \n
this.bulk_length = +small_toString(this.tmp_buffer);
this.bulk_length = +this.tmp_string;
if (this.bulk_length === -1) {
this.send_reply(null);
this.state = "type";
@@ -251,22 +245,13 @@ RedisReplyParser.prototype.send_error = function (reply) {
RedisReplyParser.prototype.send_reply = function (reply) {
if (this.multi_bulk_length > 0 || this.multi_bulk_nested_length > 0) {
if (!this.options.return_buffers && Buffer.isBuffer(reply)) {
if (reply.end > 10) {
this.add_multi_bulk_reply(reply.toString());
} else {
this.add_multi_bulk_reply(small_toString(reply));
}
this.add_multi_bulk_reply(reply.toString("utf8"));
} else {
this.add_multi_bulk_reply(reply);
}
} else {
if (!this.options.return_buffers && Buffer.isBuffer(reply)) {
console.log("converting buffer to string of len " + reply.end + ", " + util.inspect(reply));
if (reply.length > 10) {
this.emit("reply", reply.toString());
} else {
this.emit("reply", small_toString(reply));
}
this.emit("reply", reply.toString("utf8"));
} else {
this.emit("reply", reply);
}