Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is another interesting ring buffer implementation that uses mmap. https://github.com/willemt/cbuffer


I was waiting for someone to mention this -- it seemed much more interesting to me. It's a real classic in the "what the hell, you can do that?" category. (Bonus points if you've done it in a language that requires "extra data" for strings, like storing the length somewhere.)

I must admit that I never actually benchmarked my implementation properly -- it might be interesting to see if there are actual trade-offs between mmap vs. copying. (I'm guessing that nothing can beat MMU support, but I think the MMU also supports copy operations, so...?)


With the additional benefit that one can have arbitrary slices between head and tail as a contiguous memory region.


That's so cool. Unfortunately for me, the one time I could have used something like this, I was working on an embedded system with no mmap / virtual memory.


Here's another implementation that works on Windows too: https://github.com/andrewrk/libsoundio/blob/master/src/ring_...


This seems to use modulus. The whole point of the mmap trick is to get the kernel/MMU to do the work for you, IIRC.

EDIT: Oops, I see they use mirrored memory here as well.


Mike Ash talks about an implementation for macOS/iOS: https://www.mikeash.com/pyblog/friday-qa-2012-02-03-ring-buf...




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: