Get the latest tech news

Using black magic to make a fast circular buffer (2017)


. Yesterday, I took a glance at the Wikipedia page for the circular buffer and was intrigued by an alleged optimization technique that I was not familiar with: A circular-buffer implementation may be optimized by mapping the underlying buffer to two contiguous regions of virtual memory.

Though there are other optimizations we can make, the technique offered in the above Wikipedia surpasses hardware-agnostic approaches by virtue of the fact that the memory management unit can handle most of the wrap-around logic on our behalf. Can be manipulated to do evil through configuration flags.And now, a less common system call that glibc (our friendly userland interface to the kernel) doesn’t expose to us: This micro-benchmark is not wholly representative of the full queue logic (which could be affected by conditions such as page faults and TLB misses), however gives us a good indication that our optimization was worthwhile.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of fast circular buffer

fast circular buffer

Photo of black magic

black magic