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

Performance improvements for small responses.

This commit is contained in:
Matt Ranney
2010-09-17 02:03:56 -07:00
parent d7c9a010eb
commit ff03903c5f

View File

@@ -18,7 +18,7 @@ function RedisReplyParser() {
sys.inherits(RedisReplyParser, events.EventEmitter); sys.inherits(RedisReplyParser, events.EventEmitter);
RedisReplyParser.prototype.execute = function (incoming_buf) { RedisReplyParser.prototype.execute = function (incoming_buf) {
var pos = 0, state_times = {}; var pos = 0, state_times = {}, bd_tmp, bd_str, i;
//, start_execute = new Date(), start_switch, end_switch, old_state; //, start_execute = new Date(), start_switch, end_switch, old_state;
//start_switch = new Date(); //start_switch = new Date();
@@ -57,7 +57,11 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
break; break;
case "integer line": case "integer line":
if (incoming_buf[pos] === 13) { if (incoming_buf[pos] === 13) {
this.send_reply(parseInt(this.return_buffer.slice(0, this.return_buffer.end) ,10)); bd_str = "";
for (i = 0 ; i < this.return_buffer.end ; i += 1) {
bd_str += String.fromCharCode(this.return_buffer[i]);
}
this.send_reply(parseInt(bd_str, 10));
this.state = "final lf"; this.state = "final lf";
} else { } else {
this.return_buffer[this.return_buffer.end] = incoming_buf[pos]; this.return_buffer[this.return_buffer.end] = incoming_buf[pos];
@@ -98,7 +102,11 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
break; break;
case "multi bulk count lf": case "multi bulk count lf":
if (incoming_buf[pos] === 10) { // \n if (incoming_buf[pos] === 10) { // \n
this.multi_bulk_length = parseInt(this.tmp_buffer.toString("ascii", 0, this.tmp_buffer.end), 10); bd_str = "";
for (i = 0 ; i < this.tmp_buffer.end ; i += 1) {
bd_str += String.fromCharCode(this.tmp_buffer[i]);
}
this.multi_bulk_length = parseInt(bd_str, 10);
this.multi_bulk_replies = []; this.multi_bulk_replies = [];
this.state = "type"; this.state = "type";
} else { } else {
@@ -119,7 +127,11 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
break; break;
case "bulk lf": case "bulk lf":
if (incoming_buf[pos] === 10) { // \n if (incoming_buf[pos] === 10) { // \n
this.bulk_length = parseInt(this.tmp_buffer.toString("ascii", 0, this.tmp_buffer.end), 10); bd_str = "";
for (i = 0 ; i < this.tmp_buffer.end ; i += 1) {
bd_str += String.fromCharCode(this.tmp_buffer[i]);
}
this.bulk_length = parseInt(bd_str, 10);
if (this.bulk_length === -1) { if (this.bulk_length === -1) {
this.send_reply(null); this.send_reply(null);
this.state = "type"; this.state = "type";
@@ -145,9 +157,14 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
// TODO - should be faster to use Bufer.copy() here, especially if the response is large. // TODO - should be faster to use Bufer.copy() here, especially if the response is large.
// However, when the response is small, Buffer.copy() seems a lot slower. Computers are hard. // However, when the response is small, Buffer.copy() seems a lot slower. Computers are hard.
if (this.return_buffer.end === this.bulk_length) { if (this.return_buffer.end === this.bulk_length) {
// this ugilness could go away if we gave the user a volatile buffer, but that seems dangerous bd_tmp = new Buffer(this.bulk_length);
var bd_tmp = new Buffer(this.bulk_length); if (this.bulk_length > 10) {
this.return_buffer.copy(bd_tmp, 0, 0, this.bulk_length); this.return_buffer.copy(bd_tmp, 0, 0, this.bulk_length);
} else {
for (i = this.bulk_length - 1; i >= 0 ; i -= 1) {
bd_tmp[i] = this.return_buffer[i];
}
}
this.send_reply(bd_tmp); this.send_reply(bd_tmp);
this.state = "final cr"; this.state = "final cr";
} }