3 ms·
He keeps stating the case of one-element ring buffer. Is that a real concern ever?
by hzhou321 10y ago
He keeps stating the case of one-element ring buffer. Is that a real concern ever?
- alanbernstein 10y agoIt seemed like a sarcastic comment to me. Why would that ever be used?
- jsnell 10y agoIt's indeed a ridiculous data structure, but I did actually need it. It's a dynamically sized ring buffer with an optimization analogous to that of C++ strings; if the required capacity is small enough, the buffer is stored inline in the object rather than in a separate heap-allocated object. So something in the spirit of (but not exactly like): struct rb { union { Value* array; // Set N such that this array uses the same amount of space as the pointer. Value inline_array[N]; }; uint16_t read; uint16_t write; uint16_t capacity; } You'd dynamically switch between the two internal representations, and choose whether to read from array or inline_array based on whether capacity is larger than N. In this setup it'd be pretty common for N to be 1. Having to add a special case to every single method would kind of suck, generic code that could handle any size seemed like a nice property to have.
- lomnakkus 10y agoWeirdly, I think Haskell has an equivalent: MVar. It has its (low-level) uses, but its quite hard to get any sort of non-trivial (non-rendezvous) synchronization protocol right. It's incredibly easy to deadlock. (But that may be mostly to do with the MVar's paucity of non-blocking primitives.)
- qb45 10y agoProbably it was a joke though one can imagine the size being configurable which surely would lead to interesting results if somebody sets it to 1 for some reason (like troubleshooting).