PPI: avoid dynamic_cast in Connectors peer() by caching peer_ on connect()
tho [Fri, 27 Aug 2010 13:08:17 +0000 (13:08 +0000)]
Packet: minor mini optimization in next()

git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1699 270642c3-0616-0410-b53a-bc976706d245

senf/PPI/Connectors.cc
senf/PPI/Connectors.cci
senf/PPI/Connectors.hh
senf/PPI/MultiConnectorMixin.cti
senf/PPI/MultiConnectorMixin.ih
senf/Packets/Packet.cti
senf/Scheduler/ClockService.cci
senf/Scheduler/FIFORunner.cc

index 2a876b6..485312d 100644 (file)
@@ -84,6 +84,10 @@ prefix_ void senf::ppi::connector::Connector::connect(Connector & target)
         enqueueInitializable();
     if (! peer().initializationScheduled())
         peer().enqueueInitializable();
+
+    v_connected();
+    peer_->v_connected();
+
 }
 
 senf::ppi::connector::Connector::TraceState senf::ppi::connector::Connector::traceState_ (
@@ -201,8 +205,21 @@ prefix_ std::type_info const & senf::ppi::connector::Connector::packetTypeID()
     return typeid(void);
 }
 
+prefix_ void senf::ppi::connector::Connector::unregisterConnector()
+{
+    if (module_)
+        module_->unregisterConnector(*this);
+}
+
+prefix_ void senf::ppi::connector::Connector::setModule(module::Module & module)
+{
+    module_ = &module;
+}
+
 prefix_ void senf::ppi::connector::Connector::v_disconnected()
-    const
+{}
+
+prefix_ void senf::ppi::connector::Connector::v_connected()
 {}
 
 ///////////////////////////////////////////////////////////////////////////
@@ -214,6 +231,18 @@ prefix_ senf::ppi::connector::PassiveConnector::~PassiveConnector()
     unregisterConnector();
 }
 
+prefix_ void senf::ppi::connector::PassiveConnector::v_disconnected()
+{
+    Connector::v_disconnected();
+    peer_ = 0;
+}
+
+prefix_ void senf::ppi::connector::PassiveConnector::v_connected()
+{
+    Connector::v_connected();
+    peer_ = & dynamic_cast<ActiveConnector&>(Connector::peer());
+}
+
 ////////////////////////////////////////
 // private members
 
@@ -267,6 +296,18 @@ prefix_ senf::ppi::connector::ActiveConnector::~ActiveConnector()
     unregisterConnector();
 }
 
+prefix_ void senf::ppi::connector::ActiveConnector::v_disconnected()
+{
+    Connector::v_disconnected();
+    peer_ = 0;
+}
+
+prefix_ void senf::ppi::connector::ActiveConnector::v_connected()
+{
+    Connector::v_connected();
+    peer_ = & dynamic_cast<PassiveConnector&>(Connector::peer());
+}
+
 ////////////////////////////////////////
 // private members
 
@@ -335,6 +376,18 @@ prefix_ senf::Packet senf::ppi::connector::InputConnector::operator()()
     }
 }
 
+prefix_ void senf::ppi::connector::InputConnector::v_disconnected()
+{
+    Connector::v_disconnected();
+    peer_ = 0;
+}
+
+prefix_ void senf::ppi::connector::InputConnector::v_connected()
+{
+    Connector::v_connected();
+    peer_ = & dynamic_cast<OutputConnector&>(Connector::peer());
+}
+
 ////////////////////////////////////////
 // private members
 
@@ -348,6 +401,21 @@ prefix_ void senf::ppi::connector::InputConnector::v_dequeueEvent()
 {}
 
 ///////////////////////////////////////////////////////////////////////////
+// senf::ppi::connector::OutputConnector
+
+prefix_ void senf::ppi::connector::OutputConnector::v_disconnected()
+{
+    Connector::v_disconnected();
+    peer_ = 0;
+}
+
+prefix_ void senf::ppi::connector::OutputConnector::v_connected()
+{
+    Connector::v_connected();
+    peer_ = & dynamic_cast<InputConnector&>(Connector::peer());
+}
+
+///////////////////////////////////////////////////////////////////////////
 // senf::ppi::connector::GenericActiveInput
 
 ////////////////////////////////////////
@@ -361,6 +429,20 @@ prefix_ void senf::ppi::connector::GenericActiveInput::v_requestEvent()
 ///////////////////////////////////////////////////////////////////////////
 // senf::ppi::connector::GenericPassiveInput
 
+prefix_ void senf::ppi::connector::GenericPassiveInput::v_disconnected()
+{
+    PassiveConnector::v_disconnected();
+    InputConnector::v_disconnected();
+    peer_ = 0;
+}
+
+prefix_ void senf::ppi::connector::GenericPassiveInput::v_connected()
+{
+    PassiveConnector::v_connected();
+    InputConnector::v_connected();
+    peer_ = & dynamic_cast<GenericActiveOutput&>(Connector::peer());
+}
+
 ////////////////////////////////////////
 // private members
 
@@ -394,6 +476,58 @@ prefix_ void senf::ppi::connector::GenericPassiveInput::v_unthrottleEvent()
     }
 }
 
+///////////////////////////////////////////////////////////////////////////
+// senf::ppi::connector::GenericPassiveOutput
+
+prefix_ void senf::ppi::connector::GenericPassiveOutput::v_disconnected()
+{
+    PassiveConnector::v_disconnected();
+    OutputConnector::v_disconnected();
+    peer_ = 0;
+}
+
+prefix_ void senf::ppi::connector::GenericPassiveOutput::v_connected()
+{
+    PassiveConnector::v_connected();
+    OutputConnector::v_connected();
+    peer_ = & dynamic_cast<GenericActiveInput&>(Connector::peer());
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::ppi::connector::GenericActiveInput
+
+prefix_ void senf::ppi::connector::GenericActiveInput::v_disconnected()
+{
+    ActiveConnector::v_disconnected();
+    InputConnector::v_disconnected();
+    peer_ = 0;
+}
+
+prefix_ void senf::ppi::connector::GenericActiveInput::v_connected()
+{
+    ActiveConnector::v_connected();
+    InputConnector::v_connected();
+    peer_ = & dynamic_cast<GenericPassiveOutput&>(Connector::peer());
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::ppi::connector::GenericActiveOutput
+
+prefix_ void senf::ppi::connector::GenericActiveOutput::v_disconnected()
+{
+    ActiveConnector::v_disconnected();
+    OutputConnector::v_disconnected();
+    peer_ = 0;
+}
+
+prefix_ void senf::ppi::connector::GenericActiveOutput::v_connected()
+{
+    ActiveConnector::v_connected();
+    OutputConnector::v_connected();
+    peer_ = & dynamic_cast<GenericPassiveInput&>(Connector::peer());
+}
+
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "Connectors.mpp"
index 10d3399..2b21add 100644 (file)
@@ -82,27 +82,14 @@ prefix_ bool senf::ppi::connector::Connector::connected()
     return peer_;
 }
 
-prefix_ void senf::ppi::connector::Connector::unregisterConnector()
-{
-    if (module_)
-        module_->unregisterConnector(*this);
-}
-
-////////////////////////////////////////
-// private members
-
-prefix_ void senf::ppi::connector::Connector::setModule(module::Module & module)
-{
-    module_ = &module;
-}
-
 ///////////////////////////////////////////////////////////////////////////
 // senf::ppi::connector::PassiveConnector
 
 prefix_ senf::ppi::connector::ActiveConnector & senf::ppi::connector::PassiveConnector::peer()
     const
 {
-    return dynamic_cast<ActiveConnector&>(Connector::peer());
+    SENF_ASSERT(peer_, "senf::ppi::connect() call missing");
+    return *peer_;
 }
 
 prefix_ bool senf::ppi::connector::PassiveConnector::throttled()
@@ -191,7 +178,8 @@ prefix_ void senf::ppi::connector::PassiveConnector::emit()
 prefix_ senf::ppi::connector::PassiveConnector & senf::ppi::connector::ActiveConnector::peer()
     const
 {
-    return dynamic_cast<PassiveConnector&>(Connector::peer());
+    SENF_ASSERT(peer_, "senf::ppi::connect() call missing");
+    return *peer_;
 }
 
 prefix_ void senf::ppi::connector::ActiveConnector::onThrottle()
@@ -228,7 +216,8 @@ prefix_ senf::Packet senf::ppi::connector::InputConnector::read()
 prefix_ senf::ppi::connector::OutputConnector & senf::ppi::connector::InputConnector::peer()
     const
 {
-    return dynamic_cast<OutputConnector &>(Connector::peer());
+    SENF_ASSERT(peer_, "senf::ppi::connect() call missing");
+    return *peer_;
 }
 
 prefix_ senf::ppi::connector::InputConnector::queue_iterator
@@ -288,7 +277,8 @@ prefix_ void senf::ppi::connector::InputConnector::enqueue(Packet const & p)
 prefix_ senf::ppi::connector::InputConnector & senf::ppi::connector::OutputConnector::peer()
     const
 {
-    return dynamic_cast<InputConnector&>(Connector::peer());
+    SENF_ASSERT(peer_, "senf::ppi::connect() call missing");
+    return *peer_;
 }
 
 prefix_ void senf::ppi::connector::OutputConnector::operator()(Packet const & p)
@@ -319,7 +309,8 @@ prefix_ senf::ppi::connector::GenericPassiveInput::GenericPassiveInput()
 prefix_ senf::ppi::connector::GenericActiveOutput & senf::ppi::connector::GenericPassiveInput::peer()
     const
 {
-    return dynamic_cast<GenericActiveOutput&>(Connector::peer());
+    SENF_ASSERT(peer_, "senf::ppi::connect() call missing");
+    return *peer_;
 }
 
 prefix_ bool senf::ppi::connector::GenericPassiveInput::boolean_test()
@@ -334,7 +325,8 @@ prefix_ bool senf::ppi::connector::GenericPassiveInput::boolean_test()
 prefix_ senf::ppi::connector::GenericActiveInput & senf::ppi::connector::GenericPassiveOutput::peer()
     const
 {
-    return dynamic_cast<GenericActiveInput&>(Connector::peer());
+    SENF_ASSERT(peer_, "senf::ppi::connect() call missing");
+    return *peer_;
 }
 
 prefix_ bool senf::ppi::connector::GenericPassiveOutput::boolean_test()
@@ -357,7 +349,8 @@ prefix_ senf::ppi::connector::GenericPassiveOutput::GenericPassiveOutput()
 prefix_ senf::ppi::connector::GenericPassiveOutput & senf::ppi::connector::GenericActiveInput::peer()
     const
 {
-    return dynamic_cast<GenericPassiveOutput&>(Connector::peer());
+    SENF_ASSERT(peer_, "senf::ppi::connect() call missing");
+    return *peer_;
 }
 
 prefix_ bool senf::ppi::connector::GenericActiveInput::boolean_test()
@@ -381,7 +374,9 @@ prefix_ senf::ppi::connector::GenericActiveInput::GenericActiveInput()
 prefix_ senf::ppi::connector::GenericPassiveInput & senf::ppi::connector::GenericActiveOutput::peer()
     const
 {
-    return dynamic_cast<GenericPassiveInput&>(Connector::peer());
+//    return dynamic_cast<GenericPassiveInput&>(Connector::peer());
+    SENF_ASSERT(peer_, "senf::ppi::connect() call missing");
+    return *peer_;
 }
 
 prefix_ bool senf::ppi::connector::GenericActiveOutput::boolean_test()
index bb0724e..33e651f 100644 (file)
@@ -187,11 +187,12 @@ namespace connector {
 
         void unregisterConnector();
 
+        virtual void v_disconnected();
+        virtual void v_connected();
+
     private:
         virtual std::type_info const & packetTypeID();
 
-        virtual void v_disconnected() const;
-
         void setModule(module::Module & module);
 
         Connector * peer_;
@@ -251,6 +252,9 @@ namespace connector {
 
         void emit();
 
+        virtual void v_disconnected();
+        virtual void v_connected();
+
     private:
         virtual void v_init();
 
@@ -269,11 +273,14 @@ namespace connector {
         void registerRoute(ForwardingRoute & route);
         void unregisterRoute(ForwardingRoute & route);
 
+        ActiveConnector * peer_;
+
         typedef ppi::detail::Callback<>::type Callback;
         Callback callback_;
 
         bool remoteThrottled_;
         bool nativeThrottled_;
+        ActiveConnector * myPeer_;
 
         typedef std::vector<ForwardingRoute*> Routes;
         Routes routes_;
@@ -331,6 +338,9 @@ namespace connector {
     protected:
         ActiveConnector();
 
+        virtual void v_disconnected();
+        virtual void v_connected();
+
     private:
         virtual void v_init();
 
@@ -342,6 +352,8 @@ namespace connector {
         void registerRoute(ForwardingRoute & route);
         void unregisterRoute(ForwardingRoute & route);
 
+        PassiveConnector * peer_;
+
         Callback throttleCallback_;
         Callback unthrottleCallback_;
 
@@ -406,6 +418,9 @@ namespace connector {
     protected:
         InputConnector();
 
+        virtual void v_disconnected();
+        virtual void v_connected();
+
     private:
         void enqueue(Packet const & p);
 
@@ -413,6 +428,7 @@ namespace connector {
         virtual void v_enqueueEvent();
         virtual void v_dequeueEvent();
 
+        OutputConnector * peer_;
         Queue queue_;
 
         friend class OutputConnector;
@@ -436,6 +452,12 @@ namespace connector {
 
     protected:
         OutputConnector();
+
+        virtual void v_disconnected();
+        virtual void v_connected();
+
+    private:
+        InputConnector * peer_;
     };
 
     /** \brief Combination of PassiveConnector and InputConnector
@@ -465,15 +487,18 @@ namespace connector {
         void qdisc(QueueingDiscipline::None_t);
                                         ///< Disable queueing discipline
 
-
     protected:
         GenericPassiveInput();
 
+        virtual void v_disconnected();
+        virtual void v_connected();
+
     private:
         void v_enqueueEvent();
         void v_dequeueEvent();
         void v_unthrottleEvent();
 
+        GenericActiveOutput * peer_;
         boost::scoped_ptr<QueueingDiscipline> qdisc_;
     };
 
@@ -495,6 +520,11 @@ namespace connector {
     protected:
         GenericPassiveOutput();
 
+        virtual void v_disconnected();
+        virtual void v_connected();
+
+    private:
+        GenericActiveInput * peer_;
     };
 
     /** \brief Combination of ActiveConnector and InputConnector
@@ -513,8 +543,13 @@ namespace connector {
     protected:
         GenericActiveInput();
 
+        virtual void v_disconnected();
+        virtual void v_connected();
+
     private:
         void v_requestEvent();
+
+        GenericPassiveOutput * peer_;
     };
 
     /** \brief Combination of ActiveConnector and OutputConnector
@@ -533,8 +568,14 @@ namespace connector {
     protected:
         GenericActiveOutput();
 
+        virtual void v_disconnected();
+        virtual void v_connected();
+
+    private:
+        GenericPassiveInput * peer_;
     };
 
+
 #ifndef DOXYGEN
 
 #   define TypedConnector_Input read
index 463ba58..691038e 100644 (file)
@@ -35,8 +35,8 @@
 
 template <class Module, class Connector>
 prefix_ void senf::ppi::module::detail::MultiConnectorWrapper<Module, Connector>::v_disconnected()
-    const
 {
+    Connector::v_disconnected();
     static_cast<Module&>(this->module()).disconnected(*this);
 }
 
index 9d4a7f5..3741bf2 100644 (file)
@@ -56,7 +56,7 @@ namespace detail {
         : public Connector
     {
     private:
-        virtual void v_disconnected() const;
+        virtual void v_disconnected();
     };
 
 #ifndef DOXYGEN
index ecb8baa..bea3eaa 100644 (file)
@@ -78,7 +78,8 @@ prefix_ OtherPacket senf::Packet::next(NoThrow_t)
     const
 {
     Packet p (next(nothrow));
-    return p && p.is<OtherPacket>() ? p.as<OtherPacket>() : OtherPacket();
+    return p && p.is<OtherPacket>() ?
+            OtherPacket(p.ptr()->as<typename OtherPacket::type>()) : OtherPacket();
 }
 
 template <class OtherPacket>
index 9b70a3a..5fdf668 100644 (file)
@@ -49,7 +49,7 @@ namespace senf { namespace scheduler { ClockService::clock_type now(); } }
 
 prefix_ senf::ClockService::clock_type senf::ClockService::clock_m(abstime_type time)
 {
-    if (senf::scheduler::now() - baseClock_ > 1000000000ll)
+    if (scheduler::now() - baseClock_ > 1000000000ll)
         restart_m();
     boost::posix_time::time_duration delta (time - baseAbstime_);
     return baseClock_ + clock_type( delta.ticks() )
@@ -60,7 +60,7 @@ prefix_ senf::ClockService::abstime_type senf::ClockService::abstime_m(clock_typ
 {
     if (clock == 0)
         return abstime_type();
-    if (senf::scheduler::now() - baseClock_ > 1000000000ll)
+    if (scheduler::now() - baseClock_ > 1000000000ll)
         restart_m();
 #ifdef BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG
     return baseAbstime_ + boost::posix_time::nanoseconds(clock-baseClock_);
@@ -178,7 +178,7 @@ prefix_ senf::ClockService::clock_type senf::ClockService::in_days(int64_type v)
 
 prefix_ senf::ClockService::clock_type senf::ClockService::from_timeval(timeval const & time)
 {
-    return from_time_t(time.tv_sec) + ClockService::microseconds(time.tv_usec);
+    return from_time_t(time.tv_sec) + microseconds(time.tv_usec);
 }
 
 prefix_ void senf::ClockService::restart()
index 62e14f5..b1aaa6b 100644 (file)
@@ -224,11 +224,11 @@ prefix_ senf::scheduler::detail::FIFORunner::TaskList::iterator
 senf::scheduler::detail::FIFORunner::priorityEnd(TaskInfo::Priority p)
 {
     switch (p) {
-    case senf::scheduler::detail::FIFORunner::TaskInfo::PRIORITY_LOW :
+    case TaskInfo::PRIORITY_LOW :
         return tasks_.end();
-    case senf::scheduler::detail::FIFORunner::TaskInfo::PRIORITY_NORMAL :
+    case TaskInfo::PRIORITY_NORMAL :
         return TaskList::current(normalPriorityEnd_);
-    case senf::scheduler::detail::FIFORunner::TaskInfo::PRIORITY_HIGH :
+    case TaskInfo::PRIORITY_HIGH :
         return TaskList::current(highPriorityEnd_);
     }
     return tasks_.begin();