Skip to content

Commit 0f46c1c

Browse files
BridgeARtargos
authored andcommitted
repl: fix cpu overhead pasting big strings to the REPL
Pasting input should not trigger any completions and other calculations. This is now handled by just writing the string to the terminal in case the user is pasting. As soon as pasting is done, the completions will be re-enabled. Fixes: #40626 Fixes: #43343 PR-URL: #59857 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 026d4e3 commit 0f46c1c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/internal/readline/interface.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,12 @@ class Interface extends InterfaceConstructor {
658658

659659
[kInsertString](c) {
660660
this[kBeforeEdit](this.line, this.cursor);
661+
if (!this.isCompletionEnabled) {
662+
this.line += c;
663+
this.cursor += c.length;
664+
this[kWriteToOutput](c);
665+
return;
666+
}
661667
if (this.cursor < this.line.length) {
662668
const beg = StringPrototypeSlice(this.line, 0, this.cursor);
663669
const end = StringPrototypeSlice(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const repl = require('repl');
5+
const stream = require('stream');
6+
const assert = require('assert');
7+
8+
// Pasting big input should not cause the process to have a huge CPU usage.
9+
10+
const cpuUsage = process.cpuUsage();
11+
12+
const r = initRepl();
13+
r.input.emit('data', 'a'.repeat(2e4) + '\n');
14+
r.input.emit('data', '.exit\n');
15+
16+
r.once('exit', common.mustCall(() => {
17+
const diff = process.cpuUsage(cpuUsage);
18+
assert.ok(diff.user < 1e6);
19+
}));
20+
21+
function initRepl() {
22+
const input = new stream();
23+
input.write = input.pause = input.resume = () => {};
24+
input.readable = true;
25+
26+
const output = new stream();
27+
output.write = () => {};
28+
output.writable = true;
29+
30+
return repl.start({
31+
input,
32+
output,
33+
terminal: true,
34+
prompt: ''
35+
});
36+
}

0 commit comments

Comments
 (0)