From 92be0a347cdfd9c407611982c72938ecde777a8c Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Mon, 25 May 2009 18:20:50 +0000 Subject: PollableQueue optimization - replace deque with vector. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@778464 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/sys/PollableQueue.h | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'cpp/src/qpid/sys/PollableQueue.h') diff --git a/cpp/src/qpid/sys/PollableQueue.h b/cpp/src/qpid/sys/PollableQueue.h index f8acf0a5f6..1d390a6eb0 100644 --- a/cpp/src/qpid/sys/PollableQueue.h +++ b/cpp/src/qpid/sys/PollableQueue.h @@ -28,7 +28,7 @@ #include #include #include -#include +#include namespace qpid { namespace sys { @@ -44,16 +44,18 @@ class Poller; template class PollableQueue { public: - typedef std::deque Queue; + typedef std::vector Batch; typedef T value_type; /** * Callback to process a batch of items from the queue. * - * @param values Queue of values to process. Any items remaining + * @param batch Queue of values to process. Any items remaining * on return from Callback are put back on the queue. + * @return iterator pointing to the first un-processed item in batch. + * Items from this point up to batch.end() are put back on the queue. */ - typedef boost::function Callback; + typedef boost::function Callback; /** * Constructor; sets necessary parameters. @@ -99,7 +101,7 @@ class PollableQueue { mutable sys::Monitor lock; Callback callback; PollableCondition condition; - Queue queue, batch; + Batch queue, batch; Thread dispatcher; bool stopped; }; @@ -141,17 +143,18 @@ template void PollableQueue::dispatch(PollableCondition& cond) { } template void PollableQueue::process() { + // Called with lock held while (!stopped && !queue.empty()) { assert(batch.empty()); batch.swap(queue); + typename Batch::const_iterator putBack; { ScopedUnlock u(lock); // Allow concurrent push to queue. - callback(batch); - } - if (!batch.empty()) { - queue.insert(queue.begin(), batch.begin(), batch.end()); // put back unprocessed items. - batch.clear(); + putBack = callback(batch); } + // put back unprocessed items. + queue.insert(queue.begin(), putBack, typename Batch::const_iterator(batch.end())); + batch.clear(); } } -- cgit v1.2.1