`<print>`: Optimize no-argument `print()` brace handling #6465
All Vouch review confidence checks passed
Residual risk 0.33 = Change Risk (0.60) × (1 − Review Confidence (0.45)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.60 · 551 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.378
Shallow Review Depth
shallow_review_depth
0.344
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #6465"
general
weight: 0.35
YexuanXiao
<!-- Before submitting a pull request, please ensure that:
Any AI-generated code has been clearly disclosed in your PR description.
We strongly discourage AI-generated changes to product code because the STL
is a highly unusual codebase with complex correctness and performance
requirements imposed by the Standard, and highly unusual code patterns that
don't look like typical codebases. Our intensive code review process
(to avoid bugs) is intentionally a bottleneck, so we ask that you be
considerate of the time and effort that we'll need to spend, by starting
with changes that you fully understand. Test code has weaker requirements,
so AI-generated changes for tests can be acceptable if properly disclosed.
Your PR description explains your intent behind the changes.
We discourage AI-generated PR descriptions because we can read the changes,
and we have access to the same AI tools that you have, so we can get
an AI-generated summary if we want.
Identifiers in product code changes are properly
_Uglyas perhttps://eel.is/c++draft/lex.name#3.1 or there are no product code changes.
These changes introduce no known ABI breaks (adding members, renaming
members, adding virtual functions, changing whether a type is an aggregate
or trivially copyable, etc.).
Your changes are written from scratch using only this repository, the C++
Working Draft (including any cited standards), other WG21 papers (excluding
reference implementations outside of proposed standard wording), and LWG
issues as reference material. If your changes are derived from a project
that's already listed in NOTICE.txt, that's fine, but please mention it.
If your changes are derived from any other project, you must* mention it
here, so we can determine whether the license is compatible and what else
needs to be done.
-->
Optimization for std::print calls with no format arguments, ~~no new lines~~ and no escaped braces. Current code was allocating an intermediary string by calling _Unescape_braces even if the format string contained no such braces. This might seem like a very niche and constrained case but in my opinion it represents the most common real world case of a print call without format arguments. Allocating an unnecessary string for std::print("Hello long string world ... ") is surprising imo.
Optimization is simple in theory, a bit trickier to write down in practice... We do an iteration looking for escaped braces in the format string and if we don't find any, we just call _Vprint... with the string_view. An extended optimization is made here such that if we do find an escape brace at the end of a long string, we don't waste the compute of the search, _Find_escape_braces returns the offset of the found "{{" or "}}" and _Unescape_braces signature is extended with _Escape_brace_offset parameter. The substring _Old_str[0, _Escape_brace_offset) is then copied directly to the resulting string since we've guaranteed no "{{" or "}}" were found in this prefix range.
Edit: after gaining a better understand of the msvc stl\stl\src\print.cpp design I realize that the optimization can be extended to println version too, now that the noformat path doesn't allocate indiscriminantly we can expand _Vprint_unicode_noformat_impl and friends to match _Vprint_unicode_impl and pass the new line flag.
~~Existing tests already offer enough validation as far as I can tell.~~
Added test for introduced edge case discovered by the bot. We create a console-backed std::ostream and print a default-constructed empty string_view. Afterwards we check that print/println leave the stream in a good state, then print a marker and verify it appears on the line after the empty println.
Codex was used for local repo setup, PR guideline summaries, searching for relevant tests and evaluating impact outside the scope of the enhancement. It was also used for local benchmarks but they were not satisfying enough to add in the PR since they were very prone to noise. These were the most reliable numbers (sending output to a buffered NUL stream):
| Benchmark | Old | New | Result |
|---|---:|---:|---|
|
std::print— long, plain | 692 ns | 632 ns | Improved ||
std::print— long, escaped at the end | 672 ns | 677 ns | Matching is good ||
std::println— no escapes | 903 ns | 834 ns | Improved |