Skip to content

Commit

Permalink
fix: improved performance of Pipeline.exec (#991)
Browse files Browse the repository at this point in the history
NOTE:  @flunderpero improved performance of Pipeline.exec, reset buffers and made code more readable

Motivation for the change:

```
Execution cost of Pipeline.exec can be reduced to 50% or lower. This is
accomplished by first filling up an Array of Buffers and then calling Buffer.concat
instead of calling Buffer.concat for each command.
In our use-case (~3000 commands in a single Pipeline) wall-clock-runtime went
down from 1.3sec to 500ms.
```
  • Loading branch information
flunderpero authored and AVVS committed Nov 22, 2019
1 parent 5333468 commit 86470a8
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ Pipeline.prototype.exec = function(callback: CallbackFunction) {
.then(execPipeline);

function execPipeline() {
let data: Buffer | string = "";
let data = "";
let buffers: Buffer[];
let writePending: number = (_this.replyPending = _this._queue.length);

let node;
Expand All @@ -319,25 +320,38 @@ Pipeline.prototype.exec = function(callback: CallbackFunction) {
bufferMode = true;
}
if (bufferMode) {
data = Buffer.concat([
typeof data === "string" ? Buffer.from(data, "utf8") : data,
if (!buffers) {
buffers = [];
}
if (typeof data === "string") {
buffers.push(Buffer.from(data, "utf8"));
data = undefined;
}
buffers.push(
typeof writable === "string"
? Buffer.from(writable, "utf8")
: writable
]);
);
} else {
data += writable;
}
if (!--writePending) {
let sendData: Buffer | string;
if (buffers) {
sendData = Buffer.concat(buffers);
} else {
sendData = data;
}
if (_this.isCluster) {
node.redis.stream.write(data);
node.redis.stream.write(sendData);
} else {
_this.redis.stream.write(data);
_this.redis.stream.write(sendData);
}

// Reset writePending for resending
writePending = _this._queue.length;
data = "";
buffers = undefined;
bufferMode = false;
}
}
Expand Down

0 comments on commit 86470a8

Please sign in to comment.