diff options
author | Brian White <mscdex@mscdex.net> | 2019-08-27 04:12:41 -0400 |
---|---|---|
committer | Brian White <mscdex@mscdex.net> | 2019-08-30 23:39:26 -0400 |
commit | 98b718572f9c136e7a11dc60e984d873d08346cc (patch) | |
tree | 49610431a6d83383d629e9a539607128a898a456 /lib | |
parent | 25d59cf83a970658311e5bb4c6671a7fba8c1d92 (diff) | |
download | android-node-v8-98b718572f9c136e7a11dc60e984d873d08346cc.tar.gz android-node-v8-98b718572f9c136e7a11dc60e984d873d08346cc.tar.bz2 android-node-v8-98b718572f9c136e7a11dc60e984d873d08346cc.zip |
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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/streams/buffer_list.js | 54 |
1 files changed, 25 insertions, 29 deletions
diff --git a/lib/internal/streams/buffer_list.js b/lib/internal/streams/buffer_list.js index 9d6e9e2fe4..715d5d201d 100644 --- a/lib/internal/streams/buffer_list.js +++ b/lib/internal/streams/buffer_list.js @@ -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; } |