r/rust • u/Negative_Effort_2642 • Aug 19 '26
💡 ideas & proposals re-allocating" storage for a local could allow faster code
Rust already knows when a value has been moved, so I think it would make sense for the compiler to also be able to treat the storage behind that local as reusable.
For example:
´´´
let x = big_value();
let y = x; // x is moved
// x can no longer be used here anyway
x = another_value();
´´´
Right now, Rust can be more restrictive than necessary about keeping the same storage associated with `x`.
Issue #61849 proposes allowing the old storage to effectively die after the move. If `x` is initialized again later, the compiler wouldn't necessarily have to put the new value back in the exact same stack slot.
That could give the compiler more freedom to:
- reuse stack space earlier
- reduce stack usage in some functions
- shorten lifetimes of stack allocations
- potentially unlock further optimizations
What I like about the idea is that it matches how moves already feel in Rust: once a value is moved, that value is gone. It seems natural that its storage shouldn't have to remain special either.
There are obviously details around raw pointers and observable addresses that would need proper language semantics, so it isn't just a simple compiler optimization.
But the general rule seems very appealing:
If Rust says the old value no longer exists,
the compiler should be free to stop preserving its storage.
The issue has been open since 2019, and I think it would be interesting to revisit whether this could give modern rustc more optimization freedom. If you agree please react on the GitHub issue with ❤️ or 👍 to show support by the community
14
u/QuasiRandomName Aug 19 '26
Is this Rust or LLVM behavior?
20
u/afdbcreid Aug 19 '26
The modern version of the issue is https://github.com/rust-lang/rfcs/pull/3943, which has an implementation PR showing promising speed gains.
31
u/sephg Aug 19 '26
Recycle storage after move #61849
9
u/sphen_lee Aug 19 '26
Not sure this is worth the complication...
If
xis never used again after being moved out of, then LLVM usually notices and will reuse the stack space.If
xis used again (re-initialized) then what would be the gain of using its stack space in the span when it wasn't initialized? It would be really surprising that a local variable is in a new address just because of a move.This is not the same as shadowing (a new
letbinding) - it totally makes sense that this would get a new address.2
u/Kyyken Aug 19 '26
catch_unwind implements something like this manually for different types with a union ^^
-2
u/RRumpleTeazzer Aug 19 '26
you don't gain speed by saving on stack space.
adding 27 to the stack pointer takes the same time as adding 17.
to gain speed, you need to move from stack to registers.
0
u/zesterer Aug 19 '26
Why do you suppose that the code would actually be faster? Seems to me that reusing the stack space is actually going to pessimise performance because now the CPU can't do parallel instruction dispatch as effectively because you've got a bunch of false dependencies between operations that touch stack memory.
Allocating more stack space is pretty much entirely free: you just decrement the stack pointer by a slightly larger number when entering a function.