Skip to content

repl: redraw highlighted input once per chunk - #67422

Open
maxhfisher wants to merge 2 commits into
nodejs:mainfrom
maxhfisher:repl-batch-highlight-redraw
Open

maxhfisher wants to merge 2 commits into
nodejs:mainfrom
maxhfisher:repl-batch-highlight-redraw

Conversation

@maxhfisher

@maxhfisher maxhfisher commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

This is intended as a fix for #67301. When text is pasted into the REPL, the terminal provides it in chunks, but readline interprets and redraws the entire input for each character when syntax highlighting is enabled (#64591). When the pasted text is large, this makes the REPL feel noticeably slow.

This changes readline to only redraw when a pasted chunk has been processed or if there is a character in the text that requires the screen to be up to date (such as a return or backspace). This reduces the number of full redraws and output as shown in the table below. However, the total number of bytes written to the terminal still grows quadratically with the number of lines. This only improves performance by a constant multiple for multi-line pastes.

Issue's object (3,029 bytes, 161 lines) v24.15.0 (no highlighting) v26.13.0 (stock) v26.13.0 + fix
Bytes written to the terminal 4,941 7,426,597 435,541
Full redraws of the input 1 2,882 168

While working on this I noticed a separate bug (#67423 (comment)) on main which wasn't back-ported to v26 that causes large multi-line pastes to be truncated. To test this fix, you'll need to build it on top of v26.

Signed-off-by: Max H Fisher <mfisher187@bloomberg.net>
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. readline Issues and PRs related to the built-in readline module. repl Issues and PRs related to the REPL subsystem. labels Nov 11, 2026
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.71%. Comparing base (40aad9e) to head (a9858fe).
⚠️ Report is 99 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/readline/interface.js 92.68% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67422      +/-   ##
==========================================
- Coverage   90.72%   90.71%   -0.02%     
==========================================
  Files         798      801       +3     
  Lines      280074   281922    +1848     
  Branches    53876    54323     +447     
==========================================
+ Hits       254101   255737    +1636     
- Misses      16272    16309      +37     
- Partials     9701     9876     +175     
Files with missing lines Coverage Δ
lib/repl.js 92.97% <100.00%> (+0.03%) ⬆️
lib/internal/readline/interface.js 93.23% <92.68%> (-0.02%) ⬇️

... and 74 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ViviBeri

Copy link
Copy Markdown

@maxhfisher The final-character path appears to redraw the input twice.

"emitKeypressEvents()" leaves "isCompletionEnabled" true for the last character in a chunk, so "kWillDeferRefresh()" returns false. This flushes the pending redraw at the start of "kTtyWrite()", before the final character is inserted. The normal insertion path then calls "kRefreshLine()" again.

As a result, a highlighted paste with multiple characters redraws twice at the end of each chunk, rather than once as intended.

Could the final keypress consume the pending refresh instead of flushing it before dispatch? A regression test that counts full redraws for a multi-character chunk would catch this; the current test checks final rendering but not redraw count.

@legendecas legendecas added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@maxhfisher

Copy link
Copy Markdown
Contributor Author

As a result, a highlighted paste with multiple characters redraws twice at the end of each chunk, rather than once as intended. Could the final keypress consume the pending refresh instead of flushing it before dispatch?

@ViviBeri This is true, although the cost is quite minimal. I ran an experiment with a fix for this and it saved 2 redraws and about 5KB of write() calls to the terminal for the object described in the issue.

I've added this change to the PR. I (or I should say an agentic reviewer I was using) noticed that when the last character of a paste is a tab, the current behavior is to treat that tab as a real keypress and provide the autocompletion behavior. No test actually asserts that for the case where kColorize is true, but I've kept that behavior here at least.

A regression test that counts full redraws for a multi-character chunk would catch this; the current test checks final rendering but not redraw count.

I intentionally chose not to add a test which asserts on the number of redraws because I didn't think that should be part of the contract. This is just a performance optimization, so I don't think we should make promises about the implementation. If maintainers feel strongly that this is worth asserting on, I'm happy to add it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. readline Issues and PRs related to the built-in readline module. repl Issues and PRs related to the REPL subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants