Skip to content

Commit fe7d1e7

Browse files
Thiago SantosFarenheith
Thiago Santos
authored andcommitted
lib: performance improvement on readline async iterator
Using a direct approach to create the readline async iterator allowed an iteration over 20 to 58% faster. **BREAKING CHANGE**: With that change, the async iteterator obtained from the readline interface doesn't have the property "stream" any longer. This happened because it's no longer created through a Readable, instead, the async iterator is created directly from the events of the readline interface instance, so, if anyone is using that property, this change will break their code. Also, the Readable added a backpressure control that is fairly compensated by the use of FixedQueue + monitoring its size. This control wasn't really precise with readline before, though, because it only pauses the reading of the original stream, but the lines generated from the last message received from it was still emitted. For example: if the readable was paused at 1000 messages but the last one received generated 10k lines, but no further messages were emitted again until the queue was lower than the readable highWaterMark. A similar behavior still happens with the new implementation, but the highWaterMark used is fixed: 1024, and the original stream is resumed again only after the queue is cleared. Before making that change, I created a package implementing the same concept used here to validate it. You can find it [here](https://github.com/Farenheith/faster-readline-iterator) if this helps anyhow.
1 parent cf69964 commit fe7d1e7

4 files changed

+246
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
'use strict';
2+
const {
3+
Promise,
4+
SymbolAsyncIterator,
5+
ArrayPrototypeConcat,
6+
} = primordials;
7+
const FixedQueue = require('internal/fixed_queue');
8+
9+
const PAUSE_THRESHOLD = 1024;
10+
const RESUME_THRESHOLD = 1;
11+
const ITEM_EVENTS = ['data'];
12+
const CLOSE_EVENTS = ['close', 'end'];
13+
const ERROR_EVENTS = ['error'];
14+
15+
16+
function waitNext(emitter, next, events) {
17+
return new Promise((resolve, reject) => {
18+
const resolveNext = () => {
19+
for (let i = 0; i < events.length; i++)
20+
emitter.off(events[i], resolveNext);
21+
try {
22+
resolve(next());
23+
} catch (promiseError) {
24+
reject(promiseError);
25+
}
26+
};
27+
for (let i = 0; i < events.length; i++)
28+
emitter.once(events[i], resolveNext);
29+
});
30+
}
31+
32+
module.exports = function eventsToAsyncIteratorFactory(readable, {
33+
pauseThreshold = PAUSE_THRESHOLD,
34+
resumeThreshold = RESUME_THRESHOLD,
35+
closeEvents = CLOSE_EVENTS,
36+
itemEvents = ITEM_EVENTS,
37+
errorEvents = ERROR_EVENTS,
38+
}) {
39+
const events = ArrayPrototypeConcat(itemEvents, errorEvents, closeEvents);
40+
const highWaterMark = RESUME_THRESHOLD;
41+
42+
const queue = new FixedQueue();
43+
let done = false;
44+
let error;
45+
let queueSize = 0;
46+
let paused = false;
47+
const onError = (value) => {
48+
turn('off');
49+
error = value;
50+
};
51+
const onClose = () => {
52+
turn('off');
53+
done = true;
54+
};
55+
const onItem = (value) => {
56+
queue.push(value);
57+
queueSize++;
58+
if (queueSize >= pauseThreshold) {
59+
paused = true;
60+
readable.pause();
61+
}
62+
};
63+
function turn(onOff) {
64+
for (let i = 0; i < closeEvents.length; i++)
65+
readable[onOff](closeEvents[i], onClose);
66+
for (let i = 0; i < itemEvents.length; i++)
67+
readable[onOff](itemEvents[i], onItem);
68+
for (let i = 0; i < itemEvents.length; i++)
69+
readable[onOff](errorEvents[i], onError);
70+
}
71+
72+
turn('on');
73+
74+
function next() {
75+
if (!queue.isEmpty()) {
76+
const value = queue.shift();
77+
queueSize--;
78+
if (queueSize < resumeThreshold) {
79+
paused = false;
80+
readable.resume();
81+
}
82+
return {
83+
done: false,
84+
value,
85+
};
86+
}
87+
if (error) {
88+
throw error;
89+
}
90+
if (done) {
91+
return { done };
92+
}
93+
return waitNext(readable, next, events);
94+
}
95+
96+
const result = {
97+
next,
98+
highWaterMark,
99+
get isPaused() {
100+
return paused;
101+
},
102+
get queueSize() {
103+
return queueSize;
104+
}
105+
};
106+
result[SymbolAsyncIterator] = () => result;
107+
return result;
108+
};

lib/internal/readline/interface.js

+9-37
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ const {
6464

6565
const { StringDecoder } = require('string_decoder');
6666

67-
// Lazy load Readable for startup performance.
68-
let Readable;
69-
7067
const kHistorySize = 30;
7168
const kMincrlfDelay = 100;
7269
// \r\n, \n, or \r followed by something other than \n
@@ -1185,41 +1182,16 @@ class Interface extends InterfaceConstructor {
11851182
* @returns {InterfaceAsyncIterator}
11861183
*/
11871184
[SymbolAsyncIterator]() {
1188-
if (this[kLineObjectStream] === undefined) {
1189-
if (Readable === undefined) {
1190-
Readable = require('stream').Readable;
1191-
}
1192-
const readable = new Readable({
1193-
objectMode: true,
1194-
read: () => {
1195-
this.resume();
1196-
},
1197-
destroy: (err, cb) => {
1198-
this.off('line', lineListener);
1199-
this.off('close', closeListener);
1200-
this.close();
1201-
cb(err);
1202-
},
1203-
});
1204-
const lineListener = (input) => {
1205-
if (!readable.push(input)) {
1206-
// TODO(rexagod): drain to resume flow
1207-
this.pause();
1208-
}
1209-
};
1210-
const closeListener = () => {
1211-
readable.push(null);
1212-
};
1213-
const errorListener = (err) => {
1214-
readable.destroy(err);
1215-
};
1216-
this.on('error', errorListener);
1217-
this.on('line', lineListener);
1218-
this.on('close', closeListener);
1219-
this[kLineObjectStream] = readable;
1185+
if (!this[kLineObjectStream]) {
1186+
this[kLineObjectStream] = require(
1187+
'internal/readline/eventsToAsyncIteratorFactory'
1188+
)(
1189+
this, {
1190+
itemEvents: ['line'],
1191+
closeEvents: ['close']
1192+
});
12201193
}
1221-
1222-
return this[kLineObjectStream][SymbolAsyncIterator]();
1194+
return this[kLineObjectStream];
12231195
}
12241196
}
12251197

test/parallel/test-readline-async-iterators-backpressure.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,41 @@ const { Readable } = require('stream');
66
const readline = require('readline');
77

88
const CONTENT = 'content';
9-
const TOTAL_LINES = 18;
9+
const LINES_PER_PUSH = 2051;
10+
const REPETITIONS = 3;
1011

1112
(async () => {
1213
const readable = new Readable({ read() {} });
13-
readable.push(`${CONTENT}\n`.repeat(TOTAL_LINES));
14+
let salt = 0;
15+
for (let i = 0; i < REPETITIONS; i++) {
16+
readable.push(`${CONTENT}\n`.repeat(LINES_PER_PUSH + i));
17+
salt += i;
18+
}
19+
const TOTAL_LINES = LINES_PER_PUSH * REPETITIONS + salt;
1420

1521
const rli = readline.createInterface({
1622
input: readable,
1723
crlfDelay: Infinity
1824
});
1925

2026
const it = rli[Symbol.asyncIterator]();
21-
const highWaterMark = it.stream.readableHighWaterMark;
27+
const highWaterMark = it.highWaterMark;
2228

2329
// For this test to work, we have to queue up more than the number of
2430
// highWaterMark items in rli. Make sure that is the case.
2531
assert(TOTAL_LINES > highWaterMark);
2632

2733
let iterations = 0;
2834
let readableEnded = false;
35+
let notPaused = 0;
2936
for await (const line of it) {
3037
assert.strictEqual(readableEnded, false);
31-
3238
assert.strictEqual(line, CONTENT);
33-
34-
const expectedPaused = TOTAL_LINES - iterations > highWaterMark;
35-
assert.strictEqual(readable.isPaused(), expectedPaused);
39+
assert.ok(it.queueSize <= TOTAL_LINES);
40+
assert.strictEqual(readable.isPaused(), it.queueSize >= 1);
41+
if (!readable.isPaused()) {
42+
notPaused++;
43+
}
3644

3745
iterations += 1;
3846

@@ -45,4 +53,5 @@ const TOTAL_LINES = 18;
4553
}
4654

4755
assert.strictEqual(iterations, TOTAL_LINES);
56+
assert.strictEqual(notPaused, REPETITIONS);
4857
})().then(common.mustCall());

test/parallel/test-readline-async-iterators.js

+113-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const common = require('../common');
44
const fs = require('fs');
55
const { join } = require('path');
66
const readline = require('readline');
7+
const { Readable } = require('stream');
78
const assert = require('assert');
89

910
const tmpdir = require('../common/tmpdir');
@@ -63,7 +64,6 @@ async function testMutual() {
6364
// This outer loop should only iterate once.
6465
assert.strictEqual(iterated, false);
6566
iterated = true;
66-
6767
iteratedLines.push(k);
6868
for await (const l of rli) {
6969
iteratedLines.push(l);
@@ -74,4 +74,115 @@ async function testMutual() {
7474
}
7575
}
7676

77-
testSimple().then(testMutual).then(common.mustCall());
77+
async function testPerformance() {
78+
const loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
79+
Dui accumsan sit amet nulla facilisi morbi tempus iaculis urna.
80+
Eget dolor morbi non arcu risus quis varius quam quisque.
81+
Lacus viverra vitae congue eu consequat ac felis donec.
82+
Amet porttitor eget dolor morbi non arcu.
83+
Velit ut tortor pretium viverra suspendisse.
84+
Mauris nunc congue nisi vitae suscipit tellus.
85+
Amet nisl suscipit adipiscing bibendum est ultricies integer.
86+
Sit amet dictum sit amet justo donec enim diam.
87+
Condimentum mattis pellentesque id nibh tortor id aliquet lectus proin.
88+
Diam in arcu cursus euismod quis viverra nibh.
89+
`;
90+
91+
const REPETITIONS = 10000;
92+
const SAMPLE = 100;
93+
const THRESHOLD = 81;
94+
95+
function getLoremIpsumStream() {
96+
const readable = Readable({
97+
objectMode: true,
98+
});
99+
let i = 0;
100+
readable._read = () => readable.push(
101+
i++ >= REPETITIONS ? null : loremIpsum
102+
);
103+
return readable;
104+
}
105+
106+
function oldWay() {
107+
const readable = new Readable({
108+
objectMode: true,
109+
read: () => {
110+
this.resume();
111+
},
112+
destroy: (err, cb) => {
113+
this.off('line', lineListener);
114+
this.off('close', closeListener);
115+
this.close();
116+
cb(err);
117+
},
118+
});
119+
const lineListener = (input) => {
120+
if (!readable.push(input)) {
121+
// TODO(rexagod): drain to resume flow
122+
this.pause();
123+
}
124+
};
125+
const closeListener = () => {
126+
readable.push(null);
127+
};
128+
const errorListener = (err) => {
129+
readable.destroy(err);
130+
};
131+
this.on('error', errorListener);
132+
this.on('line', lineListener);
133+
this.on('close', closeListener);
134+
return readable[Symbol.asyncIterator]();
135+
}
136+
137+
function getAvg(mean, x, n) {
138+
return (mean * n + x) / (n + 1);
139+
}
140+
141+
let totalTimeOldWay = 0;
142+
let totalTimeNewWay = 0;
143+
let totalCharsOldWay = 0;
144+
let totalCharsNewWay = 0;
145+
const linesOldWay = [];
146+
const linesNewWay = [];
147+
148+
for (let time = 0; time < SAMPLE; time++) {
149+
const rlOldWay = readline.createInterface({
150+
input: getLoremIpsumStream(),
151+
});
152+
let currenttotalTimeOldWay = Date.now();
153+
for await (const line of oldWay.call(rlOldWay)) {
154+
totalCharsOldWay += line.length;
155+
if (time === 0) {
156+
linesOldWay.push(line);
157+
}
158+
}
159+
currenttotalTimeOldWay = Date.now() - currenttotalTimeOldWay;
160+
totalTimeOldWay = getAvg(totalTimeOldWay, currenttotalTimeOldWay, SAMPLE);
161+
162+
const rlNewWay = readline.createInterface({
163+
input: getLoremIpsumStream(),
164+
});
165+
let currentTotalTimeNewWay = Date.now();
166+
for await (const line of rlNewWay) {
167+
totalCharsNewWay += line.length;
168+
if (time === 0) {
169+
linesNewWay.push(line);
170+
}
171+
}
172+
currentTotalTimeNewWay = Date.now() - currentTotalTimeNewWay;
173+
totalTimeNewWay = getAvg(totalTimeNewWay, currentTotalTimeNewWay, SAMPLE);
174+
}
175+
176+
assert.strictEqual(totalCharsOldWay, totalCharsNewWay);
177+
assert.strictEqual(linesOldWay.length, linesNewWay.length);
178+
linesOldWay.forEach((line, index) =>
179+
assert.strictEqual(line, linesNewWay[index])
180+
);
181+
const percentage = totalTimeNewWay * 100 / totalTimeOldWay;
182+
assert.ok(percentage <= THRESHOLD, `Failed: ${totalTimeNewWay} isn't lesser than ${THRESHOLD}% of ${totalTimeOldWay}. Actual percentage: ${percentage.toFixed(2)}%`);
183+
}
184+
185+
testSimple()
186+
.then(testMutual)
187+
.then(testPerformance)
188+
.then(common.mustCall());

0 commit comments

Comments
 (0)