r/ProgrammingLanguages • u/goat-luffy • Jul 04 '26
Does implementing GC makes languages slow?
https://github.com/berylangMonth ago, I created a team of 5 and started working on "Bery - The compiled programming language". By the end of June we have quite good working compiler (it's not complete yet). In Bery we have decided to add the automatic Garbage Collector so we choose the "Mark and Sweep" method for it in the Bery Runtime Environment (BRE).
Now as we are heading forward with adding OOP and Exception Handling, I notice some delays in the compilation of program.
So we are now at this point of discussion - should we remove it from compiler or let it be there.
I will looking forward for help regarding this. and btw these are some constraints we set -
unsigned int BERY_GC_ALLOC_THRESHHOLD = 1000;
size_t BERY_GC_HEAP_SIZE_THRESHHOLD = 4 * 1024 * 1024;
20
u/BeamMeUpBiscotti Jul 05 '26
What would you do if you removed it?
I think it's natural for a compiler or compiler-like tool to slow down as more language features are supported, as different types of analysis/optimizations are added.
Better to have something working & optimize the compiler itself later, as long as you don't lock yourself into an architecture that severely limits performance.
13
u/Inevitable-Ant1725 Jul 05 '26
"Does implementing GC makes languages slow?"
No.
Why would it?
Systems like the Ravenbrook Memory Pool system and the Boehm garbage collector show that code with a GC doesn't even have to be different than code without. You can compile C++ and have a concurrent GC.
Boehm does it by never moving anything and occasionally stopping threads to finalize or if the mutator runs ahead of collection.
Ravenbrook gives you the choice of non-compacting collection or of being conservative for anything in stacks or registers, and pinning anything they might point at and occasionally putting up barriers by locking pages.
No doubt systems like these that don't need any software read or write barriers don't have the same maximum throughput of memory collected as the big enterprise collectors like Java, .net or Azul. But if you're thinking of getting rid of gc, you didn't have "everything is gc" like Java in the first place.
One thing I think that's interesting is that some kinds of languages can have garbage collectors that don't have to trace. Some kinds of functional or logic languages can have specialized collection.
4
u/Inconstant_Moo 🧿 Pipefish Jul 05 '26
When you say "some delays in the compilation of program", do you mean execution? That would normally be the larger problem.
Either way, when you designed your language, you presumably put a GC into the design because it fit your users and use-case. You know it is possible, though difficult, to make a performant and/or rapidly-compiling GC (e.g. Golang). If you take it out because that bit's difficult and making your users write
mallocandfreeis easy, what are you even doing designing and developing a language?3
u/RoughCap7233 Jul 05 '26
In your post you are talking about compiling - normally this would not be a significant bottleneck. You only compile once and run many times.
In runtime any GC would be slower and use more resources than languages that do not use GC. Java use heavily optimised mark and sweep garbage collector.
Swift and ObjectiveC use reference counting, but this has a different set of costs.
45
u/Tonexus Jul 05 '26
Advanced GCs can be very performant... but it takes a lot of work to get there.
5
u/Soupeeee Jul 05 '26
It can actually make them faster in some situations by delaying memory management tasks until the program is idle. Allocation with a moving GC is really fast, as you usually just need to bump a pointer; you don't need to search for open blocks or do any bookkeeping. Of course, you need to pay for it later during the other GC phases, but it can greatly speedup some workloads.
Although not directly related to mark-sweep GCs, arena allocators are a similar concept.
3
u/SadPie9474 Jul 05 '26
Finally, a compiled programming language!
2
u/kaplotnikov Jul 05 '26
IMHO the lack of GC make developers slower because there is an additional big set of concerns to enter developer's mind. When creating a language is a primary concern is if paying this extra development cost worth it or not for target users of the language. Also Java experience suggests that for VM it is better to be somewhat GC-agnostic, as different application profiles require different GC.
0
u/runningOverA Jul 05 '26
As per my benchmarks. mark and sweep collection takes 50% of your total execution time.
Manual memory management with ref counting performs better.
1
u/vmcrash Jul 05 '26 edited Jul 05 '26
Just a guess, I'm not sure how difficult this would be to implement, or whether this improves anything: Maybe the GC does not need to handle every object? Maybe some our used so short and so easy to reason about that you can allocate and free them manually? Or you could transpile to, e.g., Go (like Lisette is doing) and get the GC for free.
Out of curiosity: what problems your language tries to solve? I remember following natively compiled languages to also use GC (at least optionally): Go, Nim, D, and IIRC Crystal, but there are definitely more.
There are already too many manually memory managed programming languages like C/C++, C3, Odin, Pascal, Zig. IMHO, their time is over. If you want to avoid a GC, you need to find a smarter solution like Rust or Hylo.
1
u/david-1-1 Jul 08 '26
You could instrument your objects and gather lots of statistics that show clearly where the time is going. Worth doing for any compiler. And make sure the rest of the processing is also efficient: instrument your major inner loops, too.
0
u/[deleted] Jul 05 '26
[deleted] — view deleted comment