Skip to content

Commit

Permalink
stream: improve read() performance
Browse files Browse the repository at this point in the history
PR-URL: #29337
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
  • Loading branch information
mscdex authored and BridgeAR committed Sep 4, 2019
1 parent 0c4df35 commit 2efd72f
Showing 1 changed file with 25 additions and 29 deletions.
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

0 comments on commit 2efd72f

Please sign in to comment.