Skip to content

Fast-path the CSI introducer in StateMachine::ProcessString - #20974

Open
Clint Rutkas (crutkas) wants to merge 1 commit into
microsoft:mainfrom
crutkas:perf/csi-prefix-fast-path
Open

Clint Rutkas (crutkas) wants to merge 1 commit into
microsoft:mainfrom
crutkas:perf/csi-prefix-fast-path

Conversation

@crutkas

@crutkas Clint Rutkas (crutkas) commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Summary of the Pull Request

When the ground-state print scan stops on ESC [, enter Escape and CsiEntry directly instead of feeding both characters through ProcessCharacter. Only the output engine in ANSI mode with tracing off takes this path; entry actions and the passthrough prefix are unchanged. New tests split CSI sequences at every offset.

References and Relevant Issues

One of 8 perf PRs; numbers for the whole set are in #20973.

Detailed Description of the Pull Request / Additional comments

This PR alone vs main 9a8127f (Hyper-V VM, WARP, Release x64, no PGO, 12 interleaved rounds, 24 rows per arm per mode; median Δ, 95% bootstrap CI, Mann-Whitney p; CPU is WindowsTerminal + OpenConsole per MiB):

Mode MiB/s Δ CPU ms/MiB Δ
seq +0.7% [−3.7%, +3.3%], p=.381 +1.5% [−2.0%, +3.1%], p=.910
sgr +7.6% [+5.6%, +8.9%], p<.001 −7.0% [−7.6%, −4.3%], p<.001
cursor +7.7% [+6.8%, +10.0%], p<.001 −7.5% [−8.9%, −5.1%], p<.001

Memory: median peak working set and private bytes of both processes moved by at most 0.4%.

Validation Steps Performed

Release x64 build with only this commit on main; TAEF: til 278/279, TextBuffer 13/13, ConParser 762/763 (1 skipped), ConAdapter 414/414, Conhost 5106/5107 (1 skipped), Terminal.Core 59/59. EnvTests::Generate fails on main too.

PR Checklist

  • Closes #xxx
  • Tests added/passed
  • Documentation updated
    • If checked, please file a pull request on our docs repo and link it here: #xxx
  • Schema updated (if necessary)

When the ground-state print scan stops on ESC [, enter Escape and
CsiEntry directly instead of feeding both characters through
ProcessCharacter. Only for the output engine in ANSI mode with parser
tracing off. Both entry actions run and the raw prefix is kept for
passthrough, so behavior doesn't change.

Adds ParserTracing::IsEnabled(), the provider check AddSequenceTrace
already does inline. The TraceCharInput change adds the identical
helper.

Adds tests for CSI sequences split at every offset across two writes,
including passthrough and the OnCsiComplete callback.
Comment on lines +2013 to +2019
if (_state == VTStates::Ground &&
string.size() - i >= 2 &&
til::at(string, i) == AsciiChars::ESC &&
til::at(string, i + 1) == L'[' &&
!_isEngineForInput &&
_parserMode.test(Mode::Ansi) &&
!_trace.IsEnabled())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the perf improvement of this change alone? The PR description does not establish whether this individual change results in a significant improvement.

My 50ct are that we should rewrite the VT parser from a push to a pull parser, as this is easier to unit test, fuzz, and to optimize. However, this requires a major rewrite.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. Measuring it alone, will post the number. If it's noise I'll close it. Agree a pull parser is the real fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Measured it alone vs main: sgr +7.6% throughput, -7.0% CPU; cursor +7.7%, -7.5%; seq flat. Table's in the description. Keeping it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, the way this is written both leaves perf on the table and is also written in a somewhat odd way. I think this isn't a bad idea per se due to how common CSIs are but I'd like to see this cleaned up.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants