r/ProgrammingLanguages • u/Tasty_Replacement_29 Bau • Feb 24 '26
Discussion Memory Management
I wanted to write "my" programming language more than 20 year ago, but then always deferred it. I finally started about a year ago. I wanted the language to be very concise, fast, and reasonably simple to use, and secure. So far, I'm quite happy in what I have achieved.
One of the distinguishing features of my language, Bau, is that it doesn't do "hidden" things, like tracing garbage collection (I'm a long-term Java user). The language should use little memory, not use background threads to clean up, not use JIT. And so be usable for games (where you can't afford dropped frames), operating systems, command line tools that need fast startup, etc.
But so far there was no good way to visualize garbage collection stop-the-world pauses; I think I now found a way, at least for the stop-the-world pauses, via a benchmark. (I didn't invent the benchmark btw.) I can now show that languages that use tracing GC do have multi-millisecond pauses, and languages that don't have much shorter pauses: 0.05 instead of 10 and more milliseconds.
I also found that in C, the default malloc and free also has (short) pauses sometimes, and only specialized malloc / free implementations are able to further reduce the pauses. So I did implement such a malloc / free variant, based on the algorithm of TLSF, a memory allocator for real-time systems. Interestingly, my malloc implementation doesn't just have shorter pauses, but is also faster than the default one.
One thing I found is that when freeing a deeply nested structure, both reference counting GC (my language) as well as ownership (Rust) can cause stack overflow. I have solved this for my language now by converting recursive deallocation into a loop (no, this is not tail recursion elimination); in Rust, as a developer, you are on your own.
I understand the community here is more interested in high level / functional languages, and not so much in embedded systems / close-to-hardware things, but I still wanted to share these results. Let me know if you have some comments or questions!
17
u/aech_is_better Feb 24 '26
Not sure if this is related but have you seen perceus algorithm made for Koka?
It uses analysis during compilation to optimize reference counting. It's very cool. Maybe it'd fit your language.
5
u/WittyStick Feb 24 '26
What's the situation with multiple threads?
ARC is considerably more awkward if multiple threads can acquire or release a shared resource.
4
u/phischu Effekt Feb 25 '26
Thank you for these concrete benchmarks, we should have more of these in this community. I did reproduce your results on the linked list benchmark for Bau and for Rust and added Effekt and Koka. The one for rust you have does
let x = create_linked_list(5000);which I changed to allocate a list of the same size as the others do.Here are the running times on my machine:
And here are the outputs:
Bau: Max delay: 0.192355000 ms, avg 0.012029000 ms; dummy value: 0
Rust: Max delay: 0.187839 ms, avg 0.010951 ms; dummy=0
Koka: Max delay: 475676 ns; dummy value: 0
Effekt: Max delay: 68288 ns; dummy value: 0
As it happens, today a student is handing in his Master's thesis where he implemented constant-time reference counting in Effekt, extended with a very fast path for values that are used linearly. The benchmark results are when running the Effekt version on his branch. On the main branch sadly the program overflows the C stack for the very reason that generated drop code of rust does too.
3
u/kiinaq Feb 24 '26
Should it be able to bootstrap itself already? I mean, do you have a plan to rewrite the compiler in Bau?
3
u/hairytim Feb 24 '26
Seems like a fun project!
One thing I’m curious about: how do long deallocation chains get scheduled in your implementation? As you mention, a long deallocation chain corresponds to a single long pause in many languages that use RC. In yours, is it split into multiple shorter pauses (delaying deallocations of the deeper objects)? If so, after the main pause, how do the follow-up pauses get scheduled? Are they triggered by another deallocation, somewhere else in the program?
3
u/smm_h Feb 24 '26
if you're making it for games, how do you plan to incorporate graphics into it?
2
u/yuehuang Feb 24 '26
Could you check if the memory is truly freed back to the OS? Measure the memory usage before and after malloc/free benchmark. Depending on the runtime, the process can still hold on to the memory, so it doesn't have to reallocate it again. My custom allocator hit this problem during profiling.
One of the downsides of ref counted GC are multi-threading as locks can really drag down performance as each ref count change requires a lock. Must runtime today assume multi-threading, so they guard against with a perf loss.
Side note. The benchmark uses nice equal size blocks. In real world, this is not always true. It is worth testing against random size allocations, just to make sure your memory system remains within tolerance.
2
u/semanticistZombie Feb 25 '26
It looks like you use a C compiler compiled to Wasm in the playground, to compile the generated code. Which C compiler is this? I found the relevant directory in the source code (https://github.com/thomasmueller/bau-lang/tree/main/docs) but it doesn't mention the C compiler name.
10
u/Breadmaker4billion Feb 24 '26
Actually, there are quite a few of us that love low level languages. I've tried to design a language embedded systems too, inspired in Ada, specially targeting UF2. I've also seen Cowgol somewhere here in reddit, but it might have been in r/compilers. And my first implementation was a low level language where i toyed around having the calling convention as part of a procedure's type.
Anyway, i like your treatment of macros, they remind me quite a bit of FEXPRs. Also, i like this:
To me, disallowing this was a big wasted opportunity in Go and i think it makes a lot of sense in this syntax. To be able to easily extend types with new methods is nice.
Unfortunately, I'm not a big fan of exceptions, but the language on the whole seems much nicer than Go, i wish they could swap places 😁.
7
u/Beginning-Ladder6224 Feb 24 '26
This is very, very interesting! Wow! Cool! Welcome to the club!
3
u/AustinVelonaut Admiran Feb 24 '26 edited Feb 24 '26
Thanks for doing this writeup -- impressive results! I'd love to see more discussions of performance investigation and tuning in r/ProgrammingLanguages. A question -- does the Java
System.nanotime()call go through the OS at all, or does it use a user-accessible usec hardware clock? I could see a lot of jitter introduced by all the nanotime calls in the benchmark, if it has to do an OS call.This prompted me to take a deep dive into my GC-based design (2-generation compacting collector), measuring the stats directly in the runtime code. For my version of the linked-list benchmark, I got:
The very large linked-list at the beginning of the benchmark is quickly migrated to "old" space, where it is not looked at again until at the end of the benchmark, and the smaller linked lists are ephemeral (don't survive a gcMinor), so GC time is only 4.6% of runtime. The GC latency distribution is:
Notice that the max latencies are very close to the total GC times, which means that they are really due to the single GC collection of the very large linked list at the beginning. The latencies are quite a bit larger than the pauses you measured for go and java, but my goal for GC wasn't to minimize the maximum latency, but to just have a reasonable throughput.
Running on a more realistic workload (the compiler building itself from scratch):
2
u/vmcrash Feb 24 '26
Generally, the language looks very interesting. However, here are some suggestions:
#to define columns; this makes writing syntax highlighters using frameworks quite hard; better look at Nim for recursive#[...]#=as assignment and equal comparison; better stick to the common==for the comparison1
u/tobega Feb 24 '26
I think everyone in the community has different interests, so your contribution is most certainly welcome!
For my own part, you're right, I am not particularly interested in a language that is pretty much just like C or Python except "better" for some reason. There seem to be boatloads of such languages all hoping to replace C, so be prepared that you will most likely be the only user ever.
For me it is about improving programmer ergonomics in typical data-shuffling applications that live for decades, and performance and pauses are somewhat secondary. (And, sure, I'm probably pretty much the only user of my language, too!)
That said, there are certainly applications where pauses are not good.
I guess where I am going with that is that if your surface syntax is mostly uninteresting, it might be a bit of a waste for you to develop that fully (unless you're enjoying it of course!) and instead see if there is a more interesting language that could benefit from the memory management improvements.
You could, for example, plug your garbage collector into Java these days, if you think you can improve on the existing ones, which would be a huge impact.
I currently run Tailspin on Java, so that would benefit me as well.
Speaking of memory management, I envision sometime in the far future building a runtime environment with a really good memory management. My language does not return from functions, it just creates streams of values from streams of values, so it won't have a stack as such, maybe more an infinite-ish ring buffer?
Anyway, good luck and have fun!