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

build/i2c: generate range matches #1720

Merged
merged 1 commit into from
Apr 4, 2024
Merged

Conversation

cbiffle
Copy link
Collaborator

@cbiffle cbiffle commented Apr 4, 2024

We've noticed the compiler tends to generate better code for integer range matches of the form

match x {
    0..=7 => a,
    7..=10 => b,
    _ => c,
}

as compared to

match x {
    0
    | 1
    | 2
    | 3
    | 4
    | 5
    | 6
    | 7 => a,
    // and so on
}

Also, Clippy in recent nightly has started objecting to the "cascade of digits" form. Rather than suppressing that warning, I thought I'd convert the biggest matches in our codegen output to use ranges.

This commit uses rangemap to automatically consolidate adjacent ranges of integers into a convenient match statement. I've also factored out the code from the two places it was used.

@cbiffle cbiffle force-pushed the cbiffle/i2c-config-range-matches branch from bf15907 to 467f474 Compare April 4, 2024 19:08
@cbiffle cbiffle requested a review from hawkw April 4, 2024 19:09
build/i2c/src/lib.rs Outdated Show resolved Hide resolved
We've noticed the compiler tends to generate better code for integer
range matches of the form

    match x {
        0..=7 => a,
        7..=10 => b,
        _ => c,
    }

as compared to

    match x {
        0
        | 1
        | 2
        | 3
        | 4
        | 5
        | 6
        | 7 => a,
        // and so on
    }

Also, Clippy in recent nightly has started objecting to the "cascade of
digits" form. Rather than suppressing that warning, I thought I'd
convert the biggest matches in our codegen output to use ranges.

This commit uses rangemap to automatically consolidate adjacent ranges
of integers into a convenient match statement. I've also factored out
the code from the two places it was used.
@cbiffle cbiffle force-pushed the cbiffle/i2c-config-range-matches branch from 467f474 to 07cdfbb Compare April 4, 2024 22:52
@cbiffle cbiffle enabled auto-merge (rebase) April 4, 2024 22:52
@cbiffle cbiffle merged commit 4303eb3 into master Apr 4, 2024
103 checks passed
@cbiffle cbiffle deleted the cbiffle/i2c-config-range-matches branch April 15, 2024 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants