Skip to content

RangeInclusive and Range bits are not optimizes a way but also not used in a function #116861

Closed

Description

I tried this code: Also godbolt link https://rust.godbolt.org/z/9qs4cTP4v

pub struct MB {
    addr: u8,
}

pub fn new(addr: u8) -> Option<MB> {
    if !(1..=247).contains(&addr) {
        return None;
    }
    Some(MB { addr })
}

I expected to see this happen:

I was trying this code to see what kind of assemble the compilers makes.
I tried different optimize levels.
I see with optimize levels s and 1, that the RangeInclusive bits are still there but not used in the function.
function is self takes about 5 instructions and the RangeInclusive code about 40 instruction, which is dead code.

Instead, this happened:
RangeInclusive bits removed in all the optimize levels.
I was using a similar code on a embedded device.

FYI: It also happens with Range . Remove the = before 247.

Meta

I see this happen on stable and nightly both for x86_64 and thumbv6/v7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-heavyIssue: Problems and improvements with respect to binary size of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions