Skip to content
Permalink
Browse files

stream: improve read() performance

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 Aug 27, 2019
1 parent 0c4df35 commit 2efd72f28d7d7e2d3aa1fe08ce64390e1c6be947
Showing with 25 additions and 29 deletions.
  1. +25 −29 lib/internal/streams/buffer_list.js
@@ -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;
}

0 comments on commit 2efd72f

Please sign in to comment.
You can’t perform that action at this time.