Add optimized best_fit_parenthesize IR#7475
Conversation
|
Current dependencies on/for this PR: This comment was auto-generated by Graphite. |
4997395 to
4941b6e
Compare
CodSpeed Performance ReportMerging #7475 will improve performances by 9.15%Comparing Summary
Benchmarks breakdown
|
87951d6 to
118ebee
Compare
best_fit_parenthesize IR
| -annotated_variable: Literal[ | ||
| - "fakse_literal" | ||
| -] = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped." |
There was a problem hiding this comment.
These didn't use the best_fitting layout before because they were excluded by the use_best_fit. This is more consistent
| OptionalParentheses::BestFit | ||
| } else { | ||
| OptionalParentheses::Never | ||
| OptionalParentheses::BestFit |
There was a problem hiding this comment.
It's now cheap enough to always use BestFit. We only need to avoid it in cases where we never want parentheses
| comments.has_leading(*expression) || comments.has_trailing_own_line(*expression); | ||
| let node_comments = comments.leading_dangling_trailing(*expression); | ||
|
|
||
| let has_comments = node_comments.has_leading() || node_comments.has_trailing_own_line(); |
There was a problem hiding this comment.
Thanks @charliermarsh for adding these methods. It makes it straightforward to replace one API with the other :)
118ebee to
bfefd85
Compare
927f1f6 to
7419718
Compare
charliermarsh
left a comment
There was a problem hiding this comment.
This looks great, and the speedups are impressive, though I'll admit I'm not very familiar with the printer internals.
7419718 to
fd3d7a5
Compare
fd3d7a5 to
81333ac
Compare
Ports the `best_fit_parenthesize` IR from Ruff (astral-sh/ruff#7475). This is an optimized alternative to using `best_fitting!` for the common pattern of choosing between flat content and parenthesized content: ```rust // Instead of: best_fitting![ content, format_args!(token("("), block_indent(&content), token(")")) ] // You can now use: best_fit_parenthesize(&content) ``` **Behavior:** - If content fits on a single line: prints without parentheses - If content doesn't fit: adds parentheses with indented content **Features:** - Supports optional `group_id` for conditional content that depends on whether parentheses are rendered - Integrates with existing `if_group_breaks`/`if_group_fits_on_line` Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Summary
This PR adds a custom IR for content that should only be parenthesized if it makes it fit. The custom IR removes the need to use the costly
BestFittingIR element, allowing us to remove the poor heuristic around when to use theBestFitlayout (because the IR is now cheap).The main downside of the custom IR is that it is less tested and further increases the complexity of the Printer (It is re-implementation of
BestFittingwith the three variants)Closes #7463
Closes #7067
Closes #7462
Test Plan
cargo testBase
This PR
New transformers deviations: There are a few new transformers deviations of the form:
This is because the
BestFitlayout is now also applied for constants.The formatting is now consistent with a 6 character identifier: