Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optimized best_fit_parenthesize IR #7475

Merged
merged 1 commit into from
Sep 19, 2023
Merged

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Sep 18, 2023

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 BestFitting IR element, allowing us to remove the poor heuristic around when to use the BestFit layout (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 BestFitting with the three variants)

Closes #7463
Closes #7067
Closes #7462

Test Plan

cargo test

Base

project similarity index total files changed files
cpython 0.76083 1789 1632
django 0.99982 2760 37
transformers 0.99957 2587 398
twine 1.00000 33 0
typeshed 0.99983 3496 18
warehouse 0.99929 648 16
zulip 0.99962 1437 22

This PR

project similarity index total files changed files
cpython 0.76083 1789 1631
django 0.99983 2760 36
transformers 0.99956 2587 404
twine 1.00000 33 0
typeshed 0.99983 3496 18
warehouse 0.99929 648 16
zulip 0.99969 1437 21

New transformers deviations: There are a few new transformers deviations of the form:

if True:
    if True:
        if True:
			model.config.use_cache = False  # FSTM still requires this hack -> FSTM should probably be refactored similar to BART afterward

# Formatted
if True:
    if True:
        if True:
			model.config.use_cache = (
				False
			)  # FSTM still requires this hack -> FSTM should probably be refactored similar to BART afterward

This is because the BestFit layout is now also applied for constants.

The formatting is now consistent with a 6 character identifier:

if True:
    if True:
        if True:
            model.config.use_cache = aaaaaa  # FSTM still requires this hack -> FSTM should probably be refactored similar to BART afterward

# Formatted
if True:
    if True:
        if True:
            model.config.use_cache = (
                aaaaaa
            )  # FSTM still requires this hack -> FSTM should probably be refactored similar to BART afterward

@MichaReiser
Copy link
Member Author

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@MichaReiser MichaReiser force-pushed the optional-parentheses-ir branch 2 times, most recently from 4997395 to 4941b6e Compare September 18, 2023 11:28
@codspeed-hq
Copy link

codspeed-hq bot commented Sep 18, 2023

CodSpeed Performance Report

Merging #7475 will improve performances by 9.15%

Comparing optional-parentheses-ir (81333ac) with main (28b48ab)

Summary

⚡ 4 improvements
✅ 21 untouched benchmarks

Benchmarks breakdown

Benchmark main optional-parentheses-ir Change
formatter[pydantic/types.py] 20.8 ms 19.3 ms +7.62%
formatter[large/dataset.py] 57.3 ms 52.5 ms +9.15%
formatter[numpy/ctypeslib.py] 10.7 ms 9.9 ms +7.9%
formatter[unicode/pypinyin.py] 3.6 ms 3.4 ms +7.92%

@MichaReiser MichaReiser added performance Potential performance improvement formatter Related to the formatter labels Sep 18, 2023
@MichaReiser MichaReiser force-pushed the optional-parentheses-ir branch 4 times, most recently from 87951d6 to 118ebee Compare September 18, 2023 15:07
@MichaReiser MichaReiser changed the title Add optional parentheses IR Add optimized best_fit_parenthesize IR Sep 18, 2023
@MichaReiser MichaReiser marked this pull request as ready for review September 18, 2023 15:07
Comment on lines 323 to 325
-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."
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These didn't use the best_fitting layout before because they were excluded by the use_best_fit. This is more consistent

} else {
OptionalParentheses::Never
OptionalParentheses::BestFit
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
Copy link
Member Author

@MichaReiser MichaReiser Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @charliermarsh for adding these methods. It makes it straightforward to replace one API with the other :)

@MichaReiser MichaReiser force-pushed the optional-parentheses-ir branch from 118ebee to bfefd85 Compare September 18, 2023 15:13
@MichaReiser MichaReiser force-pushed the optional-parentheses-ir branch 2 times, most recently from 927f1f6 to 7419718 Compare September 18, 2023 20:03
Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, and the speedups are impressive, though I'll admit I'm not very familiar with the printer internals.

@MichaReiser MichaReiser force-pushed the optional-parentheses-ir branch from 7419718 to fd3d7a5 Compare September 19, 2023 06:16
@MichaReiser MichaReiser force-pushed the optional-parentheses-ir branch from fd3d7a5 to 81333ac Compare September 19, 2023 06:17
@MichaReiser MichaReiser enabled auto-merge (squash) September 19, 2023 06:18
@MichaReiser MichaReiser merged commit 6a4dbd6 into main Sep 19, 2023
@MichaReiser MichaReiser deleted the optional-parentheses-ir branch September 19, 2023 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter performance Potential performance improvement
Projects
None yet
2 participants