Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2784


Ignore:
Timestamp:
Mar 15, 2009, 1:26:23 AM (15 years ago)
Author:
rgrieder
Message:
  • Using std::vector instead of std::list in register/unregister Iterator/ObjectListIterator. That is approximately 6 times faster because the list has very few elements.
  • Inlined getIdentifier() for sure by exporting the heavy part to another function.
  • Also eliminated the need of isFirstCall() because the "static initialisation chaos" only applies to variables that cannot be evaluated at compile time.
Location:
code/trunk/src/core
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/core/Identifier.h

    r2710 r2784  
    357357            static ClassIdentifier<T> *getIdentifier(const std::string& name);
    358358            void addObject(T* object);
    359             static bool isFirstCall();
    360359
    361360            void updateConfigValues(bool updateChildren = true) const;
    362361
    363362        private:
     363            static void initialiseIdentifier();
    364364            ClassIdentifier(const ClassIdentifier<T>& identifier) {}    // don't copy
    365365            ClassIdentifier()
     
    376376
    377377    template <class T>
    378     ClassIdentifier<T>* ClassIdentifier<T>::classIdentifier_s;
    379 
    380     /**
    381         @brief Returns true if the function gets called the first time, false otherwise.
    382         @return True if this function got called the first time.
    383     */
    384     template <class T>
    385     bool ClassIdentifier<T>::isFirstCall()
    386     {
    387         static bool bFirstCall = true;
    388 
    389         if (bFirstCall)
     378    ClassIdentifier<T>* ClassIdentifier<T>::classIdentifier_s = 0;
     379
     380    /**
     381        @brief Returns the only instance of this class.
     382        @return The unique Identifier
     383    */
     384    template <class T>
     385    ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
     386    {
     387        // check if the static field has already been filled
     388        if (ClassIdentifier<T>::classIdentifier_s == 0)
     389            ClassIdentifier<T>::initialiseIdentifier();
     390
     391        return ClassIdentifier<T>::classIdentifier_s;
     392    }
     393
     394    /**
     395        @brief Does the same as getIdentifier() but sets the name if this wasn't done yet.
     396        @param name The name of this Identifier
     397        @return The Identifier
     398    */
     399    template <class T>
     400    ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier(const std::string& name)
     401    {
     402        ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier();
     403        identifier->setName(name);
     404        return identifier;
     405    }
     406
     407    /**
     408        @brief Assigns the static field for the identifier singleton.
     409    */
     410    template <class T>
     411    void ClassIdentifier<T>::initialiseIdentifier()
     412    {
     413        // Get the name of the class
     414        std::string name = typeid(T).name();
     415
     416        // create a new identifier anyway. Will be deleted in Identifier::getIdentifier if not used.
     417        ClassIdentifier<T>* proposal = new ClassIdentifier<T>();
     418
     419        // Get the entry from the map
     420        ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)Identifier::getIdentifierSingleton(name, proposal);
     421
     422        if (ClassIdentifier<T>::classIdentifier_s == proposal)
    390423        {
    391             bFirstCall = false;
    392             return true;
     424            COUT(4) << "*** Identifier: Requested Identifier for " << name << " was not yet existing and got created." << std::endl;
    393425        }
    394426        else
    395427        {
    396             return false;
     428            COUT(4) << "*** Identifier: Requested Identifier for " << name << " was already existing and got assigned." << std::endl;
    397429        }
    398     }
    399 
    400     /**
    401         @brief Returns the only instance of this class.
    402         @return The unique Identifier
    403     */
    404     template <class T>
    405     ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier()
    406     {
    407         // check if the static field has already been filled
    408         if (ClassIdentifier<T>::isFirstCall())
    409         {
    410             // Get the name of the class
    411             std::string name = typeid(T).name();
    412 
    413             // create a new identifier anyway. Will be deleted in Identifier::getIdentifier if not used.
    414             ClassIdentifier<T>* proposal = new ClassIdentifier<T>();
    415 
    416             // Get the entry from the map
    417             ClassIdentifier<T>::classIdentifier_s = (ClassIdentifier<T>*)Identifier::getIdentifierSingleton(name, proposal);
    418 
    419             if (ClassIdentifier<T>::classIdentifier_s == proposal)
    420             {
    421                 COUT(4) << "*** Identifier: Requested Identifier for " << name << " was not yet existing and got created." << std::endl;
    422             }
    423             else
    424             {
    425                 COUT(4) << "*** Identifier: Requested Identifier for " << name << " was already existing and got assigned." << std::endl;
    426             }
    427         }
    428 
    429         // Finally return the unique ClassIdentifier
    430         return ClassIdentifier<T>::classIdentifier_s;
    431     }
    432 
    433     /**
    434         @brief Does the same as getIdentifier() but sets the name if this wasn't done yet.
    435         @param name The name of this Identifier
    436         @return The Identifier
    437     */
    438     template <class T>
    439     ClassIdentifier<T>* ClassIdentifier<T>::getIdentifier(const std::string& name)
    440     {
    441         ClassIdentifier<T>* identifier = ClassIdentifier<T>::getIdentifier();
    442         identifier->setName(name);
    443         return identifier;
    444430    }
    445431
  • code/trunk/src/core/Iterator.h

    r2662 r2784  
    7575                this->element_ = exp.element_;
    7676                this->list_ = exp.list_;
    77                 this->iterator_ = this->list_->registerIterator(this);
     77                this->list_->registerIterator(this);
    7878            }
    7979
     
    8686                this->element_ = other.element_;
    8787                this->list_ = other.list_;
    88                 this->iterator_ = this->list_->registerIterator(this);
     88                this->list_->registerIterator(this);
    8989            }
    9090
     
    9898                this->element_ = (element) ? static_cast<ObjectListBaseElement*>(element) : 0;
    9999                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
    100                 this->iterator_ = this->list_->registerIterator(this);
     100                this->list_->registerIterator(this);
    101101            }
    102102
     
    110110                this->element_ = (other.element_) ? static_cast<ObjectListBaseElement*>(other.element_) : 0;
    111111                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
    112                 this->iterator_ = this->list_->registerIterator(this);
     112                this->list_->registerIterator(this);
    113113            }
    114114
     
    118118            inline ~Iterator()
    119119            {
    120                 this->list_->unregisterIterator(this->iterator_);
     120                this->list_->unregisterIterator(this);
    121121            }
    122122
     
    128128            {
    129129                if (this->list_)
    130                     this->list_->unregisterIterator(this->iterator_);
     130                    this->list_->unregisterIterator(this);
    131131
    132132                this->element_ = exp.element_;
    133133                this->list_ = exp.list_;
    134                 this->iterator_ = this->list_->registerIterator(this);
     134                this->list_->registerIterator(this);
    135135
    136136                return (*this);
     
    144144            {
    145145                if (this->list_)
    146                     this->list_->unregisterIterator(this->iterator_);
     146                    this->list_->unregisterIterator(this);
    147147
    148148                this->element_ = other.element_;
    149149                this->list_ = other.list_;
    150                 this->iterator_ = this->list_->registerIterator(this);
     150                this->list_->registerIterator(this);
    151151
    152152                return (*this);
     
    161161            {
    162162                if (this->list_)
    163                     this->list_->unregisterIterator(this->iterator_);
     163                    this->list_->unregisterIterator(this);
    164164
    165165                this->element_ = (element) ? static_cast<ObjectListBaseElement*>(element) : 0;
    166166                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
    167                 this->iterator_ = this->list_->registerIterator(this);
     167                this->list_->registerIterator(this);
    168168
    169169                return (*this);
     
    179179            {
    180180                if (this->list_)
    181                     this->list_->unregisterIterator(this->iterator_);
     181                    this->list_->unregisterIterator(this);
    182182
    183183                this->element_ = (other.element_) ? (ObjectListBaseElement*)other.element_ : 0;
    184184                this->list_ = ClassIdentifier<O>::getIdentifier()->getObjects();
    185                 this->iterator_ = this->list_->registerIterator(this);
     185                this->list_->registerIterator(this);
    186186
    187187                return (*this);
     
    300300            ObjectListBaseElement* element_;       //!< The element the Iterator points at
    301301            ObjectListBase* list_;                 //!< The list wherein the element is
    302             std::list<void*>::iterator iterator_;  //!< The iterator in the notifying list of the ObjectList
    303302    };
    304303
  • code/trunk/src/core/ObjectListBase.cc

    r2171 r2784  
    7575    void ObjectListBase::notifyIterators(OrxonoxClass* object) const
    7676    {
    77         for (std::list<void*>::const_iterator it = this->iterators_.begin(); it != this->iterators_.end(); ++it)
     77        for (std::vector<void*>::const_iterator it = this->iterators_.begin(); it != this->iterators_.end(); ++it)
    7878            ((Iterator<OrxonoxClass>*)(*it))->incrementIfEqual(object);
    79         for (std::list<void*>::const_iterator it = this->objectListIterators_.begin(); it != this->objectListIterators_.end(); ++it)
     79        for (std::vector<void*>::const_iterator it = this->objectListIterators_.begin(); it != this->objectListIterators_.end(); ++it)
    8080            ((ObjectListIterator<OrxonoxClass>*)(*it))->incrementIfEqual(object);
    8181    }
  • code/trunk/src/core/ObjectListBase.h

    r2171 r2784  
    3838#define _ObjectListBase_H__
    3939
    40 #include <list>
     40#include <vector>
    4141
    4242#include "CorePrereqs.h"
     
    110110            inline Export rend() { return ObjectListBase::Export(this, 0); }
    111111
    112             inline std::list<void*>::iterator registerIterator(void* iterator) { return this->iterators_.insert(this->iterators_.begin(), iterator); }
    113             inline void unregisterIterator(const std::list<void*>::iterator& iterator) { this->iterators_.erase(iterator); }
    114             inline std::list<void*>::iterator registerObjectListIterator(void* iterator) { return this->objectListIterators_.insert(this->objectListIterators_.begin(), iterator); }
    115             inline void unregisterObjectListIterator(const std::list<void*>::iterator& iterator) { this->objectListIterators_.erase(iterator); }
     112            inline void registerIterator(void* iterator) { this->iterators_.push_back(iterator); }
     113            inline void unregisterIterator(void* iterator)
     114            {
     115                for (unsigned int i = 0; i < this->iterators_.size(); ++i)
     116                {
     117                    if (iterators_[i] == iterator)
     118                    {
     119                        iterators_.erase(iterators_.begin() + i);
     120                        break;
     121                    }
     122                }
     123            }
     124            inline void registerObjectListIterator(void* iterator) { this->objectListIterators_.push_back(iterator); }
     125            inline void unregisterObjectListIterator(void* iterator)
     126            {
     127                for (unsigned int i = 0; i < this->objectListIterators_.size(); ++i)
     128                {
     129                    if (objectListIterators_[i] == iterator)
     130                    {
     131                        objectListIterators_.erase(objectListIterators_.begin() + i);
     132                        break;
     133                    }
     134                }
     135            }
    116136            void notifyIterators(OrxonoxClass* object) const;
    117137
     
    122142            ObjectListBaseElement* first_;         //!< The first element in the list
    123143            ObjectListBaseElement* last_;          //!< The last element in the list
    124             std::list<void*> iterators_;           //!< A list of Iterators pointing on an element in this list
    125             std::list<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list
     144            std::vector<void*> iterators_;           //!< A list of Iterators pointing on an element in this list
     145            std::vector<void*> objectListIterators_; //!< A list of ObjectListIterators pointing on an element in this list
    126146    };
    127147}
  • code/trunk/src/core/ObjectListIterator.h

    r2662 r2784  
    6565            {
    6666                this->element_ = 0;
    67                 this->iterator_ = ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
     67                ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
    6868            }
    6969
     
    7575            {
    7676                this->element_ = element;
    77                 this->iterator_ = ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
     77                ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
    7878            }
    7979
     
    8585            {
    8686                this->element_ = other.element_;
    87                 this->iterator_ = ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
     87                ClassIdentifier<T>::getIdentifier()->getObjects()->registerObjectListIterator(this);
    8888            }
    8989
     
    9393            inline ~ObjectListIterator()
    9494            {
    95                 ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterObjectListIterator(this->iterator_);
     95                ClassIdentifier<T>::getIdentifier()->getObjects()->unregisterObjectListIterator(this);
    9696            }
    9797
     
    227227        private:
    228228            ObjectListElement<T>* element_;        //!< The element the Iterator points at
    229             std::list<void*>::iterator iterator_;  //!< The iterator in the notifying list of the ObjectList
    230229    };
    231230}
Note: See TracChangeset for help on using the changeset viewer.