From 2744fe865027bc599fa184b07166872692b5e0a2 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Fri, 2 Oct 2015 20:21:13 +0200 Subject: [PATCH] Optimize statements and speed up the common case --- .gitignore | 2 +- benchmarks/multi_bench.js | 2 +- index.js | 22 ++++++++++++++++++---- lib/parsers/javascript.js | 10 +--------- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 2cf7258aa6..3730c72444 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ node_modules .tern-port .nyc_output coverage -npm-debug.log +*.log *.rdb diff --git a/benchmarks/multi_bench.js b/benchmarks/multi_bench.js index a1d470611a..05fcd2dc04 100644 --- a/benchmarks/multi_bench.js +++ b/benchmarks/multi_bench.js @@ -7,7 +7,7 @@ var redis = require("../index"); var totalTime = 0; var metrics = require("metrics"); var num_clients = parseInt(process.argv[2], 10) || 5; -var num_requests = 20000; +var num_requests = 50000; var tests = []; var versions_logged = false; var client_options = { diff --git a/index.js b/index.js index 4458b68da2..2f9a8885cb 100644 --- a/index.js +++ b/index.js @@ -851,6 +851,14 @@ commands.forEach(function (fullCommand) { arg = [key].concat(arg); return this.send_command(command, arg, callback); } + // Speed up the common case + var len = arguments.length; + if (len === 2) { + return this.send_command(command, [key, arg]); + } + if (len === 3) { + return this.send_command(command, [key, arg, callback]); + } return this.send_command(command, utils.to_array(arguments)); }; RedisClient.prototype[command.toUpperCase()] = RedisClient.prototype[command]; @@ -867,7 +875,15 @@ commands.forEach(function (fullCommand) { } this.queue.push([command, key].concat(arg)); } else { - this.queue.push([command].concat(utils.to_array(arguments))); + // Speed up the common case + var len = arguments.length; + if (len === 2) { + this.queue.push([command, key, arg]); + } else if (len === 3) { + this.queue.push([command, key, arg, callback]); + } else { + this.queue.push([command].concat(utils.to_array(arguments))); + } } return this; }; @@ -1102,9 +1118,7 @@ var createClient_tcp = function (port_arg, host_arg, options) { exports.createClient = function(port_arg, host_arg, options) { if (typeof port_arg === 'object' || port_arg === undefined) { options = port_arg || options || {}; - var host = options.host || default_host; - var port = +options.port || default_port; - return createClient_tcp(port, host, options); + return createClient_tcp(+options.port, options.host, options); } if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) { return createClient_tcp(port_arg, host_arg, options); diff --git a/lib/parsers/javascript.js b/lib/parsers/javascript.js index 029080ec29..4beda4841f 100644 --- a/lib/parsers/javascript.js +++ b/lib/parsers/javascript.js @@ -11,7 +11,7 @@ function ReplyParser(return_buffers) { this.name = exports.name; this.return_buffers = return_buffers; - this._buffer = null; + this._buffer = new Buffer(0); this._offset = 0; this._encoding = "utf-8"; } @@ -171,18 +171,10 @@ ReplyParser.prototype.execute = function (buffer) { ReplyParser.prototype.append = function (newBuffer) { - // first run - if (this._buffer === null) { - this._buffer = newBuffer; - - return; - } - // out of data if (this._offset >= this._buffer.length) { this._buffer = newBuffer; this._offset = 0; - return; }