-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Begin exercising rustc's right to reorder struct fields for optimal padding #28951
Comments
Let's use this as a tracking issue to begin discussing blockers that need to be resolved. @eddyb mentions that we should start by removing trailing padding... whatever that means. :P |
C mixes pointers and arrays in various ways and thus requires that the size of any type is a multiple of the alignment. |
@eddyb |
I was thinking only about pointers + |
A quick grep across stdlib tells that whole libcollections and libarena is one big fragile use. |
BTW, what is the algorithm for the optimal structure packing? (Let's start with "in-array" packing, which is more important to optimize.) |
NP hard problems are not really concerning if |
This is an interesting point. The user-defined allocator API that I've been working on has some utility methods for doing stuff with sizes and alignment; I, like @eddyb, had assumed that we should make But after seeing the discussion here, I am wondering if that is wise ... given the precedent that has been set by C, and the likelihood of widespread assumptions about the |
I am wary about having the same type T with different layout or size depending on the context where it is used (array vs single value, for example). Nonetheless AFAICT there should be no problem with rearranging the fields of a struct as long as the layout (padding included) is the same for all of the uses of the type. |
@ranma42 |
Some notes from the Swift ABI related to this. |
Actually I don't think we need to make the struct packing algorithm part of the ABI. Instead we can just put the chosen struct layout in the metadata of the crate which defines the struct. The only downside is that two different structs with the same contents are not guaranteed to have the same layout. |
@Amanieu At least in theory, layouts have to be deterministic, but can depend on the crate SVH (as a seed). |
I think #37429 addresses this for the most part, but note that it's being turned off by #38523. There is a thread on it here. There is more exotic stuff being discussed here, in particular the Swift ABI. I think it might be worth doing someday, but I doubt it will help much. Especially as compared to the amount of work required to make it happen given that it wasn't planned for from the start, and the potential pessimization of using packed structs for everything in LLVM. That said, I thought field reordering would help tremendously and it didn't help tremendously, so perhaps I'm wrong about this too. @bstrie wants to know if they can close this; I'd leave it open for now. The functionality is in and working, but it's not rolled out yet. |
I think we turned this back on with optimization fuel; I'm uncertain whether there's any reason to keep this open. cc @camlorn @eddyb |
Yes, in #40377. |
Yeah, someone can close this. We can technically do better, but not without a major refactor on order with the one I already did, and not without losing performance elsewhere. Since we already do better than most other languages (the only one I know of that does better is Swift), I think it's more than sufficient. |
The representation of structs that are not tagged with a repr attribute is currently and deliberately undefined, in order to allow us to optimize the representation to automatically reduce memory usage due to excess padding (as described in http://www.catb.org/esr/structure-packing/ ). I don't believe we're currently exercising this anywhere, which could present a subtle backcompat hazard if people are using
unsafe
code whose semantics relies on the unstable representation. Even if we've technically reserved the right to break this sort of code, failing to act soon enough could potentially make such breakage more than we can tolerate, tying our hands.I don't believe that this requires an RFC, as it's a longstanding design decision and the "ideal" representation is fairly straightforward and uncontroversial (issues like false sharing notwithstanding).
The text was updated successfully, but these errors were encountered: