Skip to content

repl: redraw highlighted input once per chunk - #67422

Merged
nodejs-github-bot merged 2 commits into
nodejs:mainfrom
maxhfisher:repl-batch-highlight-redraw
Nov 21, 2026
Merged

nodejs-github-bot merged 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 87.03704% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.66%. Comparing base (40aad9e) to head (debfb34).
⚠️ Report is 219 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/readline/interface.js 85.10% 5 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67422      +/-   ##
==========================================
- Coverage   90.72%   90.66%   -0.07%     
==========================================
  Files         798      799       +1     
  Lines      280074   280367     +293     
  Branches    53876    54154     +278     
==========================================
+ Hits       254101   254196      +95     
- Misses      16272    16281       +9     
- Partials     9701     9890     +189     
Files with missing lines Coverage Δ
lib/repl.js 92.97% <100.00%> (+0.03%) ⬆️
lib/internal/readline/interface.js 93.02% <85.10%> (-0.24%) ⬇️

... and 117 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.

@ViviBeri

Copy link
Copy Markdown

@maxhfisher Thanks for testing this and incorporating the fix.

The measured reduction in redraws and terminal writes confirms the optimization is worthwhile. I agree that asserting an exact redraw count could unnecessarily couple the test to implementation details.

Preserving tab completion behavior is also important. The updated approach looks reasonable to me.

@legendecas legendecas added request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. author ready PRs with CI started, the required approvals, and no outstanding review comments. labels Nov 20, 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 20, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@legendecas legendecas added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 21, 2026
@nodejs-github-bot nodejs-github-bot added commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. and removed commit-queue PRs queued for automated landing through the Commit Queue. labels Nov 21, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Commit Queue failed

This pull request has multiple commits, but no landing policy was selected.

Add commit-queue-squash PRs the Commit Queue should land as one squashed commit. to land it as one commit, or commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. to land the commits separately.

The pull request was removed from the Commit Queue and labeled commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. . After resolving the failure, remove that label and add commit-queue PRs queued for automated landing through the Commit Queue. to retry.

Full Commit Queue output
�[36m⠋�[39m Loading data for nodejs/node/pull/67422
�[36m⠋�[39m Loading data for nodejs/node/pull/67422
�[36m⠋�[39m Getting collaborator contacts from README of nodejs/node
�[36m⠋�[39m Getting PR from nodejs/node/pull/67422
�[36m⠋�[39m Getting reviews from nodejs/node/pull/67422
�[36m⠋�[39m Getting comments from nodejs/node/pull/67422
�[36m⠋�[39m Getting commits from nodejs/node/pull/67422
✔  Done loading data for nodejs/node/pull/67422
----------------------------------- PR info ------------------------------------
Title      repl: redraw highlighted input once per chunk (#67422)
   ⚠  Could not retrieve the email or name of the PR's author from user's GitHub profile!
Branch     maxhfisher:repl-batch-highlight-redraw -> nodejs:main
Labels     readline, repl, author ready, needs-ci, commit-queue
Commits    2
 - repl: redraw highlighted input once per chunk
 - repl: make last character of chunk redraw once
Committers 1
 - Max H Fisher <mfisher187@bloomberg.net>
PR-URL: https://github.com/nodejs/node/pull/67422
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/67422
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Wed, 11 Nov 2026 22:54:24 GMT
   ✔  Approvals: 2
   ✔  - James M Snell (@jasnell) (TSC): https://github.com/nodejs/node/pull/67422#pullrequestreview-5902051534
   ✔  - Chengzhong Wu (@legendecas) (TSC): https://github.com/nodejs/node/pull/67422#pullrequestreview-6000159150
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2026-11-20T22:24:41Z: https://ci.nodejs.org/job/node-test-pull-request/79375/
�[36m⠙�[39m Querying data for job/node-test-pull-request/79375/
�[36m⠙�[39m Querying data for job/node-test-pull-request/79375/
�[36m⠙�[39m Querying API for job/node-test-pull-request/79375/
✔  Build data downloaded
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  No git cherry-pick in progress
   ✔  No git am in progress
   ✔  No git rebase in progress
--------------------------------------------------------------------------------
�[36m⠹�[39m Bringing origin/main up to date...
�[36m⠹�[39m Bringing origin/main up to date...
From https://github.com/nodejs/node
 * branch                  main       -> FETCH_HEAD
✔  origin/main is now up-to-date
�[36m⠸�[39m Downloading patch for 67422
�[36m⠸�[39m Downloading patch for 67422
From https://github.com/nodejs/node
 * branch                  refs/pull/67422/merge -> FETCH_HEAD
✔  Fetched commits as 3fda34e18625..debfb346bb07
--------------------------------------------------------------------------------
[main 118d4e588a] repl: redraw highlighted input once per chunk
 Author: Max H Fisher <mfisher187@bloomberg.net>
 Date: Wed Nov 11 17:29:32 2026 -0500
 3 files changed, 132 insertions(+), 1 deletion(-)
 create mode 100644 test/parallel/test-repl-paste-highlight.js
[main e372f21084] repl: make last character of chunk redraw once
 Author: Max H Fisher <mfisher187@bloomberg.net>
 Date: Mon Nov 16 11:40:00 2026 -0500
 1 file changed, 13 insertions(+), 7 deletions(-)
   ✔  Patches applied
There are 2 commits in the PR. Attempting autorebase.
(node:371) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated.
(Use `node --trace-deprecation ...` to show where the warning was created)
Rebasing (2/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
repl: redraw highlighted input once per chunk

Signed-off-by: Max H Fisher <mfisher187@bloomberg.net>
PR-URL: https://github.com/nodejs/node/pull/67422
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
--------------------------------------------------------------------------------
[detached HEAD 887057b340] repl: redraw highlighted input once per chunk
 Author: Max H Fisher <mfisher187@bloomberg.net>
 Date: Wed Nov 11 17:29:32 2026 -0500
 3 files changed, 132 insertions(+), 1 deletion(-)
 create mode 100644 test/parallel/test-repl-paste-highlight.js
Rebasing (3/4)
Rebasing (4/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
repl: make last character of chunk redraw once

PR-URL: https://github.com/nodejs/node/pull/67422
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
--------------------------------------------------------------------------------
[detached HEAD ceab8372ac] repl: make last character of chunk redraw once
 Author: Max H Fisher <mfisher187@bloomberg.net>
 Date: Mon Nov 16 11:40:00 2026 -0500
 1 file changed, 13 insertions(+), 7 deletions(-)
Successfully rebased and updated refs/heads/main.
--------------------------------------------------------------------------------
   ℹ  Add `commit-queue-squash` label to land the PR as one commit, or `commit-queue-rebase` to land as separate commits.

View workflow run

@legendecas legendecas added commit-queue PRs queued for automated landing through the Commit Queue. commit-queue-squash PRs the Commit Queue should land as one squashed commit. and removed commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. labels Nov 21, 2026
@nodejs-github-bot
nodejs-github-bot merged commit a6dcfec into nodejs:main Nov 21, 2026
80 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in a6dcfec

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue-squash PRs the Commit Queue should land as one squashed commit. 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