Skip to content

perf(readline): optimize per-keystroke rendering and completion latency - #32

Merged
caomengxuan666 merged 2 commits into
unixwin:mainfrom
arookieofc:feature/winux-shell-integration
Mar 20, 2026
Merged

caomengxuan666 merged 2 commits into
unixwin:mainfrom
arookieofc:feature/winux-shell-integration

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Summary

Reduces unnecessary computation and Win32 API calls on every key event
in the interactive readline loop.

Changes

1. Lazy completion recomputation (completions_dirty flag)

Previously completer(buffer) was called unconditionally at the top of
the event loop, even for arrow keys, Escape, or any event that does not
modify the buffer. A completions_dirty boolean now gates the call —
completions are recomputed only when the buffer actually changes
(character insert, Backspace, Tab accept, history navigation).

2. Remove duplicate UTF-8 → wstring conversion in redraw()

utf8_to_wstring(buffer) was called twice per redraw: once to render
the buffer text (step 3) and again to render the cursor position (step 5
via a substr copy). The wstring is now produced once and hoisted to
the enclosing scope. Cursor wchar-offset is computed with a zero-copy
MultiByteToWideChar(..., nullptr, 0) dry-run, eliminating a heap
allocation on every redraw.

3. Single syscall for completion row rendering (writeAt)

The old implementation issued two separate Win32 calls per row:
FillConsoleOutputAttribute then WriteConsoleOutputCharacterW.
Replaced with a single WriteConsoleOutputW over a stack-allocated
CHAR_INFO buffer, combining character and attribute writes. With up
to 8 visible completion rows this halves the per-frame syscall count
for the popup.

4. Efficient string padding for completion rows

The per-character while (size < CMD_COLUMN_WIDTH) line += L' ' loop
is replaced with line.append(n, L' '). line.reserve(W) is added
upfront to avoid repeated reallocation during row construction.

Testing

  • Builds cleanly under MSVC with the dev preset (no errors, no new warnings).
  • Interactive behavior (completion popup, history, cursor movement) is
    unchanged; only the rendering path is affected.

- Add completions_dirty flag: recompute completions only when buffer
  changes (insert, backspace, tab, history nav), not on every event
- Avoid double UTF-8 conversion in redraw(): convert buffer to wstring
  once and reuse in both step 3 (render) and step 5 (cursor restore);
  use MultiByteToWideChar() to compute wchar cursor offset in-place
- Replace FillConsoleOutputAttribute + WriteConsoleOutputCharacterW
  with a single WriteConsoleOutputW call in writeAt(), halving Win32
  syscalls for each completion row drawn
- Replace per-char while-loop padding with wstring::append(n, L' ')
  and add reserve(W) to avoid reallocation when building each row
…fallback

- add REPL sudo rewrite: `sudo <builtin> ...` -> `sudo "<winuxcmd.exe>" <builtin> ...`
- ensure Windows native sudo can elevate and execute WinuxCmd built-ins (e.g. lsof)
- route shell meta commands (`|`, `>`, `<`, `&`) to native fallback in REPL
- keep direct built-in dispatch for normal single commands unchanged
@arookieofc

Copy link
Copy Markdown
Member Author

Fix Windows sudo compatibility for WinuxCmd built-ins in REPL

Summary

This PR fixes a Windows-specific REPL issue where sudo <winuxcmd built-in> failed with "command not found" (e.g. sudo lsof -p 6424).

Root Cause

In REPL mode, built-in commands are dispatched internally and do not necessarily exist as standalone executables (lsof.exe may be missing).
Windows native sudo expects an external command path and therefore failed.

Changes

  • Added REPL rewrite logic:
    • sudo <builtin> ... becomes sudo "<absolute path to winuxcmd.exe>" <builtin> ...
  • Added shell-meta fallback in REPL:
    • commands containing |, >, <, & are executed via native fallback (cmd.exe /c)
    • fixes cases like ps -ef | grep winuxcmd not being piped correctly

Behavior After

  • sudo lsof -p <pid> works on Windows (native elevation path)
  • pipeline commands in REPL are executed correctly through shell fallback
  • normal non-meta built-in commands remain on fast internal dispatch path

Verification

  • Rebuilt target: cmake --build build-dev --target winuxcmd --config Debug
  • Manual checks:
    • sudo lsof -p <pid>
    • ps -ef | grep winuxcmd

@caomengxuan666
caomengxuan666 merged commit 02973e5 into unixwin:main Mar 20, 2026
4 checks passed
caomengxuan666 added a commit that referenced this pull request Mar 24, 2026
…l-integration

perf(readline): optimize per-keystroke rendering and completion latency
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