ring_view icon indicating copy to clipboard operation
ring_view copied to clipboard

Support physical memory hack ("magic ring buffer")

Open Quuxplusone opened this issue 9 years ago • 1 comments

Alex Rosenberg suggests that no "STL ring-buffer" will replace the most efficient real-world ring-buffer unless it supports the following performance hack:

Let's back our particular ring_view by a span of (virtual) memory exactly 2 pages[1] in length. But, in the page table, we map both of these virtual pages to the same 1 page of physical memory. This means that if you manage the head and tail pointers correctly, you can arrange it so that it is always true (in the virtual-memory space) that begin < end, which means that iterating over the ring-buffer from begin to end (or vice versa) never requires a % capacity operation.

Obviously this can't be the default behavior; but ring_view must be generic enough to support this particular idiom, or else nobody who uses this idiom will be able to switch to ring_view, which defeats the whole purpose.

How can we genericize out the % capacity part of this code so that you don't get it when it's not needed?

( [1] Well, I think 2N pages backed by N pages of physical memory would work for any N. But 2-by-1 is the easy-to-visualize case.)

Quuxplusone avatar Oct 22 '15 05:10 Quuxplusone

Some more background reading: https://fgiesen.wordpress.com/2012/07/21/the-magic-ring-buffer/ And a sample implementation: https://en.wikipedia.org/w/index.php?title=Circular_buffer&oldid=600431497#Optimized_POSIX_implementation

Quuxplusone avatar Mar 09 '16 03:03 Quuxplusone