104 points · by vitpro2213 · 2y ago · 32 comments
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
[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.
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.