From ba3c0bb806ddbdb0d690468bd97db3d1dd33063d Mon Sep 17 00:00:00 2001 From: Oliver Kowalke Date: Sat, 22 Nov 2014 11:23:18 +0100 Subject: [PATCH] optimize queues : from Nat Goodspeed --- include/boost/fiber/bounded_queue.hpp | 17 ++++++----------- include/boost/fiber/unbounded_queue.hpp | 15 +++++---------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/include/boost/fiber/bounded_queue.hpp b/include/boost/fiber/bounded_queue.hpp index 1e484086..affa325a 100644 --- a/include/boost/fiber/bounded_queue.hpp +++ b/include/boost/fiber/bounded_queue.hpp @@ -93,7 +93,7 @@ private: state_t state_; std::size_t count_; typename node_type::ptr head_; - typename node_type::ptr tail_; + typename node_type::ptr * tail_; mutable mutex mtx_; condition not_empty_cond_; condition not_full_cond_; @@ -171,13 +171,8 @@ private: void push_tail_( typename node_type::ptr new_node) { - if ( is_empty_() ) - head_ = tail_ = new_node; - else - { - tail_->next = new_node; - tail_ = new_node; - } + *tail_ = new_node; + tail_ = &new_node->next; ++count_; } @@ -210,7 +205,7 @@ private: { typename node_type::ptr old_head = head_; head_ = old_head->next; - if ( 0 == head_) tail_ = 0; + if ( 0 == head_) tail_ = &head_; old_head->next = 0; return old_head; } @@ -222,7 +217,7 @@ public: state_( OPEN), count_( 0), head_(), - tail_( head_), + tail_( &head_), mtx_(), not_empty_cond_(), not_full_cond_(), @@ -240,7 +235,7 @@ public: state_( OPEN), count_( 0), head_(), - tail_( head_), + tail_( &head_), mtx_(), not_empty_cond_(), not_full_cond_(), diff --git a/include/boost/fiber/unbounded_queue.hpp b/include/boost/fiber/unbounded_queue.hpp index 20e08b40..a199d04d 100644 --- a/include/boost/fiber/unbounded_queue.hpp +++ b/include/boost/fiber/unbounded_queue.hpp @@ -90,7 +90,7 @@ private: state state_; typename node_type::ptr head_; - typename node_type::ptr tail_; + typename node_type::ptr * tail_; mutable mutex mtx_; condition not_empty_cond_; @@ -132,13 +132,8 @@ private: void push_tail_( typename node_type::ptr new_node) { - if ( is_empty_() ) - head_ = tail_ = new_node; - else - { - tail_->next = new_node; - tail_ = new_node; - } + *tail_ = new_node; + tail_ = &new_node->next; } value_type value_pop_() @@ -161,7 +156,7 @@ private: { typename node_type::ptr old_head = head_; head_ = old_head->next; - if ( 0 == head_) tail_ = 0; + if ( 0 == head_) tail_ = &head_; old_head->next = 0; return old_head; } @@ -170,7 +165,7 @@ public: unbounded_queue() : state_( OPEN), head_(), - tail_( head_), + tail_( &head_), mtx_(), not_empty_cond_() {}