Skip to content

Missed optimization when putting structs with padding an enum #109555

Closed as not planned
@HomelikeBrick42

Description

@HomelikeBrick42
struct S {
    a: u32,
    b: i128,
}

enum Foo {
    Var1(S),
    Var2(S),
}

use std::mem::size_of;
dbg!(size_of::<S>());
dbg!(size_of::<Foo>());

this code prints

[src/main.rs:13] size_of::<S>() = 24
[src/main.rs:14] size_of::<Foo>() = 32

when it could have instead used the free padding bytes in S for the discriminant, so Foo would be the same size as S

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions