Skip to content

Commit 5f35204

Browse files
mscdexFishrock123
authored andcommitted
stream: improve Readable.read() performance
read() performance is improved most by switching from an array to a linked list for storing buffered data. However, other changes that also contribute include: making some hot functions inlinable, faster read() argument checking, and misc code rearrangement to avoid unnecessary code execution. PR-URL: #7077 Reviewed-By: Calvin Metcalf <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent d8fee36 commit 5f35204

10 files changed

+420
-137
lines changed

benchmark/streams/readable-bigread.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const v8 = require('v8');
5+
const Readable = require('stream').Readable;
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [100e1]
9+
});
10+
11+
function main(conf) {
12+
const n = +conf.n;
13+
const b = new Buffer(32);
14+
const s = new Readable();
15+
function noop() {}
16+
s._read = noop;
17+
18+
// Force optimization before starting the benchmark
19+
s.push(b);
20+
v8.setFlagsFromString('--allow_natives_syntax');
21+
eval('%OptimizeFunctionOnNextCall(s.read)');
22+
s.push(b);
23+
while (s.read(128));
24+
25+
bench.start();
26+
for (var k = 0; k < n; ++k) {
27+
for (var i = 0; i < 1e4; ++i)
28+
s.push(b);
29+
while (s.read(128));
30+
}
31+
bench.end(n);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const v8 = require('v8');
5+
const Readable = require('stream').Readable;
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [100e1]
9+
});
10+
11+
function main(conf) {
12+
const n = +conf.n;
13+
const b = new Buffer(32);
14+
const s = new Readable();
15+
function noop() {}
16+
s._read = noop;
17+
18+
// Force optimization before starting the benchmark
19+
s.push(b);
20+
v8.setFlagsFromString('--allow_natives_syntax');
21+
eval('%OptimizeFunctionOnNextCall(s.read)');
22+
s.push(b);
23+
while (s.read(106));
24+
25+
bench.start();
26+
for (var k = 0; k < n; ++k) {
27+
for (var i = 0; i < 1e4; ++i)
28+
s.push(b);
29+
while (s.read(106));
30+
}
31+
bench.end(n);
32+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const v8 = require('v8');
5+
const Readable = require('stream').Readable;
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [200e1]
9+
});
10+
11+
function main(conf) {
12+
const n = +conf.n;
13+
const b = new Buffer(32);
14+
const s = new Readable();
15+
function noop() {}
16+
s._read = noop;
17+
18+
// Force optimization before starting the benchmark
19+
s.push(b);
20+
v8.setFlagsFromString('--allow_natives_syntax');
21+
eval('%OptimizeFunctionOnNextCall(s.push)');
22+
eval('%OptimizeFunctionOnNextCall(s.read)');
23+
s.push(b);
24+
while (s.read(32));
25+
26+
bench.start();
27+
for (var k = 0; k < n; ++k) {
28+
for (var i = 0; i < 1e4; ++i)
29+
s.push(b);
30+
while (s.read(32));
31+
}
32+
bench.end(n);
33+
}

benchmark/streams/readable-readall.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const v8 = require('v8');
5+
const Readable = require('stream').Readable;
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [50e2]
9+
});
10+
11+
function main(conf) {
12+
const n = +conf.n;
13+
const b = new Buffer(32);
14+
const s = new Readable();
15+
function noop() {}
16+
s._read = noop;
17+
18+
// Force optimization before starting the benchmark
19+
s.push(b);
20+
v8.setFlagsFromString('--allow_natives_syntax');
21+
eval('%OptimizeFunctionOnNextCall(s.read)');
22+
s.push(b);
23+
while (s.read());
24+
25+
bench.start();
26+
for (var k = 0; k < n; ++k) {
27+
for (var i = 0; i < 1e4; ++i)
28+
s.push(b);
29+
while (s.read());
30+
}
31+
bench.end(n);
32+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const v8 = require('v8');
5+
const Readable = require('stream').Readable;
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [100e1]
9+
});
10+
11+
function main(conf) {
12+
const n = +conf.n;
13+
const b = new Buffer(32);
14+
const s = new Readable();
15+
function noop() {}
16+
s._read = noop;
17+
18+
// Force optimization before starting the benchmark
19+
s.push(b);
20+
v8.setFlagsFromString('--allow_natives_syntax');
21+
eval('%OptimizeFunctionOnNextCall(s.read)');
22+
s.push(b);
23+
while (s.read(12));
24+
25+
bench.start();
26+
for (var k = 0; k < n; ++k) {
27+
for (var i = 0; i < 1e4; ++i)
28+
s.push(b);
29+
while (s.read(12));
30+
}
31+
bench.end(n);
32+
}

0 commit comments

Comments
 (0)