Skip to content

Commit 2744fe8

Browse files
author
Ruben Bridgewater
committed
Optimize statements and speed up the common case
1 parent 2ca4241 commit 2744fe8

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ node_modules
22
.tern-port
33
.nyc_output
44
coverage
5-
npm-debug.log
5+
*.log
66
*.rdb

benchmarks/multi_bench.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var redis = require("../index");
77
var totalTime = 0;
88
var metrics = require("metrics");
99
var num_clients = parseInt(process.argv[2], 10) || 5;
10-
var num_requests = 20000;
10+
var num_requests = 50000;
1111
var tests = [];
1212
var versions_logged = false;
1313
var client_options = {

index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,14 @@ commands.forEach(function (fullCommand) {
851851
arg = [key].concat(arg);
852852
return this.send_command(command, arg, callback);
853853
}
854+
// Speed up the common case
855+
var len = arguments.length;
856+
if (len === 2) {
857+
return this.send_command(command, [key, arg]);
858+
}
859+
if (len === 3) {
860+
return this.send_command(command, [key, arg, callback]);
861+
}
854862
return this.send_command(command, utils.to_array(arguments));
855863
};
856864
RedisClient.prototype[command.toUpperCase()] = RedisClient.prototype[command];
@@ -867,7 +875,15 @@ commands.forEach(function (fullCommand) {
867875
}
868876
this.queue.push([command, key].concat(arg));
869877
} else {
870-
this.queue.push([command].concat(utils.to_array(arguments)));
878+
// Speed up the common case
879+
var len = arguments.length;
880+
if (len === 2) {
881+
this.queue.push([command, key, arg]);
882+
} else if (len === 3) {
883+
this.queue.push([command, key, arg, callback]);
884+
} else {
885+
this.queue.push([command].concat(utils.to_array(arguments)));
886+
}
871887
}
872888
return this;
873889
};
@@ -1102,9 +1118,7 @@ var createClient_tcp = function (port_arg, host_arg, options) {
11021118
exports.createClient = function(port_arg, host_arg, options) {
11031119
if (typeof port_arg === 'object' || port_arg === undefined) {
11041120
options = port_arg || options || {};
1105-
var host = options.host || default_host;
1106-
var port = +options.port || default_port;
1107-
return createClient_tcp(port, host, options);
1121+
return createClient_tcp(+options.port, options.host, options);
11081122
}
11091123
if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) {
11101124
return createClient_tcp(port_arg, host_arg, options);

lib/parsers/javascript.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function ReplyParser(return_buffers) {
1111
this.name = exports.name;
1212
this.return_buffers = return_buffers;
1313

14-
this._buffer = null;
14+
this._buffer = new Buffer(0);
1515
this._offset = 0;
1616
this._encoding = "utf-8";
1717
}
@@ -171,18 +171,10 @@ ReplyParser.prototype.execute = function (buffer) {
171171

172172
ReplyParser.prototype.append = function (newBuffer) {
173173

174-
// first run
175-
if (this._buffer === null) {
176-
this._buffer = newBuffer;
177-
178-
return;
179-
}
180-
181174
// out of data
182175
if (this._offset >= this._buffer.length) {
183176
this._buffer = newBuffer;
184177
this._offset = 0;
185-
186178
return;
187179
}
188180

0 commit comments

Comments
 (0)