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

Collapse trailing padding #17027

Closed
retep998 opened this issue Sep 5, 2014 · 10 comments
Closed

Collapse trailing padding #17027

retep998 opened this issue Sep 5, 2014 · 10 comments
Labels
A-codegen Area: Code generation C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-llvm Working group: LLVM backend code generation

Comments

@retep998
Copy link
Member

retep998 commented Sep 5, 2014

Since we don't have to worry about FFI interop for types that are not #[repr(C)], this means we can perform certain optimizations with regards to padding. One such optimization is collapsing trailing padding. For example std::mem::size_of::<((u16, u8), u8)>() returns 6, but ideally it would be 4.

To implement this size optimization would require keeping track of two separate sizes, one being the actual size not including trailing padding, and the other being the array element size or aligned size. For example (u16, u8) would have an actual size of 3, but an array size of 4.

As an added benefit, this would allow for enums to use that extra space at the end to store the discriminant.

This likely depends on teaching LLVM to support this sort of stuff.

@retep998
Copy link
Member Author

retep998 commented Sep 5, 2014

cc @eddyb

@steveklabnik steveklabnik added the A-codegen Area: Code generation label Jan 27, 2015
@steveklabnik
Copy link
Member

Triage: not aware of any changes.

@Mark-Simulacrum
Copy link
Member

Should this be closed in favor of the RFC issue: rust-lang/rfcs#1397?

@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 22, 2017
@kennytm
Copy link
Member

kennytm commented Dec 28, 2017

Just for record, std::mem::size_of::<((u16, u8), u8)>() is still 6 on the current nightly, unaffected by #45225.

@nox
Copy link
Contributor

nox commented Mar 31, 2018

Cc @rust-lang/wg-codegen

I'll look at this given I'm having fun in the layout module these days.

@nox
Copy link
Contributor

nox commented Mar 31, 2018

Though I don't think it's something where we can just change the layout to be clever and expect everything else to work. What happens if you have a value coming from a [(u16, u8)] slice and you want to write that value in the first element of the ((u16, u8), u8) tuple?

@oli-obk oli-obk added the WG-llvm Working group: LLVM backend code generation label Mar 31, 2018
@petrochenkov
Copy link
Contributor

I think we should add an attribute for this, something like #[no_trailing_padding], then wait until all unsafe code in the ecosystem supports it and stops doing things equivalent to memset(&x, sizeof(X)) (maybe few years) and only then maybe make #[no_trailing_padding] a default.

@hanna-kruppe
Copy link
Contributor

hanna-kruppe commented Apr 2, 2018

To enable code to handle types with "no trailing padding" correctly, we need to introduce a distinction between "size" and "stride" like Swift has. Theoretically possible but a lot of churn and very hard to ensure all code is updated.

@hanna-kruppe
Copy link
Contributor

And also note the downside noted in rust-lang/rfcs#1397

@Dylan-DPC
Copy link
Member

Closing this in favour of the rfc listed above: rust-lang/rfcs#1397

@Dylan-DPC Dylan-DPC closed this as not planned Won't fix, can't repro, duplicate, stale Nov 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-llvm Working group: LLVM backend code generation
Projects
None yet
Development

No branches or pull requests

10 participants