Skip to content

Commit e790eb6

Browse files
BridgeARaduh95
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 <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 34dcb7d commit e790eb6

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
@@ -624,6 +624,12 @@ class Interface extends InterfaceConstructor {
624624

625625
[kInsertString](c) {
626626
this[kBeforeEdit](this.line, this.cursor);
627+
if (!this.isCompletionEnabled) {
628+
this.line += c;
629+
this.cursor += c.length;
630+
this[kWriteToOutput](c);
631+
return;
632+
}
627633
if (this.cursor < this.line.length) {
628634
const beg = StringPrototypeSlice(this.line, 0, this.cursor);
629635
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)