r/C_Programming • u/oraziorillo • Jun 18 '26
Manual reference counting to free a computation graph in C
Context: I've been wanting to get back into C for some months, I haven't had the chance to work with it seriously since uni. So, I reimplemented Karpathy's micrograd in C as a learning project (a small autograd engine, repo link below). Coding this project, I had one simple rule: no AI assistance. So, the `src/` code is completely handwritten; AI was only involved in polishing documentation, Makefile, tests and a toy example.
The core structure of the engine is a computation graph: a DAG where each node is the result of an operation on earlier nodes, and it's full of shared pointers, as one node can be owned by many others.
To solve this, I went with manual reference counting:
- Every node is born with ref_count 1.
- Each operation retains its operands (a result co-owns the nodes it came from).
- The caller uses value_release to decrement the ref_count; at zero the Value recursively releases its operands and frees itself.
So, releasing the root of the DAG cascades down and frees exactly the subgraph that produced it in the first place. The parameters of the network survive because they're referenced elsewhere, whereas pure intermediates disappear.
This choice felt very natural at first, but as I was progressing, I started doubting it, so here I am, asking your opinion about it. Given that the whole graph is built and torn down as a unit, is plain reference counting the right call here, or would you reach for something else? Tell me what you'd have done differently.
•
u/AutoModerator Jun 18 '26
Hi /u/oraziorillo,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/Linguistic-mystic Jun 19 '26
Sounds like arena allocator is the way to go here. Much more performant than reference counting
3
u/stianhoiland Jun 21 '26 edited Jun 21 '26
I saw your post on HN and checked out the code.
I thought, what a neat and clean implementation, and what a great way to write [insert your object oriented programming language here] in C. And thus, what a shame! Because in C you don’t have to do it this way!
Savor that for a moment: "In C, you don’t have to do it this way."
I say that as if doing it this way (though neat and clean!) is a chore one would rather not do. But how would you know that it’s a way of doing things which you would rather not have to do things in C if you have no contrast to a better way, if you don’t know any alternative?
I scrolled through your code and thought, damn, this would be such a good opportunity to convert to a more thoroughly original C-style; but ain’t no one got time for that!
So I thought I would see if you’d posted here on Reddit (I don’t have a habit of participating on HN, but do so on Reddit) so I could at least *say* this, if not so much show it. When I launched the Reddit app, I opened the sidebar to find the r/C_Programming subreddit and saw u/skeeto under favorites and thought I could just tap into his profile and probably be one tap away from this subreddit instead of scrolling down and fishing for it.
And what do you know. Not only was I one tap away from this subreddit, and not only had he replied to exactly the thread I was looking for, he had no less than provided an LLM refactor that moved the project in the direction I had in mind (but certainly not fully).
Crazy that!
1
u/Educational-Paper-75 Jun 19 '26
What are your doubts then?
1
u/promeritum Jun 20 '26
You want to let Boehm handle that nonsense.
https://www.hboehm.info/gc/