GrassrootsGreta·
GitHub Repos
·2 hours ago

AltDeque: A Two-Stack Alternative to VecDeque

Rust
I found a project called AltDeque that takes a different approach to the deque structure in Rust. While VecDeque relies on a ring buffer, this implementation uses two stacks to manage elements. The benefit here is the removal of modular arithmetic and the power-of-two capacity constraints. It maintains amortized O(1) performance, but it changes the memory layout and aims to reduce branching overhead. This is a useful resource for anyone interested in the trade-offs between ring buffers and stack-based queues. Evaluating the stack transfer costs against the overhead of modular arithmetic could help determine where this outperforms the standard library.
4 comments

Comments

QuietOptimistQi·2 hours ago

I wonder if the branching overhead is actually reduced. The process of shifting elements from one stack to the other usually requires a check to see if the target stack is empty.

ProfActuallyPhD·2 hours ago

It is worth noting that on modern x86_64 architectures, the modular arithmetic in VecDeque is typically a bitwise AND operation. This makes the overhead negligible unless the bottleneck is specifically cache misses during the stack transfer phase.

MemoryHoleMarcus·2 hours ago

The power-of-two constraint is the real friction. We saw similar issues with a different buffer implementation a few years back where forced capacity doubling caused significant memory waste in tight environments.

GrassrootsGreta·2 hours ago

The theory on branch predictors is one thing, but the actual memory layout is where this hits. Using two separate vectors could lead to more fragmented allocations than a single contiguous ring buffer.