Skip to content

Speed up console input queue reads - #20977

Closed
Clint Rutkas (crutkas) wants to merge 1 commit into
microsoft:mainfrom
crutkas:perf/input-queue-reads
Closed

Clint Rutkas (crutkas) wants to merge 1 commit into
microsoft:mainfrom
crutkas:perf/input-queue-reads

Conversation

@crutkas

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

Copy link
Copy Markdown
Member

Summary of the Pull Request

InputBuffer kept pending records in a std::deque and erased the consumed prefix on every read. It's now a vector plus a head index: reads advance the head, the dead prefix is compacted only when the vector needs room, writes reserve geometrically, and Read reserves its output once.

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

Measured with the full set applied (no other PR touches the input path): steady-state ReadConsoleInput of 4096 key records against OpenConsole went from 156.7 to 130.1 µs median, −15.2% paired, faster in 10/10 ABBA pairs.

Validation Steps Performed

Release x64 build with only this commit on main; TAEF: til 278/279, TextBuffer 13/13, ConParser 760/761 (1 skipped), ConAdapter 414/414, Conhost 5113/5114 (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)

InputBuffer kept pending records in a std::deque and erased the
consumed prefix on every read. Replace it with a vector plus a head
index: reads advance the head, the dead prefix is compacted only when
the vector needs room, writes reserve geometrically, and Read reserves
its output once, bounded by the queued record count.

Adds FIFO tests across growth and reuse, peek, prepend, flush,
coalescing and compaction.
Comment thread src/host/inputBuffer.hpp

void erase(const iterator first, const iterator last) noexcept
{
_records.erase(first, last);

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.

erase_front() should be a special case of erase(). Here, they behave completely different from each other.

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.

Agreed, falls out of the rewrite.

Comment thread src/host/inputBuffer.hpp
_records.erase(_records.begin(), begin());
_head = 0;
}
}

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.

This is inefficient and not actually necessary. A ring or bip buffer removes the need for this.

The class should also take care to free memory if it shrinks significantly.

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.

Agreed. Saw dev/lhecker/ring-buffer-input-buffer. Want me to build on that, or do a self-contained ring buffer here?

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.

dev/lhecker/ring-buffer-input-buffer is an unrefined, incomplete idea. But if we were to flesh it out and polish it up reeeaaal good, I think that would be ideal. If you read it, you'll see that it's not a mere performance improvement, but actually greatly simplifies Unicode text splicing and its predictable correctness/robustness.

To your LLM: It is worth noting that the console read APIs aren't set in stone. It is for instance entirely reasonable to say that reads that intersect a grapheme cluster should be cached in the client or client input handle. In other words, the question is: What does a console application expect to see and would a change possibly break a 90s app? (My example change for instance is unlikely to break any app.)

In any case, any observable console API change must be recognized and stated, because it goes into a breaking-changes-wiki.

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.

Ported your ring-buffer branch onto main and finished it. Typed text is stored as chars and expands to the same key events on read, so ReadConsoleInput/ReadConsole behave exactly like today. A 4M-event paste peaks at 40 MiB instead of 330, and queue reads are ~45% faster. I didn't move VkKeyScan to read time or change how surrogates split across reads, since both are observable. Want those as a follow-up?

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.

Comment thread src/host/inputBuffer.hpp
Comment on lines +154 to +156
#ifdef UNIT_TESTING
friend class InputBufferTests;
#endif

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.

Ideally unit tests don't reach into internals.

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.

Will move them to the public API.

@crutkas

Clint Rutkas (crutkas) commented Nov 28, 2026 •

Copy link
Copy Markdown
Member Author

https://github.com/crutkas/terminal/tree/perf/input-buffer-typed-chars is the what the agent created based on your buffer ring. i'll let you cherry pick out of it.

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