r/ProgrammingLanguages • u/smthamazing • Jun 25 '26
Discussion Is reference counting a trap?
I'm trying to choose a memory management strategy for my language. Since I come from gamedev background, I'm mostly focused on the following:
- Static memory safety guarantees (duh). Which basically implies automatic memory management: if the compiler can prove a leak, it can also insert a
drop()there. - A lot of my code is tight loops in game engine internals or audio processing. Not sure if this rules out GC completely, but I cannot afford any unpredictable pauses - they are very noticeable in games and audio.
- When writing code, I generally don't want to think about allocation strategies. I still choose them consciously per call graph: "module A will just allocate everything with the default strategy", "function B will receive a custom arena allocator and I will just guarantee that it always has enough space". But I don't want to write
allocator.alloc(MyThing, ...)everywhere instead of justnew MyThing(). This leads me to think of some sane default strategy + dynamically scoped allocators via some implicit or effect mechanism. - Not a hard requirement, but ideally I'd like to avoid a mandatory runtime and make sure my approach can work even on tiny devices like Arduino Nano (2KB - 32KB RAM). But I can drop (hehe) this idea if it proves impossible, embedded is not my main target.
All of this leads me to think about reference counting. It sounds rather attractive initially, and on the first glance many of its problems are solvable:
- It incurs some overhead (every deallocation is an int decrement and a check, + you need to keep all these RC counts in memory), but it's very predictable.
- That overhead can be avoided in many cases if the compiler can statically find where RC drops to zero and insert a
drop(). So it will mostly affect objects that persist across frames, like event queues, loggers, etc. - It gets bad when you have cross-thread data sharing and have to use slow atomics for counters. But since I want some form of linear types and ownership semantics anyway, I probably can give explicit choice to programmers: either your value is exclusive to one thread and you can only pass ownership fully; or you wrap it in a "slow"
Arc<...>and incur that overhead consciously (plus some unsafe escape hatches). - Freeing a large object graph can be slow, but this is solvable e.g. by passing ownership to a different thread.
- For strict immutable languages it seems pretty good, see counting immutable beans and Koka's Perceus paper.
What concerns me is that RC seems a rather unpopular choice in programming languages. The only major languages I know that use RC are Python (which is notoriously slow) and Swift, which I have little experience with, but I know that handling of weak and unowned references sometimes gets finicky. This unpopularity makes me think that maybe RC is a dead end for my project.
On top of that concern, pure RC doesn't handle cycles. They are often an architecture smell anyway, but some things like intrusive doubly-linked lists are powerful tools in gamedev, and I wouldn't want to lose them. I wouldn't mind having an optional opt-in GC for cycles specifically, but I'm not sure how to express in the type system that some relation is cyclic (often indirectly) and requires a GC in the context to work. I really want it to be explicit and statically checked.
Another thing is that for games, CPU cache locality is paramount, and RC seems to have issues with that: either you store counters with the objects and your arrays are no longer neatly packed, or you store them separately, and then every inc/dec is a memory read. Maybe it's solvable by selectively using arenas over RC though.
I've heard Nim does something in this space, but I'm not super familiar with that language either, and I'm wary of it having compiler flags for different memory strategies (possibility of ecosystem split, bugs, etc).
Is reference counting a worthwile direction to pursue at all given my requirements, or should I focus on other approaches like a lightweight and more predictable GC, or Rust-like borrowing model? I actually like how borrows work in Rust, but it's a very complex feature, and I'm not confident in my abilities to avoid associated bugs, unsoundness, and variance pitfalls when implementing the compiler.
Any thoughts are appreciated!
View all comments
Show parent comment
2
u/Puzzleheaded-Lab-635 Glyph Jun 25 '26
Two things.
On indices/ids: I think this is the strongest move on your list. It doesn’t just dodge bounds issues; it mostly dissolves the type-system problem. An index is not an ownership edge, so RC never sees a cycle. Your scene graph lives in one owning array, and the “backpointers” are just
u32s. So the graph can be logically cyclic, but as far as the memory model is concerned, it’s acyclic.You pay for generational-index bookkeeping instead of a cycle collector. Look at Rust’s
slotmapor Bevy’sEntitymodel for the stale-handle trick. For most gamedev cycles, I’d reach for this before anything fancier.On expressing “this part is cyclic” in the type system: you’re not wrong, you just haven’t found the prior art yet. Verona is probably the closest thing to crib from: explicit region types, where a region can carry its own memory discipline, including GC. Vale is also worth looking at: regions, generational refs, and making the memory strategy explicit instead of ambient.
The reframe I’d make on your GC-effect idea is: don’t make it a global effect. Make it a region.
A
gcregion confines collection to that region’s heap. So the pause is local and schedulable (you collect it at a frame boundary you choose), not “surprise, the whole world stops now.” That turns GC from “unpredictable pauses are back” into “this small graph gets collected when I say so,” which is the version a game can actually live with.And once you combine that with ids/indices, I suspect most of your cycles never need to enter a GC region at all.
Good luck!