Windows: cache the stdio write mode for the duration of a lock session#159562
Open
Joel-Wwalker wants to merge 1 commit into
Open
Windows: cache the stdio write mode for the duration of a lock session#159562Joel-Wwalker wants to merge 1 commit into
Joel-Wwalker wants to merge 1 commit into
Conversation
Every write to stdout/stderr re-queried GetStdHandle and GetConsoleMode (and GetConsoleOutputCP for consoles) to decide how to write, which dominates bulk writes through a locked handle. Cache the handle and the console verdict in the sys-level Stdout/Stderr and re-query when a new lock session begins, so SetStdHandle calls made between lock sessions keep working.
Collaborator
|
r? @aapoalas rustbot has assigned @aapoalas. Use Why was this reviewer chosen?The reviewer was selected based on:
|
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.
On Windows, every write to stdout/stderr calls
GetStdHandleandGetConsoleMode(andGetConsoleOutputCPfor consoles) to decide how to write. For bulk writes through a locked handle that is most of the work; #154071 measured 14% of CPU time inis_consolealone.This caches the handle and the console verdict in the sys-level
Stdout/Stderrand re-queries when a new lock session begins, as suggested in the issue. Holding aStdoutLockpins the stream; unlocked writes acquire the lock per call, so they re-query per write exactly as before, andSetStdHandlecalls made between lock sessions keep working (the #40490 behavior is preserved).Numbers from the issue's workload shape (16 KiB chunks to
NUL, 4 GiB total, Windows 11):stdout().lock()The unlocked case also improves because one implicit lock session can contain several sys-level writes when
LineWritersplits a chunk at newlines.The console path cannot run in CI, so it was verified under a pseudoconsole: several writes through one lock session on a CP437 console came through
WriteConsoleWintact (non-ASCII included), and afterSetConsoleOutputCPplus a new lock session the refresh picked up the change.The
refreshhook is a small#[cfg(windows)]shim onStdoutRaw/StderrRaw; happy to change it to a method on every platform's sys type instead if that is preferred.Fixes #154071