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

Allocate new Arrays with known size instead of dynamically growing them.

This commit is contained in:
Matt Ranney
2010-12-13 15:55:30 -08:00
parent 202df58b93
commit aa3cefe506
2 changed files with 19 additions and 7 deletions

View File

@@ -43,6 +43,7 @@ RedisReplyParser.prototype.reset = function () {
this.multi_bulk_length = 0; this.multi_bulk_length = 0;
this.multi_bulk_replies = null; this.multi_bulk_replies = null;
this.multi_bulk_pos = 0;
this.multi_bulk_nested_length = 0; this.multi_bulk_nested_length = 0;
this.multi_bulk_nested_replies = null; this.multi_bulk_nested_replies = null;
}; };
@@ -136,16 +137,20 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
if (this.multi_bulk_length) { // nested multi-bulk if (this.multi_bulk_length) { // nested multi-bulk
this.multi_bulk_nested_length = this.multi_bulk_length; this.multi_bulk_nested_length = this.multi_bulk_length;
this.multi_bulk_nested_replies = this.multi_bulk_replies; this.multi_bulk_nested_replies = this.multi_bulk_replies;
this.multi_bulk_nested_pos = this.multi_bulk_pos;
} }
this.multi_bulk_length = +this.tmp_string; this.multi_bulk_length = +this.tmp_string;
this.multi_bulk_replies = []; this.multi_bulk_pos = 0;
this.state = "type"; this.state = "type";
if (this.multi_bulk_length < 0) { if (this.multi_bulk_length < 0) {
this.send_reply(null); this.send_reply(null);
this.multi_bulk_length = 0; this.multi_bulk_length = 0;
} else if (this.multi_bulk_length === 0) { } else if (this.multi_bulk_length === 0) {
this.multi_bulk_pos = 0;
this.multi_bulk_replies = null; this.multi_bulk_replies = null;
this.send_reply([]); this.send_reply([]);
} else {
this.multi_bulk_replies = new Array(this.multi_bulk_length);
} }
} else { } else {
this.parser_error(new Error("didn't see LF after NL reading multi bulk count")); this.parser_error(new Error("didn't see LF after NL reading multi bulk count"));
@@ -265,8 +270,9 @@ RedisReplyParser.prototype.send_reply = function (reply) {
RedisReplyParser.prototype.add_multi_bulk_reply = function (reply) { RedisReplyParser.prototype.add_multi_bulk_reply = function (reply) {
if (this.multi_bulk_replies) { if (this.multi_bulk_replies) {
this.multi_bulk_replies.push(reply); this.multi_bulk_replies[this.multi_bulk_pos] = reply;
if (this.multi_bulk_replies.length < this.multi_bulk_length) { this.multi_bulk_pos += 1;
if (this.multi_bulk_pos < this.multi_bulk_length) {
return; return;
} }
} else { } else {
@@ -274,17 +280,23 @@ RedisReplyParser.prototype.add_multi_bulk_reply = function (reply) {
} }
if (this.multi_bulk_nested_length > 0) { if (this.multi_bulk_nested_length > 0) {
this.multi_bulk_nested_replies.push(this.multi_bulk_replies); this.multi_bulk_nested_replies[this.multi_bulk_nested_pos] = this.multi_bulk_replies;
this.multi_bulk_nested_pos += 1;
this.multi_bulk_length = 0; this.multi_bulk_length = 0;
delete this.multi_bulk_replies; this.multi_bulk_replies = null;
if (this.multi_bulk_nested_length === this.multi_bulk_nested_replies.length) { this.multi_bulk_pos = 0;
if (this.multi_bulk_nested_length === this.multi_bulk_nested_pos) {
this.emit("reply", this.multi_bulk_nested_replies); this.emit("reply", this.multi_bulk_nested_replies);
this.multi_bulk_nested_length = 0; this.multi_bulk_nested_length = 0;
this.multi_bulk_nested_pos = 0;
this.multi_bulk_nested_replies = null; this.multi_bulk_nested_replies = null;
} }
} else { } else {
this.emit("reply", this.multi_bulk_replies); this.emit("reply", this.multi_bulk_replies);
this.multi_bulk_length = 0; this.multi_bulk_length = 0;
this.multi_bulk_replies = null; this.multi_bulk_replies = null;
this.multi_bulk_pos = 0;
} }
}; };

View File

@@ -1,5 +1,5 @@
{ "name" : "redis", { "name" : "redis",
"version" : "0.4.1", "version" : "0.4.2",
"description" : "Redis client library", "description" : "Redis client library",
"author": "Matt Ranney <mjr@ranney.com>", "author": "Matt Ranney <mjr@ranney.com>",
"contributors": [ "contributors": [