Fix keyboard input latency (~100ms per keystroke) from VMIN=3/VTIME=1 - #63
Merged
Merged
Conversation
set_terminal() used VMIN=3 + VTIME=1 (interbyte-timer mode), introduced in 03baea1 to make arrow keys work under Cygwin. With VMIN=3, a single ordinary keystroke must wait for the 100ms interbyte timeout before read() returns, so typing feels sluggish (~0.1s per character). - set_terminal: switch to VMIN=1/VTIME=0 so each byte returns immediately - read_escape_tail(): after a lone ESC, drain trailing bytes within a short select() window so multi-byte sequences (arrow keys, Home/End) are still parsed as a single key instead of leaking control bytes ('[', 'A', ...) into the input line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Typing in the VPCS console feels sluggish: each ordinary keystroke is delayed by roughly 100ms.
Root cause
set_terminal()puts the terminal in non-canonical mode withVMIN=3andVTIME=1(interbyte-timer mode). This was introduced in 03baea1 ("arrow keys is OK in cygwin") so that a 3-byte escape sequence such as an arrow key (\e[A) would be returned by a singleread().With
VMIN=3,read()blocks until it has collected 3 bytes or the interbyte timer (VTIME=1= 100ms) expires with no new byte arriving. A normal key produces only 1 byte, so every keystroke has to wait for the 100ms timeout before it is returned — hence the ~0.1s-per-character lag. (Escape sequences happen to be 3 bytes, which is why arrow keys did not feel laggy while ordinary typing did.)Fix
set_terminal(): switch toVMIN=1, VTIME=0so each byte is returned immediately.read_escape_tail(): after a loneESC, drain any trailing bytes arriving within a shortselect()window (50ms for the first byte, 10ms for subsequent ones), so multi-byte sequences (arrow keys, Home/End, ...) are still parsed as a single key instead of leaking their control bytes ([,A, ...) into the input line.This removes the per-character delay while preserving the arrow-key / Home / End / history behavior — the original reason
VMINwas raised.Verification
gcc -O2 -Wall; with-Wextrathere are no new warnings (the few-Wextrawarnings are pre-existing and unrelated).\x1b[A(Up arrow) underVMIN=1is recognized as a single key and does not leak[/Ainto the input line.