Skip to content

Add an easy way to get details of a type's size and layout in memory #37623

Closed
@nnethercote

Description

@nnethercote

Multiple times recently, while looking at #36799, I have wanted to know exactly how a type is laid out in memory, including padding and unused bytes. Particular for enums where the variants can vary significantly in size, and shrinking large variants can be a win.

I have successfully shrunk some types (#37445, #37577). I manually worked out their memory layouts by using a combination of println! statements and staring at the code. I can usually work out the layout, though not always. (E.g. I know that Rvalue is 152 bytes on 64-bit platforms, and that the BinaryOp and CheckedBinaryOp variants are the largest, but I haven't yet worked out why they take that much space because the nesting of the types within those variants is deep and non-obvious.)

But it is a huge pain to do it manually, as well as unreliable. It would be lovely if there was an easy way to get this information. Something like std::intrinsics::type_name, perhaps. Such a feature would have wide utility for developers of many Rust programs.

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