optimize EventImpl refcounting
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Fri, 27 Jul 2007 17:18:14 +0200
changeset 1009 adc3ac9baea8
parent 1008 6f2ea723a1db
child 1010 3a252ceeac44
optimize EventImpl refcounting
src/simulator/event-id.cc
src/simulator/event-id.h
src/simulator/event-impl.cc
src/simulator/event-impl.h
src/simulator/scheduler-heap.cc
src/simulator/scheduler-heap.h
src/simulator/scheduler-list.cc
src/simulator/scheduler-list.h
src/simulator/scheduler-map.cc
src/simulator/scheduler-map.h
src/simulator/scheduler.cc
src/simulator/scheduler.h
src/simulator/simulator.cc
--- a/src/simulator/event-id.cc	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/event-id.cc	Fri Jul 27 17:18:14 2007 +0200
@@ -55,6 +55,11 @@
 {
   return m_eventImpl;
 }
+EventImpl *
+EventId::PeekEventImpl (void) const
+{
+  return PeekPointer (m_eventImpl);
+}
 uint64_t 
 EventId::GetTs (void) const
 {
--- a/src/simulator/event-id.h	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/event-id.h	Fri Jul 27 17:18:14 2007 +0200
@@ -54,6 +54,7 @@
    * subclasses of the Scheduler base class.
    */
   Ptr<EventImpl> GetEventImpl (void) const;
+  EventImpl *PeekEventImpl (void) const;
   uint64_t GetTs (void) const;
   uint32_t GetUid (void) const;
 private:
--- a/src/simulator/event-impl.cc	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/event-impl.cc	Fri Jul 27 17:18:14 2007 +0200
@@ -32,21 +32,6 @@
   : m_cancel (false),
     m_count (1)
 {}
-void
-EventImpl::Ref (void) const
-{
-  m_count++;
-}
-void
-EventImpl::Unref (void) const
-{
-  m_count--;
-  if (m_count == 0)
-    {
-      delete this;
-    }
-}
-
 void 
 EventImpl::Invoke (void)
 {
--- a/src/simulator/event-impl.h	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/event-impl.h	Fri Jul 27 17:18:14 2007 +0200
@@ -28,8 +28,8 @@
 class EventImpl {
 public:
   EventImpl ();
-  void Ref (void) const;
-  void Unref (void) const;
+  inline void Ref (void) const;
+  inline void Unref (void) const;
   virtual ~EventImpl () = 0;
   void Invoke (void);
   void Cancel (void);
@@ -44,4 +44,23 @@
 
 }; // namespace ns3
 
+namespace ns3 {
+
+void
+EventImpl::Ref (void) const
+{
+  m_count++;
+}
+void
+EventImpl::Unref (void) const
+{
+  m_count--;
+  if (m_count == 0)
+    {
+      delete this;
+    }
+}
+
+} // namespace ns3
+
 #endif /* EVENT_IMPL_H */
--- a/src/simulator/scheduler-heap.cc	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/scheduler-heap.cc	Fri Jul 27 17:18:14 2007 +0200
@@ -170,7 +170,7 @@
 }
 
 bool
-SchedulerHeap::RealIsEmpty (void) const
+SchedulerHeap::IsEmpty (void) const
 {
   return (m_heap.size () == 1)?true:false;
 }
@@ -223,7 +223,7 @@
 
 
 void
-SchedulerHeap::RealInsert (EventId id)
+SchedulerHeap::Insert (const EventId &id)
 {
   // acquire single ref
   EventImpl *event = GetPointer (id.GetEventImpl ());
@@ -235,25 +235,24 @@
 }
 
 EventId
-SchedulerHeap::RealPeekNext (void) const
+SchedulerHeap::PeekNext (void) const
 {
   std::pair<EventImpl *,Scheduler::EventKey> next = m_heap[Root ()];
   return EventId (next.first, next.second.m_ts, next.second.m_uid);
 }
-void     
-SchedulerHeap::RealRemoveNext (void)
+EventId
+SchedulerHeap::RemoveNext (void)
 {
   std::pair<EventImpl *,Scheduler::EventKey> next = m_heap[Root ()];
-  // release single ref
-  next.first->Unref ();
   Exch (Root (), Last ());
   m_heap.pop_back ();
   TopDown (Root ());
+  return EventId (Ptr<EventImpl> (next.first, false), next.second.m_ts, next.second.m_uid);
 }
 
 
 bool
-SchedulerHeap::RealRemove (EventId id)
+SchedulerHeap::Remove (const EventId &id)
 {
   uint32_t uid = id.GetUid ();
   for (uint32_t i = 1; i < m_heap.size (); i++)
--- a/src/simulator/scheduler-heap.h	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/scheduler-heap.h	Fri Jul 27 17:18:14 2007 +0200
@@ -35,13 +35,13 @@
   SchedulerHeap ();
   virtual ~SchedulerHeap ();
 
+  virtual void Insert (const EventId &id);
+  virtual bool IsEmpty (void) const;
+  virtual EventId PeekNext (void) const;
+  virtual EventId RemoveNext (void);
+  virtual bool Remove (const EventId &ev);
+
 private:
-  virtual void RealInsert (EventId id);
-  virtual bool RealIsEmpty (void) const;
-  virtual EventId RealPeekNext (void) const;
-  virtual void RealRemoveNext (void);
-  virtual bool RealRemove (EventId ev);
-
   typedef std::vector<std::pair<EventImpl *, Scheduler::EventKey> > BinaryHeap;
 
   inline uint32_t Parent (uint32_t id) const;
--- a/src/simulator/scheduler-list.cc	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/scheduler-list.cc	Fri Jul 27 17:18:14 2007 +0200
@@ -67,7 +67,7 @@
 }
 
 void
-SchedulerList::RealInsert (EventId id)
+SchedulerList::Insert (const EventId &id)
 {
   Scheduler::EventKey key;
   // acquire refcount on EventImpl
@@ -85,28 +85,27 @@
   m_events.push_back (std::make_pair (event, key));
 }
 bool 
-SchedulerList::RealIsEmpty (void) const
+SchedulerList::IsEmpty (void) const
 {
   return m_events.empty ();
 }
 EventId
-SchedulerList::RealPeekNext (void) const
+SchedulerList::PeekNext (void) const
 {
   std::pair<EventImpl *, EventKey> next = m_events.front ();
   return EventId (next.first, next.second.m_ts, next.second.m_uid);
 }
 
-void
-SchedulerList::RealRemoveNext (void)
+EventId
+SchedulerList::RemoveNext (void)
 {
   std::pair<EventImpl *, EventKey> next = m_events.front ();
-  // release single acquired ref.
-  next.first->Unref ();
   m_events.pop_front ();
+  return EventId (Ptr<EventImpl> (next.first,false), next.second.m_ts, next.second.m_uid);
 }
 
 bool
-SchedulerList::RealRemove (EventId id)
+SchedulerList::Remove (const EventId &id)
 {
   for (EventsI i = m_events.begin (); i != m_events.end (); i++) 
     {
--- a/src/simulator/scheduler-list.h	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/scheduler-list.h	Fri Jul 27 17:18:14 2007 +0200
@@ -37,13 +37,13 @@
   SchedulerList ();
   virtual ~SchedulerList ();
 
+  virtual void Insert (const EventId &id);
+  virtual bool IsEmpty (void) const;
+  virtual EventId PeekNext (void) const;
+  virtual EventId RemoveNext (void);
+  virtual bool Remove (const EventId &ev);
+
  private:
-  virtual void RealInsert (EventId id);
-  virtual bool RealIsEmpty (void) const;
-  virtual EventId RealPeekNext (void) const;
-  virtual void RealRemoveNext (void);
-  virtual bool RealRemove (EventId ev);
-
   inline bool IsLower (Scheduler::EventKey const*a, Scheduler::EventKey const*b) const;
 
   typedef std::list<std::pair<EventImpl*, EventKey> > Events;
--- a/src/simulator/scheduler-map.cc	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/scheduler-map.cc	Fri Jul 27 17:18:14 2007 +0200
@@ -88,7 +88,7 @@
 
 
 void
-SchedulerMap::RealInsert (EventId id)
+SchedulerMap::Insert (const EventId &id)
 {
   // acquire a single ref
   EventImpl *event = GetPointer (id.GetEventImpl ());
@@ -101,30 +101,29 @@
 }
 
 bool
-SchedulerMap::RealIsEmpty (void) const
+SchedulerMap::IsEmpty (void) const
 {
   return m_list.empty ();
 }
 
 EventId
-SchedulerMap::RealPeekNext (void) const
+SchedulerMap::PeekNext (void) const
 {
   EventMapCI i = m_list.begin ();
   NS_ASSERT (i != m_list.end ());
   
   return EventId (i->second, i->first.m_ts, i->first.m_uid);
 }
-void
-SchedulerMap::RealRemoveNext (void)
+EventId
+SchedulerMap::RemoveNext (void)
 {
   EventMapCI i = m_list.begin ();
-  // release single ref.
-  i->second->Unref ();
   m_list.erase (m_list.begin ());
+  return EventId (Ptr<EventImpl> (i->second, false), i->first.m_ts, i->first.m_uid);
 }
 
 bool
-SchedulerMap::RealRemove (EventId id)
+SchedulerMap::Remove (const EventId &id)
 {
   Scheduler::EventKey key;
   key.m_ts = id.GetTs ();
--- a/src/simulator/scheduler-map.h	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/scheduler-map.h	Fri Jul 27 17:18:14 2007 +0200
@@ -36,12 +36,12 @@
   SchedulerMap ();
   virtual ~SchedulerMap ();
 
+  virtual void Insert (const EventId &id);
+  virtual bool IsEmpty (void) const;
+  virtual EventId PeekNext (void) const;
+  virtual EventId RemoveNext (void);
+  virtual bool Remove (const EventId &ev);
 private:
-  virtual void RealInsert (EventId id);
-  virtual bool RealIsEmpty (void) const;
-  virtual EventId RealPeekNext (void) const;
-  virtual void RealRemoveNext (void);
-  virtual bool RealRemove (EventId ev);
 
   class EventKeyCompare {
   public:
--- a/src/simulator/scheduler.cc	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/scheduler.cc	Fri Jul 27 17:18:14 2007 +0200
@@ -27,33 +27,4 @@
 Scheduler::~Scheduler () 
 {}
 
-void
-Scheduler::Insert (EventId id)
-{
-  return RealInsert (id);
-}
-bool 
-Scheduler::IsEmpty (void) const
-{
-  return RealIsEmpty ();
-}
-EventId
-Scheduler::PeekNext (void) const
-{
-  NS_ASSERT (!RealIsEmpty ());
-  return RealPeekNext ();
-}
-void 
-Scheduler::RemoveNext (void)
-{
-  NS_ASSERT (!RealIsEmpty ());
-  return RealRemoveNext ();
-}
-bool
-Scheduler::Remove (EventId id)
-{
-  NS_ASSERT (!RealIsEmpty ());
-  return RealRemove (id);
-}
-
 }; // namespace ns3
--- a/src/simulator/scheduler.h	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/scheduler.h	Fri Jul 27 17:18:14 2007 +0200
@@ -33,13 +33,12 @@
  * This base class specifies the interface used to maintain the 
  * event list. If you want to provide a new event list scheduler, 
  * you need to create a subclass of this base class and implement 
- * all the private pure virtual methods defined here. Namely:
- *   - ns3::Scheduler::realInsert
- *   - ns3::Scheduler::realIsEmpty
- *   - ns3::Scheduler::realPeekNext
- *   - ns3::Scheduler::realPeekNextKey
- *   - ns3::Scheduler::realRemoveNext
- *   - ns3::Scheduler::realRemove
+ * all the pure virtual methods defined here. Namely:
+ *   - ns3::Scheduler::Insert
+ *   - ns3::Scheduler::IsEmpty
+ *   - ns3::Scheduler::PeekNext
+ *   - ns3::Scheduler::RemoveNext
+ *   - ns3::Scheduler::Remove
  *
  * If you need to provide a new event list scheduler without
  * editing the main simulator class, you need to also implement
@@ -58,13 +57,6 @@
 
   virtual ~Scheduler () = 0;
 
-  void Insert (EventId id);
-  bool IsEmpty (void) const;
-  EventId PeekNext (void) const;
-  void RemoveNext (void);
-  bool Remove (EventId);
-
-private:
   /**
    * \param event event to store in the event list
    * \param key timecode associated to this new event
@@ -72,23 +64,23 @@
    *
    * This method takes ownership of the event pointer.
    */
-  virtual void RealInsert (EventId id) = 0;
+  virtual void Insert (const EventId &id) = 0;
   /**
    * \returns true if the event list is empty and false otherwise.
    */
-  virtual bool RealIsEmpty (void) const = 0;
+  virtual bool IsEmpty (void) const = 0;
   /**
    * \returns a pointer to the next earliest event. The caller
    *      takes ownership of the returned pointer.
    *
    * This method cannot be invoked if the list is empty.
    */
-  virtual EventId RealPeekNext (void) const = 0;
+  virtual EventId PeekNext (void) const = 0;
   /**
    * This method cannot be invoked if the list is empty.
    * Remove the next earliest event from the event list.
    */
-  virtual void RealRemoveNext (void) = 0;
+  virtual EventId RemoveNext (void) = 0;
   /**
    * \param id the id of the event to remove
    * \param key the timecode of the event removed
@@ -97,7 +89,7 @@
    *
    * This methods cannot be invoked if the list is empty.
    */
-  virtual bool RealRemove (EventId id) = 0;
+  virtual bool Remove (const EventId &id) = 0;
 };
 
 }; // namespace ns3
--- a/src/simulator/simulator.cc	Fri Jul 27 15:37:22 2007 +0200
+++ b/src/simulator/simulator.cc	Fri Jul 27 17:18:14 2007 +0200
@@ -138,8 +138,7 @@
 void
 SimulatorPrivate::ProcessOneEvent (void)
 {
-  EventId next = m_events->PeekNext ();
-  m_events->RemoveNext ();
+  EventId next = m_events->RemoveNext ();
 
   NS_ASSERT (next.GetTs () >= m_currentTs);
   --m_unscheduledEvents;
@@ -151,7 +150,7 @@
     {
       m_log << "e "<<next.GetUid () << " " << next.GetTs () << std::endl;
     }
-  Ptr<EventImpl> event = next.GetEventImpl ();
+  EventImpl *event = next.PeekEventImpl ();
   event->Invoke ();
 }