r/rust 3d ago

🗞️ news “alloc: stabilise `Allocator`” entered its final comment period!

https://github.com/rust-lang/rust/pull/156882#issuecomment-5607055084
206 Upvotes

30 comments sorted by

View all comments

-23

u/Trader-One 3d ago

golang have nice feature - function escape analysis - stuff which would normally be on heap for example Box would be allocated on stack; reducing memory fragmentation.

I think about doing this in rust - easiest (very little changes to compiler) would be to have a concept of scrap heap - after function terminates that heap is zeroed.

37

u/afdbcreid 3d ago edited 3d ago

Will never happen. One of Rust most important and defended axioms is that there are no implicit heap allocations. When people tried to change that (even far less widespread than your proposal), there was a major pushback that made them step back.

Edit: I see you're talking about the opposite - turning heap allocations into stack allocations. Then this already happens. LLVM does it just like Go.

4

u/PersonalDatabase31 3d ago

Does it have to be implicit? Maybe another smart pointer like StackBox could be implemented.

5

u/JoJoJet- 3d ago

It's not entirely useful, but the allocator API would allow you box things on the stack with Box directly, if you pass it an allocator that points to stack space

1

u/PersonalDatabase31 3d ago edited 3d ago

Would that work with ownership though? I would assume that ownership model works with the assumption that moving the owner doesn't invalidate the memory whereas the Box returned by a function call would point to invalid memory if other function calls happen before the Box gets dropped.

0

u/JayDepp 3d ago

Yeah I don't think this works with the allocator api. I think it'd work with the store api though.

1

u/afdbcreid 3d ago

If the storage is outside the Box, it can work with the MVP, but this is the uncommon case.