Live data from Hacker News

A single-file C allocator with explicit heaps and tuning knobs

github.com

41–48 of 48 posts

Re: A single-file C allocator with explicit heaps and tuning knobs

#41

The classic Doug Lee's memory allocator[1] has explicit heaps by the name of mspaces. OP, were you aware of that; and if yes, what does your solution do better or different than dlmalloc's mspaces? [1] https://gee.cs.oswego.edu/pub/misc/?C=N;O=D

I am aware of dlmallc/mspaces and GNU Obstacks. Both were in a way, original inspirations for spaces. Though I hadn't looked at mspaces source in years, I remember its inline boundary tags enabling zero overhad per allocation and there were no alignment constraints on the allocator itself (and is hardened across countless archs, not just x64 :) Spaces uses 64kb aligned slabs and a metadata find is a bitop. so potentially, a buffer overflow can corrupt the heap metadata in mspaces while spaces eats a cache-line on free.

mspaces was one mutex per heap for entire task (no tlc or lockfree paths). Spaces has per thread-heaps, local caches (no atomic ops on same thrad alloc/free), and a lock-free Treiber stack (ABA tagging) for cross-thread frees. mspaces doesnt track large allocs (>= 256 or 512kb) that hit mmap, so unless one knows to explicitly call mspace_track_large_chunks(...), destroy_mspace silently leaks them all (I think obstacks is good this way but is not a general fit imo). In Spaces, a chunk_destroy walks and frees all the page types unconditionally.

Another small thing may matter is error callbacks: Spaces triggers a cb allowing the application to shed load/degrade gracefully. Effectively, the heap walking (inspection?) in msapces is a compile-time switch that holds the lock whole time and doesnt track mmap (direct) allocs, and shares the thresholds like mmap_threashold, etc. globally, whereas Spaces lets you tune everything per-heap. So I'd say Spaces is a better candidate for use cases mspaces bolts on: concurrent access, hard budgets, complete heap walking and per-heap tuning.

Re: A single-file C allocator with explicit heaps and tuning knobs

#42
post #22

"That costs ~5 ns when the line is cold" I don't see how that could possibly be true. Sounds like a low-ball estimate. Also i wish to point out that the "tcmalloc" being used as a baseline in these performance claims is Ye Olde tcmalloc, the abandoned and now community-maintained version of the project. The current version of tcmalloc is a completely different thing that the mimalloc-bench project doesn't support (co…

Fair points on both - the 5ns is the L2 hit case. I should have stated the range (30-60ns?) instead of the best case. And yes, fixing the tcmalloc case is on my list - thanks for pointing that out. And also to be clear, the goal was never to beat jemalloc or tcmalloc on raw throughput. I wanted t oshow that one doesn't have t ogive up competitive performnce to get explicit heaps, hard caps and teardown semantics.

Re: A single-file C allocator with explicit heaps and tuning knobs

#43
post #42
post #22

"That costs ~5 ns when the line is cold" I don't see how that could possibly be true. Sounds like a low-ball estimate. Also i wish to point out that the "tcmalloc" being used as a baseline in these performance claims is Ye Olde tcmalloc, the abandoned and now community-maintained version of the project. The current version of tcmalloc is a completely different thing that the mimalloc-bench project doesn't support (co…

Fair points on both - the 5ns is the L2 hit case. I should have stated the range (30-60ns?) instead of the best case. And yes, fixing the tcmalloc case is on my list - thanks for pointing that out. And also to be clear, the goal was never to beat jemalloc or tcmalloc on raw throughput. I wanted t oshow that one doesn't have t ogive up competitive performnce to get explicit heaps, hard caps and teardown semantics.

That makes sense. I have a long-standing beef with the mimalloc-bench people because they made a bunch of claims in their paper but as recently as 2022 they were apparently not aware of the distinction, and the way they tried to shoehorn tcmalloc into their harness is plain broken. That is not a problem caused by your fine project.

Re: A single-file C allocator with explicit heaps and tuning knobs

#44

There's a single commit in the whole repository. Was this AI generated?

Elements of the readme are a dead giveaway. I can also tell you that this was written with Claude. No issues with that in principle but I definitely would not trust Claude to get this stuff correct. Generally, it is quite bad at this kind of thing and usually in ways that are not obvious to people without experience.

No AI was used. I see no problems with using AI to write code whatsoever, but this isn't that. The formatting is my screw-up. I ran clang-format with a bad config, then tried to hand-fix the result and made it worse. The parenthesization is from defensive macro expansion that I inlined for the build and never cleaned up . The inline (smoke) test in the Makefile was a lazy hack from my local workflow that I forgot to replace before pushing and a proper test suite exists but the names/sections are in Telugu, my native language . I'll fix both and add.

Re: A single-file C allocator with explicit heaps and tuning knobs

#45

Earlier quoted context omitted.

Explain why you think making a single commit is related to any source code sharing obligation? You completely failed to establish why making a single commit is indicative of it being garbage. Your statements are a series of non-sequiturs so far and thus I can't take you seriously.

> Explain why you think making a single commit is related to any source code sharing obligation? When you share code it's presumably for people to use . It is often useful to have commit history to establish a few things (trust in the author, see their thought process, debug issues, figure out how to use things, etc). > You completely failed to establish why making a single commit is indicative of it being garbage. A…

Here's the thing, get used to single big commits. Eventually, somebody is going to try to train on specific change sets. This'll enable models to learn specific authors mannerisms, idiosyncracies etc... Single large commits creates an info asymmetry boundary, which is about the only defense a creator has in a world of willful infringement to train algorithms to replace or devalue them in the market. It sucks... But this is the world we're growing into now.

Re: A single-file C allocator with explicit heaps and tuning knobs

#46

Earlier quoted context omitted.

Elements of the readme are a dead giveaway. I can also tell you that this was written with Claude. No issues with that in principle but I definitely would not trust Claude to get this stuff correct. Generally, it is quite bad at this kind of thing and usually in ways that are not obvious to people without experience.

You have to spend a ton of time on writing comprehensive test suite. It can do so many subtle bugs you would otherwise only find from vague customer report and reproducing by chance.

You can't write tests if you don't know what you are looking for.

Re: A single-file C allocator with explicit heaps and tuning knobs

#47
post #27

Earlier quoted context omitted.

Not only that, since there is a super-standard std allocator api, it makes itself very amenable to memory safety analysis, as long as you don't sneakily implement allocations outside of that api. https://github.com/ityonemo/clr

Now this is very interesting. Thank you for sharing.

thanks. i pretty much have it running in the background (watching it vibecode carefully) while im doing other work.

Re: A single-file C allocator with explicit heaps and tuning knobs

#48

What is the reason for the weird `{ code };` blocks everywhere and is the below code machine generated? ```c ((PageSize) (chunk->pageSize - ((PageSize) ((PageSize) ((PageSize) (sizeof(Page) + (sizeof(struct _Block))) + (PageSize) ((sizeof(double)) - 1u)) & ((PageSize) (~((PageSize) ((sizeof(double)) - 1u)))))) - ((PageSize) ((PageSize) ((PageSize) ((sizeof(FreeBlock) + sizeof(PageSize))) + (PageSize) (((((sizeof(doub…

There's a lot of code in the file that is questionable to say the least. There are unnecessary blocks ( { ... }; ) of code with unnecessary semicolons that don't serve any logical purpose. My hunch tells me it may be the result of macro-expansion in C (cc -E ...), etc. So it's likely there's a larger code base with multiple files and they expanded it into a one large C file (sometimes called an amalgamation build) an…

[deleted]
Post reply on HN