-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. PR-URL: #41276 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
aab9f32
commit 0f3e531
Showing
7 changed files
with
362 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const readline = require('readline'); | ||
const { Readable } = require('stream'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e1, 1e2, 1e3, 1e4, 1e5, 1e6], | ||
}); | ||
|
||
const loremIpsum = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed | ||
do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
Dui accumsan sit amet nulla facilisi morbi tempus iaculis urna. | ||
Eget dolor morbi non arcu risus quis varius quam quisque. | ||
Lacus viverra vitae congue eu consequat ac felis donec. | ||
Amet porttitor eget dolor morbi non arcu. | ||
Velit ut tortor pretium viverra suspendisse. | ||
Mauris nunc congue nisi vitae suscipit tellus. | ||
Amet nisl suscipit adipiscing bibendum est ultricies integer. | ||
Sit amet dictum sit amet justo donec enim diam. | ||
Condimentum mattis pellentesque id nibh tortor id aliquet lectus proin. | ||
Diam in arcu cursus euismod quis viverra nibh. | ||
Rest of line`; | ||
|
||
function getLoremIpsumStream(repetitions) { | ||
const readable = Readable({ | ||
objectMode: true, | ||
}); | ||
let i = 0; | ||
readable._read = () => readable.push( | ||
i++ >= repetitions ? null : loremIpsum | ||
); | ||
return readable; | ||
} | ||
|
||
async function main({ n }) { | ||
bench.start(); | ||
let lineCount = 0; | ||
|
||
const iterable = readline.createInterface({ | ||
input: getLoremIpsumStream(n), | ||
}); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
for await (const _ of iterable) { | ||
lineCount++; | ||
} | ||
bench.end(lineCount); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const { | ||
Symbol, | ||
} = primordials; | ||
|
||
const kFirstEventParam = Symbol('nodejs.kFirstEventParam'); | ||
|
||
module.exports = { | ||
kFirstEventParam, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const runBenchmark = require('../common/benchmark'); | ||
|
||
runBenchmark('readline', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); |
Oops, something went wrong.