Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r66158 - trunk/boost/asio/detail
From: chris_at_[hidden]
Date: 2010-10-23 22:06:52


Author: chris_kohlhoff
Date: 2010-10-23 22:06:46 EDT (Sat, 23 Oct 2010)
New Revision: 66158
URL: http://svn.boost.org/trac/boost/changeset/66158

Log:
Fix vector reallocation performance problem. Refs #4780.

Text files modified:
   trunk/boost/asio/detail/timer_queue.hpp | 31 ++++++++++++++++---------------
   1 files changed, 16 insertions(+), 15 deletions(-)

Modified: trunk/boost/asio/detail/timer_queue.hpp
==============================================================================
--- trunk/boost/asio/detail/timer_queue.hpp (original)
+++ trunk/boost/asio/detail/timer_queue.hpp 2010-10-23 22:06:46 EDT (Sat, 23 Oct 2010)
@@ -79,36 +79,37 @@
   // function call may need to be interrupted and restarted.
   bool enqueue_timer(const time_type& time, per_timer_data& timer, timer_op* op)
   {
- // Ensure that there is space for the timer in the heap. We reserve here so
- // that the push_back below will not throw due to a reallocation failure.
- heap_.reserve(heap_.size() + 1);
-
- timer.op_queue_.push(op);
+ // Enqueue the timer object.
     if (timer.prev_ == 0 && &timer != timers_)
     {
- // Insert the new timer into the linked list of active timers.
- timer.next_ = timers_;
- timer.prev_ = 0;
- if (timers_)
- timers_->prev_ = &timer;
- timers_ = &timer;
-
- // Put the new timer at the correct position in the heap.
       if (this->is_positive_infinity(time))
       {
+ // No heap entry is required for timers that never expire.
         timer.heap_index_ = (std::numeric_limits<std::size_t>::max)();
- return false; // No need to interrupt reactor as timer never expires.
       }
       else
       {
+ // Put the new timer at the correct position in the heap. This is done
+ // first since push_back() can throw due to allocation failure.
         timer.heap_index_ = heap_.size();
         heap_entry entry = { time, &timer };
         heap_.push_back(entry);
         up_heap(heap_.size() - 1);
       }
+
+ // Insert the new timer into the linked list of active timers.
+ timer.next_ = timers_;
+ timer.prev_ = 0;
+ if (timers_)
+ timers_->prev_ = &timer;
+ timers_ = &timer;
     }
 
- return (heap_[0].timer_ == &timer);
+ // Enqueue the individual timer operation.
+ timer.op_queue_.push(op);
+
+ // Interrupt reactor only if newly added timer is first to expire.
+ return timer.heap_index_ == 0 && timer.op_queue_.front() == op;
   }
 
   // Whether there are no timers in the queue.


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk