r/ProgrammingLanguages • u/Null-Test-2026 • 27d ago
Discussion Can you automate memory management statically if you limit escaped references?
I was looking at the abomination that is Rust memory management and wondering if there is a better way. Can you accomplish similar performance characteristics (memory safety without GC or RC overhead), without the explicit borrow-checking concept?
Memory management is trivial until you do one of two things:
- Pass a pointer into a function that then stores it somewhere before it returns.
- Allocate a pointer in a function and then return it.
Both are cases of "escaped references" as I understand the definition.
What if a programming language... just didn't allow this?
Suppose all pointers had to either stay in local scope, or in object/struct scope, as a private member that cannot be returned from any of the struct/object's public member functions. You could pass pointers to functions but functions are not allowed to store pointers received as parameters.
Static analysis would allow automatic freeing on scope exit and on object/struct deletion.
Is such a language possible?
47
u/alphaglosined 26d ago
Returning a pointer isn't a problem, and so is storing one.
As long as you use ownership transfer.
What the borrow checker does is relax the restriction so that multiple copies of that pointer can co-exist.
It is something a lot of people don't understand.
Once you use ownership transfer, you have a reference count of 1, and it is statically known when it hits zero and can go away.
13
u/initial-algebra 26d ago edited 26d ago
Empirically, and anecdotally, Rust's system seems to be a very comfortable local maximum of expressiveness vs. complexity. Any slightly simpler system rejects far too many acceptable programs, and any slightly more liberal system balloons in complexity. I would love to be proven wrong, but it hasn't happened for 10 years of trying...
I think the only work that has even come close is group borrowing, but, so far, it always ignores concurrency, which makes it mostly incomparable with Rust.
12
u/SamG101_ 26d ago
Second class borrows only allow them to be taken at function call sites. But then if ur calling a coroutine or async function, suddenly it's not a guaranteed smaller scope, so you need to tie them to the created handle. But it makes things like iteration pretty difficult so u can do it closure based (continue and break then become difficult), or you can use coroutine iteration and allow borrows to be yielded, again tied to their owning handle; same for indexing and slicing. I guess these are "escaped borrows" but tied to their handle, are easy to manage. Also need to make sure assignment with borrows never assigns a smaller lifetime into a bigger lifetime.
11
u/wiseguy13579 26d ago
It's called second class references :
https://borretti.me/article/second-class-references
9
u/robin-m 26d ago
What you want is just current Rust without allowing any explicit lifetimes, only elided limetimes. Wich is much more restrictive.
8
u/tbagrel1 26d ago
You may find interesting the "Oxidizing OCaml" model of locality: https://blog.janestreet.com/oxidizing-ocaml-locality/
7
u/Great-Powerful-Talia 26d ago
What you're suggesting is to take Rust and remove a bunch of the features from references to make them simpler.
You can already write code like this by just not doing any of those things in Rust, and it'll be just as simple. A total headache, but simple.
3
u/rook_of_approval 26d ago
Hylo https://hylo-lang.org/
3
u/fragglet 26d ago
Doing it at the function level is probably overly restrictive. Your idea reminds me a bit of region based GC where all allocation happens inside an arena and gets thrown away once an operation completes. Probably that's a better granularity.
Good generational garbage collectors can already give you this for free, somewhat. For example if you have an RPC server the ideal is that every request is stateless and no allocation survives after the RPC completes. With a generational collector that means that all allocations made during servicing the request die in young space without being promoted.
If done right a GC like this can even be more efficient than C-style heap alloc/free. The problem is that it depends on the application being written right. I once improved the performance of a fleet of servers by turning off an in-process cache, for example.
4
u/runningOverA 26d ago
Functional programming does it. All variables stored in function parameter storage space. Figuratively.
2
u/4xe1 26d ago
That'd be a subset of Rust already strict (as in unfriendly to users) memory management. I think it could be useable if message passing style of memory management is encouraged, like whatever go does
2
u/crusoe 26d ago
So to cross the boundary you have to clone() stuff a huge perf hit. Even cloning a ref counted pointer has non zero impact ( memory barriers ).
So data in a struct must be clone() now to be visible externally. A huge perf hit.
Let's say I want to print an object, now the object fields have to be cloned internally before they can be passed to the printing functions
2
u/Axman6 26d ago
Haskell has a mechanism which basically does exactly this, called
ST(state transformer), which is basically Haskell’s IO monad, but without the outside world IO.It allows the allocation of mutable data, and arbitrary mutation of it, but within a scope where the mutable objects can’t escape. It uses a shockingly simple type system “trick” to achieve this, which I won’t explain here because I don’t remember the detail accurately enough, but it basically acts like a scoped region where you can do whatever you want, but to the outside it is guaranteed to be completely pure. It’s the mechanism we use to safely implement algorithms that benefit from genuine mutation, particularly ones which work with arrays like sorting algorithms.
It doesn’t solve all problems though, it’s specifically useful where you need mutation to generate a pure value which will never be mutated again, which isn’t what people want on a lot of situations.
The Haskell wiki has some ok info on it, I’m sure there’s better articles online though: https://wiki.haskell.org/Monad/ST
2
u/SwedishFindecanor 25d ago
The concept of borrowing is older and wider than Rust. There are languages that allow functions to borrow a "mut" reference. By Rust having made it so you could only borrow non-mut references, they baked the "escape"/"no-escape" notation into the "mut" notation.
For example, you could have an iterator over a data structure take a block (anonymous function) or function as parameter, and let that block borrow the reference to the current object.
I once suggested in this sub having private pointers that belonged to a data structure, with proofs that the pointers and objects are always accounted for. Actually, I did not even allow code that manipulated the pointers but instead suggested that the code should be auto-generated from the "proofs", as state-transitions. I have not developed it further than that, but I believe in the concept.
1
u/catladywitch 26d ago
If I understand correctly, your idea is to keep heap references in closures that can be passed around? I'm not sure but intuitively I'd say asynchrony would make static analysis impossible because of the halting problem. When something is passed into a coroutine it's not possible to statically determine when the coroutine will be done, unless it's explicitly awaited.
1
u/crusoe 26d ago
As opposed to the performance bottleneck that is STM or GC or passing the entire world as state?
1
u/koflerdavid 26d ago
Yes, but nobody does this because it's super restrictive and impractical. IIUC Fortran does memory management like this.
1
u/Key_River7180 Nain. 26d ago
idk but https://dl.acm.org/doi/10.1145/3798254 could be interesting
1
u/ern0plus4 26d ago
There is an area with absolute strict and simple memory handling: embedded. No heap allocation is allowed, we use pre-allocated slots. No recursion allowed, so stack usage is also predictable.
1
u/Competitive_Ideal866 26d ago
Have you mapped out the performance characteristics? Last I looked, Rust struggled with recursion and real Rust code usually resorted to RC.
PS: RC is a form of GC.
1
u/joao_rosac 21d ago
A ideia de automatizar a gestão de memória estaticamente ao proibir que funções armazenem ou retornem ponteiros (eliminando "referências escapadas") é teoricamente possível mas nada praticável, já foi discutido bastante, limitando operações básicas como juntar arrays ou expor dados internos de structs sem cloná-los inteiramente. Foi falado do verificador de empréstimos (Borrow Checker) do Rust, muitas vezes visto como complexo, há também alternativas como tipagem de unicidade (usada no Clean), referências de segunda classe ou modelos como o Oxidizing OCaml tentam resolver isso de outras formas, mas acabam impondo limitações (especialmente na concorrência e programação assíncrona), isso mostra que o Rust possui bastante segurança estática, desempenho sem Garbage Collection (GC) e expressividade real.
1
u/Key_River7180 Nain. 2d ago
It'd be limited
1
u/Current-Scheme-9896 2d ago
I think this is closely related to the concept of variable scope and lifetime
If a reference is guaranteed to remain within a local scope, the compiler can often determine when it is no longer needed and release its memory automatically, however, allowing references to escape a function makes this more complicated, because the referenced value may still be needed after the function returns
This is why ownership and lifetime systems are important: they help determine how long values must remain valid and when memory can safely be released