r/ProgrammingLanguages • • 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 just new 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!

57 Upvotes

67 comments sorted by

View all comments

Show parent comment

3

u/mamcx Jun 25 '26

Here in Rust land we say to cycles: Who cares? (just showing to you is possible to have a LARGE community around a lang without).

The main takeaway is that most problem of Langs (and DBs, and OSs) arise for the want to solve everything and let the user be as lazy, careless, clueless, irresponsible, indifferent, etc even all the time.

Once you enforce that: performance, safety, correctness, predictably, ACID, pause-less, etc IS paramount then "but what if the user want to shoot himself in the foot and wanna do it, easily?" is not your problem anymore.

PENALIZE the user for do what is wrong against the major goals (example: unsafe and cycles in Rust).

THEN, find a reasonable scape hatch and/or let the (engine, runtime, std library) be the only thing that can solve it (example: You wanna cycles? You need to use std::cycles and is an internally GC library, and nobody can create his own, sorry!).

In compensation, you will make enjoyable and ergonomic to archive: performance, safety, correctness, predictably, ACID, pause-less, etc

P.D: A major win is give what is a ad-hoc, error-prone imperative or manual "functional" goal into a proper data-structure that do it well. The "arena" is one. You can upgrade arena to handle the "cycles" solve ABA, etc for the odd-case anyone need it.

But, I suggest make your language extra-pedantic first and not extend with workarounds until you see what it can actually do (ie: Like imagine doing Rust and delay unsafe and async as much as you can).

P.D.2: With Rc and such a good idea is to see that you can do Rc<Vec<_>> instead of Vec<Rc<_>> this alone gives good perf in the common case of many readers.