Skip to content

stream: improve read() performance #29337

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

Merged
merged 1 commit into from
Aug 31, 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
54 changes: 25 additions & 29 deletions lib/internal/streams/buffer_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,67 +98,63 @@ module.exports = class BufferList {

// Consumes a specified amount of characters from the buffered data.
_getString(n) {
var p = this.head;
var c = 1;
var ret = p.data;
n -= ret.length;
while (p = p.next) {
let ret = '';
let p = this.head;
let c = 0;
do {
const str = p.data;
const nb = (n > str.length ? str.length : n);
if (nb === str.length)
if (n > str.length) {
ret += str;
else
ret += str.slice(0, n);
n -= nb;
if (n === 0) {
if (nb === str.length) {
n -= str.length;
} else {
if (n === str.length) {
ret += str;
++c;
if (p.next)
this.head = p.next;
else
this.head = this.tail = null;
} else {
ret += str.slice(0, n);
this.head = p;
p.data = str.slice(nb);
p.data = str.slice(n);
}
break;
}
++c;
}
} while (p = p.next);
this.length -= c;
return ret;
}

// Consumes a specified amount of bytes from the buffered data.
_getBuffer(n) {
const ret = Buffer.allocUnsafe(n);
var p = this.head;
var c = 1;
p.data.copy(ret);
n -= p.data.length;
while (p = p.next) {
const retLen = n;
let p = this.head;
let c = 0;
do {
const buf = p.data;
const nb = (n > buf.length ? buf.length : n);
if (nb === buf.length)
ret.set(buf, ret.length - n);
else
ret.set(new Uint8Array(buf.buffer, buf.byteOffset, nb), ret.length - n);
n -= nb;
if (n === 0) {
if (nb === buf.length) {
if (n > buf.length) {
ret.set(buf, retLen - n);
n -= buf.length;
} else {
if (n === buf.length) {
ret.set(buf, retLen - n);
++c;
if (p.next)
this.head = p.next;
else
this.head = this.tail = null;
} else {
ret.set(new Uint8Array(buf.buffer, buf.byteOffset, n), retLen - n);
this.head = p;
p.data = buf.slice(nb);
p.data = buf.slice(n);
}
break;
}
++c;
}
} while (p = p.next);
this.length -= c;
return ret;
}
Expand Down