Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved performance of Pipeline.exec #991

Merged
merged 2 commits into from
Nov 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data is ignored if its already a buffer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data cannot be a Buffer. I cleaned up the code a bit to make this clear.
Your comment made me aware that buffers is actually never re-set, so I did that, too.

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