Multi-Array Queue
github.com
github.com
Also are Bagwell's VLists[2], which were based on RAOTS, which he presents an example deque for, but this differs from OPs implementation.
A note about the VList versus RAOTS - in Bagwell's paper he claimed the VList performs better, giving a comparison of several soft MSB calculations, which may have been required at the time this was published, but nearly all modern hardware has instructions for very quickly calculating the MSB (Either a singe-cycle instruction, or via count leading zeroes), so it's questionable that there's a real benefit to it as it requires additional metadata which also comes at the cost of power-of-2 alignment of the sub-arrays. However, the VList was designed to be used for a Lisp implementation, for which there may still be other benefits.
msb = 8*sizeof(size_t) - __builtin_clz(idx)
Or in Java msb = Long.SIZE - Long.numberOfLeadingZeroes(idx)
This Multi-Array queue could perhaps benefit from this previous work. In particular, if you constrain the data arrays to be powers of 2 in size, you can use the MSB calculation to very quickly determine the index of the sub-array in the "rings" array, and by masking out the MSB, you determine the index in the sub-array. The RAOTS paper is a forgotten gem which every language developer should be aware of when they're implementing lists in their stdlib. They can be used for immutable lists too in place of linked lists, as cons only requires copying the contents of one sub-array and copying the rings array. In fact, you can modify it slightly to make the rings array a plain old linked-list to make this even cheaper for consing immutable lists, at the cost of slower random-access.[1]:https://cs.uwaterloo.ca/research/tr/1999/09/CS-99-09.pdf
I actually thought the same: Given e.g. the complex structures published in ACM papers, it would be a surprise if MultiArrayQueue would be a completely new discovery.
We are not in the pioneer years of 1960's anymore (unfortunately :-))
Separately; the simulator is cool and very helpful!
Also this one enqueue will be mega expensive - a clear "fat tail" in the latency histogram.
In MultiArrayQueue you keep all already enqueued material in-place, "just" allocate the bigger array, register the new diversion, enqueue the one new element - and done.
Thanks
for me it would be better to allocate new buffer but allow reading from old one until it contains data and after that dealocate it and keep only new one in use
One other thing I want to shout out, I saw what I thought was a really neat multicast ring buffer the other day where the author has an atomic for each element, rather than the typical reader/writer atomics. The promise was having much less contention on any given atomic, in most cases. https://github.com/rezabrizi/SPMC-Queue https://news.ycombinator.com/item?id=40410172
You never know how many concurrent threads still "are" on the place you wish to remove.
You would have to deal with stuff like hazard pointers, limbo lists and the like.
Better to keep the small arrays there.
If queue only allows to copy out data you can increase reader pointer after data were copied to different buffer, therefore nothing can be at the place we are removing
Perhaps we want to have any of the given multicast readers able to read more than one element at a time, and that does complicate things somewhat. But hardly impossible to handle.
Again: deeply disagreeing with the premise here that this can't be done. And it isn't even really a significant penalty, if your consumers do have to be async consumers that need to hold open their reading for a while. Unclear what the protests are.
The smaller arrays are not "left behind" in the garbage sense - the queue will use them again and again in the next rounds. See simulator. Re-use, not Re-cycle - the Garbage-Free Mantra.
If the Queue re-cycles the smaller arrays, it would not be garbage-free anymore.
If you still believe that the smaller arrays should be re-cycled (would be curious why), then comes the technical problem:
Let's imagine a reader stands immediately before reading the array (e.g. to check if the writer has already written). Now the OS preempts him. For how long: We don't know. In the meantime all things in the queue move forward and the program code in some other thread (writer probably) decides to de-allocate the array (and indeed does it).
Now the preempted reader wakes up and the first thing it does is to read from that (deallocated) array ...
[0] It needs a refactor, but here's some version of the idea. https://github.com/hmusgrave/zcirc
[1] You're not _really_ bounded since you have blocking calls to the underlying allocator (usually somewhere close to the OS, and even that can do more work than you might expect when you ask for a contiguous array), but it's still much cheaper than a bulk copy.
Interesting contribution policy. Makes sense in lieu of producing a strict style guide, I suppose.
Archived repos however have that ugly yellow warning.
For example unit tests. At least to show that it works, and how to use it. Also a build config, like gradle/maven, so others would be able to use this lib.
sadly it seems to be abandonware, with no commits in the last 10 years and compilation errors on Python 3.9 and above [1]
0: http://stutzbachenterprises.com/blist/implementation.html
Is there a reason you chose not implement java.util.Queue?
Look e.g. at the disclaimers at the size method of java.util.concurrent.ConcurrentLinkedQueue.
In my case, I needed a way to book and release two-ports tuples really fast to accommodate a RTP simulator. So, I wrote that slotmachine data structure and have been running in in production for months and can confirm: yes, performance is good.
Note: I should mention that my approach is almost exactly opposite to yours: I create a final backing slice, then create the traversal slices.