|
|
Log in / Subscribe / Register

Current state of in-place initialization

Current state of in-place initialization

Posted Oct 11, 2026 6:40 UTC (Sun) by matthias (subscriber, #94967)
In reply to: Current state of in-place initialization by donald.buczek
Parent article: An update on Rust's project goals

The compiler will optimize the move away, whenever it sees the option to do it. But it is not enough to make sure that this optimization has occurred.

We are talking about types that are not allowed to be moved. So the code might be unsound (contain UB) before this optimization. So this optimization has to be performed before any other optimization that might trigger this UB. This optimization would also need to be applied in debug mode where optimizations are turned off.

This might sound plausible. But the biggest issue is: Why do we need in-place initialization in the first place? Usually for self-referential types. So the initialization code will at some point take the address of the struct and write it into the struct. In the unoptimized code this initialization happend before the move. So it is not only necessary to optimize the move, but we must also make sure that the optimized code never sees the address the struct has on the stack. By some compiler magic it would need to initialize the struct with the address that it will get on the heap (or wherever it will end up), from some allocation that might happen after the initialization in program order. And the compiler cannot simply reorder initialization and allocation, as the memory allocator surely involves some memory barriers.

I have to admit that I have no expertise in compiler development. But I am quite sure that you do not want to rely on compiler optimizations to remove UB from your code. The unoptimized code should never have UB in the first place.


to post comments

Relying on optimizations to remove UB

Posted Oct 11, 2026 9:20 UTC (Sun) by farnz (subscriber, #17727) [Link] (2 responses)

Your final paragraph underplays it, if anything; relying on optimizations to remove UB is a bad idea because of how UB is defined; by definition, the optimizer could choose another route that removes the UB from the final executable, even if it's not what you want, since it can do anything that replaces UB with defined behaviour (even if it's unwanted, like the "nasal demons" traditional quip), but can't add new UB where the language defined the meaning of the program already.

You need the language to define the meaning of your code so that the optimizer has only one allowed route for handling this code, otherwise you risk a future change (even something as "trivial" as rearranging the order of optimization passes to reduce optimizer run time) breaking your output binary.

Relying on optimizations to remove UB

Posted Oct 12, 2026 19:13 UTC (Mon) by NYKevin (subscriber, #129325) [Link] (1 responses)

It may sound strange, but there is precedent for it. Option<T> provides a number of guaranteed niche optimizations that make certain forms of type punning legal. If you have the Some() variant of Option<&U>, it may be soundly cast to and from &U, because Rust guarantees that the discriminant will not be emitted as a separate field in that case (instead, the None variant is represented as a null pointer, which is not a legal value for &U, so there is no ambiguity). See https://doc.rust-lang.org/std/option/index.html#represent... for details.

However, I do agree that this probably shouldn't be stretched to include things like optimizing a move into an in-place initialization. There is just too much potential for things to go wrong.

Relying on optimizations to remove UB

Posted Oct 13, 2026 13:01 UTC (Tue) by farnz (subscriber, #17727) [Link]

Those are, however, behavioural guarantees from the language, and not just "the optimizer happens to get this right". Casting Option<&T> to *const T, and Option<&T>::Some(&value) to &T is defined as sound by the language, rather than being UB that the compiler happens to interpret in an acceptable way.

And that's really the point here - guaranteeing that something that would normally be UB is defined in this particular case is fine (as happens with Option<&T> - this is defined behaviour, even if transmuting Option<T> to T is not always defined behaviour). Relying on the optimizer spotting that this is normally UB, but that you meant something specific, is not fine.

The same would work for in-place initialization; relying on the compiler spotting that you "meant" a move to be an in-place initialization because otherwise you have UB is not OK, but defining something that would otherwise be UB to mean "in-place initialization" is fine. The important bit is that the language specification (informal or formal) says that this is defined behaviour, not just the optimizer picking up something that could be UB and defining it in a way you like today.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds