Deque rotate on current node

François Dumont frs.dumont@gmail.com
Mon May 20 05:39:00 GMT 2019


Hi

   std::deque is supposed to be like a double-queue that you can attack 
from front and back. But currrently its implementation makes it behave 
differently when starting from a fresh deque. If push_back then all goes 
well, it is copy/move to the current internal 'node'. If push_front then 
a new 'node' is created and on next pop_back the initial node will be 
deallocated without having ever be used.

   This patch changes this behavior. As long as deque is empty we can 
play with its state to make it push_back- or push_front-ready. It will 
also benefit to the usage of deque in the std::stack.

   More generally it could really improve scenario in which deque is 
used as queue of elements exchanged between different threads. As you 
need to make sure that consumers are faster than producers then your 
deque should remain empty most of the time and so this proposed patch 
should avoid nodes to be allocated/deallocated all the time.

     * include/bits/deque.tcc (deque<>::_M_push_back_aux):
     Rotate on current node if deque is empty.
     (deue<>::_M_push_front_aux): Likewise.

Tested under Linux x86_64, ok to commit ?

François

-------------- next part --------------
A non-text attachment was scrubbed...
Name: deque_rotate.patch
Type: text/x-patch
Size: 1422 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20190520/4d3299f6/attachment.bin>


More information about the Libstdc++ mailing list