Skip to content

Commit ff3f472

Browse files
committed
stream: add Readable.readv
A faster alternative to Readable.read for certain use cases. e.g. fs.writevSync(fd, readable.readv())
1 parent 6176222 commit ff3f472

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

lib/internal/streams/readable.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,16 @@ function howMuchToRead(n, state) {
641641
return (state[kState] & kEnded) !== 0 ? state.length : 0;
642642
}
643643

644+
Readable.prototype.readv = function readv() {
645+
return _read.call(this, true);
646+
};
647+
648+
Readable.prototype.read = function read () {
649+
return _read.call(this, false);
650+
}
651+
644652
// You can override either this method, or the async _read(n) below.
645-
Readable.prototype.read = function(n) {
653+
function _read (n, returnArr) {
646654
debug('read', n);
647655
// Same as parseInt(undefined, 10), however V8 7.3 performance regressed
648656
// in this scenario, so we are doing it manually.
@@ -748,7 +756,7 @@ Readable.prototype.read = function(n) {
748756

749757
let ret;
750758
if (n > 0)
751-
ret = fromList(n, state);
759+
ret = fromList(n, state, returnArr);
752760
else
753761
ret = null;
754762

@@ -777,7 +785,13 @@ Readable.prototype.read = function(n) {
777785

778786
if (ret !== null && (state[kState] & (kErrorEmitted | kCloseEmitted)) === 0) {
779787
state[kState] |= kDataEmitted;
780-
this.emit('data', ret);
788+
if (Array.isArray(ret)) {
789+
for (let i = 0; i < ret.length; ++i) {
790+
this.emit('data', ret[i]);
791+
}
792+
} else {
793+
this.emit('data', ret);
794+
}
781795
}
782796

783797
return ret;
@@ -1582,7 +1596,7 @@ Readable._fromList = fromList;
15821596
// Length is the combined lengths of all the buffers in the list.
15831597
// This function is designed to be inlinable, so please take care when making
15841598
// changes to the function body.
1585-
function fromList(n, state) {
1599+
function fromList(n, state, returnArr) {
15861600
// nothing buffered.
15871601
if (state.length === 0)
15881602
return null;
@@ -1594,8 +1608,12 @@ function fromList(n, state) {
15941608
const len = buf.length;
15951609

15961610
if ((state[kState] & kObjectMode) !== 0) {
1597-
ret = buf[idx];
1598-
buf[idx++] = null;
1611+
if (returnArr) {
1612+
ret = buf.slice(0, idx);
1613+
} else {
1614+
ret = buf[idx];
1615+
buf[idx++] = null;
1616+
}
15991617
} else if (!n || n >= state.length) {
16001618
// Read it all, truncate the list.
16011619
if ((state[kState] & kDecoder) !== 0) {
@@ -1604,11 +1622,16 @@ function fromList(n, state) {
16041622
ret += buf[idx];
16051623
buf[idx++] = null;
16061624
}
1625+
ret = returnArr ? [ret] : ret;
16071626
} else if (len - idx === 0) {
16081627
ret = Buffer.alloc(0);
1628+
ret = returnArr ? [ret] : ret;
16091629
} else if (len - idx === 1) {
16101630
ret = buf[idx];
16111631
buf[idx++] = null;
1632+
ret = returnArr ? [ret] : ret;
1633+
} else if (returnArr) {
1634+
ret = buf.slice(0, idx);
16121635
} else {
16131636
ret = Buffer.allocUnsafe(state.length);
16141637

@@ -1623,10 +1646,12 @@ function fromList(n, state) {
16231646
// `slice` is the same for buffers and strings.
16241647
ret = buf[idx].slice(0, n);
16251648
buf[idx] = buf[idx].slice(n);
1649+
ret = returnArr ? [ret] : ret;
16261650
} else if (n === buf[idx].length) {
16271651
// First chunk is a perfect match.
16281652
ret = buf[idx];
16291653
buf[idx++] = null;
1654+
ret = returnArr ? [ret] : ret;
16301655
} else if ((state[kState] & kDecoder) !== 0) {
16311656
ret = '';
16321657
while (idx < len) {
@@ -1646,6 +1671,27 @@ function fromList(n, state) {
16461671
break;
16471672
}
16481673
}
1674+
ret = returnArr ? [ret] : ret;
1675+
} else if (returnArr) {
1676+
ret = [];
1677+
const retLen = n;
1678+
while (idx < len) {
1679+
const data = buf[idx];
1680+
if (n > data.length) {
1681+
ret.push(data);
1682+
n -= data.length;
1683+
buf[idx++] = null;
1684+
} else {
1685+
if (n === data.length) {
1686+
ret.push(data);
1687+
buf[idx++] = null;
1688+
} else {
1689+
ret.push(new FastBuffer(data.buffer, data.byteOffset, n));
1690+
buf[idx] = new FastBuffer(data.buffer, data.byteOffset + n, data.length - n);
1691+
}
1692+
break;
1693+
}
1694+
}
16491695
} else {
16501696
ret = Buffer.allocUnsafe(n);
16511697

0 commit comments

Comments
 (0)