Repository navigation
Coalesce source slices when formatting attributes - #29841
Conversation
|
Merging this PR will improve performance by 5.33%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
|
|
||
| a = Namespace() | ||
|
|
||
| # Attribute slices preserve source spelling, including NFKC-normalized identifiers. |
There was a problem hiding this comment.
The tests here feel excessive. They also test more than what was changed in this PR. Are they redundant?
| if value.is_name_expr() | ||
| && !call_chain_layout.is_fluent() | ||
| && &f.context().source()[TextRange::new(value.end(), attr.start())] == "." | ||
| && !f.context().is_expression_parenthesized(value.into()) |
There was a problem hiding this comment.
Is this check necessary? We test that the only text between value and attr is a .. Which means there's no closing parentheses between value and attr.
| && &f.context().source()[TextRange::new(value.end(), attr.start())] == "." | ||
| && !f.context().is_expression_parenthesized(value.into()) | ||
| && !f.context().comments().has(value.as_ref()) | ||
| && !f.context().comments().has_dangling(item) |
There was a problem hiding this comment.
Should we move the dangling declaration further up and use it here, to avoid resolving the item's dangling comments twice?
Summary
Formatting and printing the top-level modules in CPython’s standard library took 3.9% less time in a local benchmark (125.7 ms to 120.8 ms), or 2.4% less with parsing included. CodSpeed reports a 5.1% efficiency improvement on the
large/dataset.pyformatter benchmark.Today the formatter emits a source slice for
a, a dot token, and a source slice forbjust to printa.b. We now emit the whole expression as one slice when the dot touches both names, the first name is unparenthesized, and there are no comments or call-chain breaks to handle. Reading directly from the source also preserves Unicode spellings like𝓪.𝓫.For
factory().attr, we still format the call and handle any line break before the dot, but we can emit.attras one slice. This follows the approach in Oxc #28462.