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

Enums that are #[repr(u8)] are slower than u8 #3471

Open
jsalzbergedu opened this issue Aug 30, 2024 · 2 comments
Open

Enums that are #[repr(u8)] are slower than u8 #3471

jsalzbergedu opened this issue Aug 30, 2024 · 2 comments
Labels
[C] Bug This is a bug. Something isn't working.

Comments

@jsalzbergedu
Copy link
Contributor

jsalzbergedu commented Aug 30, 2024

The following code:

#[allow(non_upper_case_globals)]
struct DrSeuss;
impl DrSeuss {
    const Thing1: u8 = 0;
    const Thing2: u8 = 1;
}

#[kani::proof]
fn main() {
    let mut haystack = [DrSeuss::Thing1; 100];
    let needle = DrSeuss::Thing2;
    haystack[kani::any_where(|idx: &usize| *idx < 100)] = needle;
    let mut i = 0;
    let mut count = 0;
    while i < 100 {
        if haystack[i] == needle {
            count += 1;
        }
        assert!(count < 2);
        i += 1;
    }
}

Is faster than the following code:

#[repr(u8)]
#[derive(Copy, Clone, Eq, PartialEq)]
enum DrSeuss {
    Thing1,
    Thing2,
}

#[kani::proof]
fn main() {
    let mut haystack = [DrSeuss::Thing1; 100];
    let needle = DrSeuss::Thing2;
    haystack[kani::any_where(|idx: &usize| *idx < 100)] = needle;
    let mut i = 0;
    let mut count = 0;
    while i < 100 {
        if haystack[i] == needle {
            count += 1;
        }
        assert!(count < 2);
        i += 1;
    }
}

This difference scales, and if you add an extra 0, it is 2 seconds difference on my machine.

It appears that the generated code, which I'm looking at through --gen-c, keeps a "struct" and never uses the u8 layout.

Kani version: 0.54.0

@jsalzbergedu jsalzbergedu added the [C] Bug This is a bug. Something isn't working. label Aug 30, 2024
@tautschnig
Copy link
Member

The first code snippet appears to have some copy&paste problem, can you please check the first line?

@jsalzbergedu
Copy link
Contributor Author

@tautschnig fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C] Bug This is a bug. Something isn't working.
Projects
None yet
Development

No branches or pull requests

2 participants