News for hackers. Stuff that matters.
Re:Memory Management Reference by tomp on Tuesday July 12, 2022 @02:41PM (#32074067)
The most depressing thing about memory management is that there's been 0 innovation in the past 10+ years, and it seems like we're fundamentally at a dead end where only tradeoffs exist.

There are basically 3 options going forward:

- manual memory management (C++, Rust) "I can verify your code but you need to `unsafe` to do anything interesting"

- reference counting (Swift) "I leak memory through cycles"

- tracing GC (Java, Go) "I stop the world and trash your swap"

The rest is engineering (e.g. Go reduced the "stop-the-world" phase to O(1) from O(n) where n is the size of memory)

Re:Memory Management Reference by verdagon on Tuesday July 12, 2022 @04:20PM (#32075129)
(Disclaimer: Vale lead here!)

You might be interested in what we're working on with Vale, its core innovations happen to be around memory management.

Some off the top of my head:

* Generational references, for single-ownership memory safety without garbage collection, ref counting, or borrow checking. [0]

* Higher RAII, a form of "linear typing" that lets us add parameters and returns to destructors, to enforce that if you do X, you won't forget to do Y. [1]

* Regions, which could make it so we can eliminate most generation checks in Vale. If it catches on, it could also help other paradigms and get reference counting back into the fight. (Note, we aren't done with this one yet, it's about 3-8 weeks out) [2]

Those are just a few off the top of my head, there are a lot of crazy ideas floating around there. I'm hoping they'll combine to form something like an easier more flexible Rust, because that's the holy grail of language design, to me at least.

A few years ago, I was also pretty down about the state of memory innovation... it felt like not much had been discovered since borrow checking. I knew there had to be something with its speed, but the flexibility of C++. Perhaps we found it!

[0] https://verdagon.dev/blog/generational-references

[1] https://verdagon.dev/blog/higher-raii-7drl

[2] https://verdagon.dev/blog/zero-cost-refs-regions

Re:Memory Management Reference by whizzter on Tuesday July 12, 2022 @07:13PM (#32077008)
Generational References is quite a neat trick, obvious in hindsight! I assume that the general idea is that non-owners really only have "weak" references (and safety comes from the compiler warning/checking that this isn't broken).

One thing though about them is that I assume the generation to be co-located with the memory? If so, then the runtime wouldn't be able to free memory back to the OS while running? (Unless implemented with a side-lookup but that would hamper runtime performance?)

Re:Memory Management Reference by verdagon on Tuesday July 12, 2022 @07:32PM (#32077172)
Askin' the right questions!

The generations are colocated with the objects, and we can release memory back to the OS using the virtual memory shenanigans explained in [0].

However, the design and implementation have simplified quite a bit since that article. Now:

* We use these generations only for assertions on dereferencing non-owning references, and don't use them for weak references anymore.

* We let malloc release memory back to the OS like normal. If the user dereferences something in a released page, we'll get a page fault instead of an assertion failure, but both are handled the same.

* When the generation hits INT_MAX, we just let it overflow now instead of blacklisting the allocation forever.

These adjustments mean we never need the heap which is a big improvement, and really unlocks Vale as a systems programming language.

The purposeful overflowing does mean that only 99.99999998% of invalid memory accesses will halt the program, which is more than enough certainty in practice [1]. (The probability is actually higher, as we increment generations by prime numbers and won't even hit our first possible collision until well after most runs have ended.)

[0] https://lobste.rs/s/sglvcc/generational_references_2_3x_fast...

[1] https://www.anandtech.com/show/16759/sponsored-post-keep-you...

Re:Memory Management Reference by kaba0 on Thursday July 14, 2022 @04:47AM (#32093607)
I really like the ideas behind Vale, but I don’t understand this:

“These adjustments mean we never need the heap which is a big improvement”

Why would the heap be problematic? It is not different from the stack, there is nothing different between the stack and the heap from the perspective of the CPU. Memory locality is what matters - if you allocate an arena and fill it tightly and refer to elements in it in a predictable manner it will be just as fast.

Re:Memory Management Reference by zozbot234 on Tuesday July 12, 2022 @05:18PM (#32075822)
The real disclaimer should perhaps be that this stuff is something that's still being worked on wrt. production use, just like many other kinds of PL research. Once it's ready, Rust will probably be in a very good position to adopt these features.
Re:Memory Management Reference by verdagon on Tuesday July 12, 2022 @05:33PM (#32075970)
That would be pretty cool! I'm not sure Rust can support these features, though.

* Generational References involve some pretty deep compiler support. One can implement something like it in Rust with a thread-local side table, but with >3x overhead compared to language-supported generational references.

* Higher RAII requires that a language not treat destructors and panic handlers as the same thing. Unfortunately, Rust uses one function (drop) for both. Additionally, it would be a backwards-incompatible change; Rust generic parameters already assume a zero-arg drop() is available for all parameters, even though there's no trait bound declared for them.

* Regions get most of the borrow checker's optimizing power with a fraction of the user complexity. Having both borrow checking and regions would just be adding complexity though, which defeats the purpose a bit.

Still, there are a few other features of Vale that we're thinking of proposing to Rust, especially around concurrency, determinism, and better memory safety guarantees.

Re:Memory Management Reference by tomp on Tuesday July 12, 2022 @05:41PM (#32076043)
I've been following Vale's progress with interest but AFAIK as of a few months ago, it's still vapourware.

Excited to learn you're nearing an actual implementation though! Let's hope it's practical!

Re:Memory Management Reference by verdagon on Tuesday July 12, 2022 @05:54PM (#32076184)
If talking about regions and HGM, that's fair to say. Regions are further behind where I hoped because the latter stages are blocked on generics, and I had to take 8 months off a year ago because of some PTSD from work (long story).

I acknowledge that these are excuses, and that these two features of Vale can reasonably be regarded as vaporware until we get them finished. We're over halfway done with regions, fingers crossed for September!

For what it's worth, most of our innovations are actually complete and released. Generational References, Fearless FFI (minus sandboxing), and Higher RAII are all done and usable, and the Perfect Replayability proof-of-concept is finished as well. All these are in version 0.2, which can be downloaded at [0].

And if you ever want to see what we're working on, we have a "What's been happening recently" on the roadmap page [1].

Thanks for following along!

[0] https://vale.dev/download

[1] https://vale.dev/roadmap

Re:Memory Management Reference by chrissinclair on Tuesday July 12, 2022 @07:47PM (#32077297)
> I acknowledge that these are excuses

I'm not privy to the story here, but taking eight months off to treat PTSD doesn't sound like an excuse to me, but a really important period of time that needed to be taken. I hope you're doing alright now, but don't let yourself go thinking that taking time to deal with something like that is an excuse for other things not happening - it's far more important.

Separately, I've not checked Vale out yet, but props to you for diving in on new ways of handling memory and RAII - looks really interesting and I'll be giving the language a whirl at some point soon!

Re:Memory Management Reference by verdagon on Wednesday July 13, 2022 @08:24AM (#32081664)
Thanks mate, I appreciate the kind words. I try not to judge myself too harshly for taking the time off, but it does help to hear it from a kind soul such as yourself. Luckily, the PTSD isn't spilling over into my other interests (games, Vale, etc) much any more so life is steadily improving!
Re:Memory Management Reference by avgcorrection on Tuesday July 12, 2022 @04:39PM (#32075372)
Don’t save the disclaimer for last.
Re:Memory Management Reference by verdagon on Tuesday July 12, 2022 @05:11PM (#32075750)
Happy to oblige, edited it upward. Hope your day is going well =)
Re:Memory Management Reference by avgcorrection on Tuesday July 12, 2022 @05:13PM (#32075767)
It is, and thanks!
Re:Memory Management Reference by WJW on Tuesday July 12, 2022 @04:15PM (#32075064)
Well first of all there is tons of research being done right now into linear type systems, which have the potential to statically verify a whole bunch of things that Rusts borrow checking still struggles with. It's a very hot field of CS research atm.

I also think that you discount the amount of advancement that has been done in the development of pauseless GC. It's a shame that many of these are only available on the JVM (and sometimes only at a steep price), but I have no doubt the main ideas will eventually get adopted by other languages.

But really there are so many unexplored niches. AFAIK no current architecture has hardware support for read/write barriers, having that would make pauseless GC much more feasible. Zig has been experimenting with letting people use custom memory allocators, but there are many more avenues there that have not been explored. Why not have a language feature for "this value lives outside GC management" similar to Haskells compact regions but more powerful? Nim has some cool ideas about mixing reference counting for most values and a more general GC for cycle breaking. Mixing GC and non-GC values is also still a seriously underexplored field IMO.

Re:Memory Management Reference by whizzter on Thursday July 14, 2022 @03:54AM (#32093246)
Actually _most_ hardware architectures has support for read/write barriers, it's just that the technique is patented. :(

Also fully agreeing on the GC/non-GC mixing being underexplored, however outside of compiler analysis optimizations this would require language support and the intersection of people who would be interested in this mix doesn't seem that large.

Re:Memory Management Reference by cb321 on Tuesday July 12, 2022 @05:50PM (#32076131)
Nim has for a long time had automatically managed `ref` as well as manually managed `ptr` which is similar to (perhaps directly inspired?) your last comment, and has several options for Auto Mem Mgmt.
Re:Memory Management Reference by oddity on Tuesday July 12, 2022 @03:12PM (#32074402)
Depends on how you define innovation.

The hard truth is that there is no free lunch. We like to pretend we're using a Turing machine, but the moment you start caring about performance or memory limits, the abstraction breaks down and you realize that physics dictates that we have a finite state machine instead. This was "solved" about as well as it could be many years ago with however many billions were poured into GC R&D for all the people who don't care deeply about the limits, but nothing is going to magic away that fundamental trade-off unless we discover new physics.

Every innovation since then has been about developing workarounds to deal with that trade-off in more or less sophisticated ways.

Re:Memory Management Reference by Hirrolot on Tuesday July 12, 2022 @03:54PM (#32074838)
There's also an interesting programming language called Neut [1]:

> A dependently-typed programming language with compile-time malloc/free determination.

Also related "ASAP: As Static As Possible memory management" [2].

I think most of the "innovation" you're talking about is happening in the programming language design field.

[1] https://github.com/vekatze/neut [2] https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-908.pdf

Re:Memory Management Reference by vitiral on Tuesday July 12, 2022 @04:48PM (#32075478)
I'm doing some pretty different things with civboot.org and http://github.com/vitiral/fngi, but most would consider them regressions instead of advancements.

The goal is to have fragmentation-free memory managers that can work with minimal hardware support. This has limitations like block size (only 4k blocks allowed), but forces me to innovate. For instance, I'm planning on an "arena stack", where most functions simply use the arena on the top of the stack. Functions can reserve an arena on the arena-stack, call a function, then copy essential data and drop the arena, reducing fragmentation.

Might not be the kind of ideas you were thinking, but I consider this simpler than the current abstractions which hide complexity.

Re:Memory Management Reference by olliej on Tuesday July 12, 2022 @02:51PM (#32074184)
By your definition of what is "engineering" I feel like all improvements or changes are just engineering.

Because you've described three classes:

* No explicit runtime knowledge of whether a given object is alive

* Explicitly tracking how many references there are to an object

* Searching the program memory to see if there are references to an object that make it live

I'm not sure there is another class of memory management that isn't one of these three?

It's also somewhat unfair to rust - the static and parametric lifetime management _is_ new, and doesn't "need unsafe" to do anything interesting; and to tracing GCs modern GCs have developed ever more techniques to not be "stop the world" including parallel, iterative, lock free tracing, etc.

Re:Memory Management Reference by tomp on Tuesday July 12, 2022 @03:27PM (#32074565)
All of the ones you mentioned above still involve a stop-the-world phase, however brief.

The keywords you’re looking for are “fullya-concurrent” or “on-the-fly” GC, but I’m not aware of any production runtime with those.

Re:Memory Management Reference by WJW on Tuesday July 12, 2022 @04:21PM (#32075138)
Doesn't the JVM ecosystem have fully concurrent GC with the Azul C4 collector? It's not my favorite set of languages but I don't think we can realistically claim the JVM is not a production-ready runtime.
Re:Memory Management Reference by tomp on Tuesday July 12, 2022 @05:39PM (#32076016)
Yes, "pauseless" is another keyword I forgot to mention.

Azul uses a read barrier which I guess makes it so slow that noone uses it (and the same ideas haven't made their way into JDK collectors). The previous version required hardware-supported read barrier, now AFAIK they only require a Linux kernel extension.

Re:Memory Management Reference by kevin_thibedeau on Tuesday July 12, 2022 @02:50PM (#32074172)
Nim has GC with controllable max time slices.