stream: improve read() performance

PR-URL: https://github.com/nodejs/node/pull/29337
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
This commit is contained in:
Brian White 2019-08-27 04:12:41 -04:00
parent 25d59cf83a
commit 98b718572f
No known key found for this signature in database
GPG Key ID: 606D7358F94DA209

View File

@ -98,33 +98,31 @@ 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;
}
@ -132,33 +130,31 @@ module.exports = class BufferList {
// 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;
}