You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Clean up small Buffer optimizations.
This commit is contained in:
38
index.js
38
index.js
@@ -17,6 +17,16 @@ function RedisReplyParser() {
|
|||||||
}
|
}
|
||||||
sys.inherits(RedisReplyParser, events.EventEmitter);
|
sys.inherits(RedisReplyParser, events.EventEmitter);
|
||||||
|
|
||||||
|
function small_toString(buf) {
|
||||||
|
var tmp = "", i = 0, end = buf.end;
|
||||||
|
|
||||||
|
while (i < end) {
|
||||||
|
tmp += String.fromCharCode(buf[i]);
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
RedisReplyParser.prototype.execute = function (incoming_buf) {
|
RedisReplyParser.prototype.execute = function (incoming_buf) {
|
||||||
var pos = 0, state_times = {}, bd_tmp, bd_str, i;
|
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;
|
||||||
@@ -57,11 +67,7 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
|
|||||||
break;
|
break;
|
||||||
case "integer line":
|
case "integer line":
|
||||||
if (incoming_buf[pos] === 13) {
|
if (incoming_buf[pos] === 13) {
|
||||||
bd_str = "";
|
this.send_reply(parseInt(small_toString(this.return_buffer), 10));
|
||||||
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];
|
||||||
@@ -82,7 +88,13 @@ RedisReplyParser.prototype.execute = function (incoming_buf) {
|
|||||||
break;
|
break;
|
||||||
case "single line":
|
case "single line":
|
||||||
if (incoming_buf[pos] === 13) {
|
if (incoming_buf[pos] === 13) {
|
||||||
this.send_reply(this.return_buffer.toString("utf8", 0, this.return_buffer.end));
|
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.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];
|
||||||
@@ -102,11 +114,7 @@ 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
|
||||||
bd_str = "";
|
this.multi_bulk_length = parseInt(small_toString(this.tmp_buffer), 10);
|
||||||
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 {
|
||||||
@@ -127,11 +135,7 @@ 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
|
||||||
bd_str = "";
|
this.bulk_length = parseInt(small_toString(this.tmp_buffer), 10);
|
||||||
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";
|
||||||
@@ -255,12 +259,14 @@ Queue.prototype.push = function (item) {
|
|||||||
return this.tail.push(item);
|
return this.tail.push(item);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Thanks Tim-Smart
|
||||||
Queue.prototype.forEach = function () {
|
Queue.prototype.forEach = function () {
|
||||||
var array = this.head.slice(this.offset);
|
var array = this.head.slice(this.offset);
|
||||||
array.push.apply(array, this.tail);
|
array.push.apply(array, this.tail);
|
||||||
return array.forEach.apply(array, arguments);
|
return array.forEach.apply(array, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Thanks Tim-Smart
|
||||||
Object.defineProperty(Queue.prototype, 'length', {
|
Object.defineProperty(Queue.prototype, 'length', {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this.head.length - this.offset + this.tail.length;
|
return this.head.length - this.offset + this.tail.length;
|
||||||
|
Reference in New Issue
Block a user