Hi,
Introduction
There was a recent thread on reddit (https://www.reddit.com/r/Zig/comments/y8torn/what_is_a_allocator_anyway) where matu3ba suggested porting rpmalloc as a performance optimized allocator for zig. The other day I have found this bug report too: implement a fast general purpose allocator · Issue #12484 · ziglang/zig · GitHub
So I have decided to give it a go. I can not promise, that I will succeed, but I will try to do my best. I am not a programmer, but I was able to port pdqsort and timsort to zig before, and I want to learn, so who knows.
As I will probably struggle sometimes and I would ask here anyway, I though it would be fun to write a progress diary here.
Code is available here: https://git.sr.ht/~voroskoi/rpmalloc.zig
Day 1
I have converted rpmalloc.c to zig with zig tranclate-c -I /usr/include > rpmalloc.zig
as a start. There are a couple of problems with it: comments are stripped, atomic does not work to name the biggest issues, but it is nice to see how it handles certain cases as a reference.
I will do a manual porting in an incremental style. That means for the first iteration I just want to have a working version without changing things too much. So it will be in zig, but I will follow the rpmalloc.c line-by-line. After reaching a working state I can make the code more zig-like.
On the other hand I will try to follow zig’s naming conventions and such, so there will be no foo_t
here.
rpmalloc.h
I have started with rpmalloc.h
. I just skipped the compiler specific part as we only care about the zig compiler. I have defined global values (everything is const for now, but I do not want to think about it), created structs (with some tweak, eg. rpmalloc_global_statistics_t
→ GlobalStatistics
).
I just skipped function exports and first class heap support for now.
rpmalloc.c
Adding global variables is simple, so I have done that. Some of them will be unnecessary at the end, but I can remove them later in a cleanup phase. There will be a lots of TODO comments here
Implemented atomic functions. This was a bit harder: I am not sure if memory_order_relaxed
’s zig equivalent is std.builtin.AtomicOrder.Unordered
, but that seems to be the best fit.
I use bool
for return value with @cmpxchgWeak()
calls instead of int
in the original code.