Skip to content

Commit

Permalink
perf: cork socket for a micro task
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Apr 1, 2024
1 parent 2405c17 commit 8f35bed
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class Sender {
}

this._socket = socket;
this._corked = false;
this._uncork = () => {
if (this._corked) {
this._socket.uncork();
}
}

Check failure on line 42 in lib/sender.js

View workflow job for this annotation

GitHub Actions / test (x64, 20, ubuntu-latest)

Insert `;`

this._firstFragment = true;
this._compress = false;
Expand Down Expand Up @@ -195,6 +201,10 @@ class Sender {
} else {
this.sendFrame(Sender.frame(buf, options), cb);
}

if (this._corked) {
this._socket.uncork();
}
}

/**
Expand Down Expand Up @@ -463,11 +473,15 @@ class Sender {
* @private
*/
sendFrame(list, cb) {
if (list.length === 2) {
if (!this._corked) {
this._corked = true;
this._socket.cork();
queueMicrotask(this._uncork);
}

if (list.length === 2) {
this._socket.write(list[0]);
this._socket.write(list[1], cb);
this._socket.uncork();
} else {
this._socket.write(list[0], cb);
}
Expand Down

0 comments on commit 8f35bed

Please sign in to comment.