Skip to content

Commit

Permalink
[Sofa.Helper] Fix and micro-optimize AdvancedTimer (#2349)
Browse files Browse the repository at this point in the history
* [Sofa.Helper] Store id as an id rather than the message used to find the id

* [Sofa.Helper] Use map for constant complexity instead of a std::vector

* [SofaCore] Missing include

* [SofaCore] Missing include

* [SofaCore] Missing include

* [SofaCore] Missing include
  • Loading branch information
alxbilger authored Sep 21, 2021
1 parent 7169130 commit f413793
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/core/objectmodel/BaseNode.h>
#include <sofa/core/ConstraintParams.h>
#include <sofa/helper/ScopedAdvancedTimer.h>

namespace sofa::core::behavior
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sofa/core/ObjectFactory.h>
#include <sofa/core/behavior/MultiMatrix.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/helper/ScopedAdvancedTimer.h>

#include <sofa/simulation/mechanicalvisitor/MechanicalGetNonDiagonalMassesCountVisitor.h>
using sofa::simulation::mechanicalvisitor::MechanicalGetNonDiagonalMassesCountVisitor;
Expand Down
25 changes: 10 additions & 15 deletions SofaKernel/modules/SofaHelper/src/sofa/helper/AdvancedTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
#include <sofa/helper/system/thread/thread_specific_ptr.h>
#include <sofa/helper/system/thread/CTime.h>
#include <sofa/type/vector.h>
#include <sofa/helper/ScopedAdvancedTimer.h>

#include <ostream>
#include <istream>
#include <string>
#include <map>

Expand Down Expand Up @@ -167,10 +165,12 @@ class SOFA_HELPER_API AdvancedTimer

/// the list of the id names. the Ids are the indices in the vector
std::vector<std::string> idsList;
std::map<std::string, unsigned int> idsMap;

IdFactory()
{
idsList.push_back(std::string("0")); // ID 0 == "0" or empty string
idsMap.insert({"0", 0});
idsList.push_back("0");
}

public:
Expand All @@ -184,22 +184,17 @@ class SOFA_HELPER_API AdvancedTimer
if (name.empty())
return 0;
IdFactory& idfac = getInstance();
std::vector<std::string>::iterator it = idfac.idsList.begin();
unsigned int i = 0;

while (it != idfac.idsList.end() && (*it) != name)
{
++it;
i++;
}

if (it!=idfac.idsList.end())
return i;
else
const auto it = idfac.idsMap.find(name);
if (it == idfac.idsMap.end())
{
const auto idsMapSize = idfac.idsMap.size();
idfac.idsMap.insert({name, idsMapSize});
idfac.idsList.push_back(name);
return i;
return idsMapSize;
}

return it->second;
}

static std::size_t getLastID()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ namespace sofa::helper
{

ScopedAdvancedTimer::ScopedAdvancedTimer(const std::string& message)
: ScopedAdvancedTimer(message.c_str())
: m_id( message )
{
AdvancedTimer::stepBegin( m_id );
}

ScopedAdvancedTimer::ScopedAdvancedTimer( const char* message )
: message( message )
: m_id( message )
{
AdvancedTimer::stepBegin( message );
AdvancedTimer::stepBegin( m_id );
}

ScopedAdvancedTimer::~ScopedAdvancedTimer()
{
AdvancedTimer::stepEnd( message );
AdvancedTimer::stepEnd( m_id );
}

} /// sofa::helper
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <sofa/helper/config.h>
#include<string>

#include <sofa/helper/AdvancedTimer.h>

namespace sofa::helper
{

Expand All @@ -37,9 +39,10 @@ namespace sofa::helper
/// measurement recorded.
struct SOFA_HELPER_API ScopedAdvancedTimer
{
const char* message;
ScopedAdvancedTimer(const std::string& message);
ScopedAdvancedTimer( const char* message );
AdvancedTimer::IdStep m_id;

explicit ScopedAdvancedTimer(const std::string& message);
explicit ScopedAdvancedTimer( const char* message );
~ScopedAdvancedTimer();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <SofaImplicitOdeSolver/StaticSolver.h>

#include <sofa/core/ObjectFactory.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/helper/ScopedAdvancedTimer.h>
#include <sofa/simulation/MechanicalOperations.h>
#include <sofa/simulation/VectorOperations.h>
#include <sofa/core/behavior/MultiMatrix.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <sofa/simulation/Node.h>
#include <sofa/core/behavior/OdeSolver.h>
#include <sofa/simulation/TaskScheduler.h>
#include <sofa/helper/ScopedAdvancedTimer.h>

namespace sofa::simulation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <sofa/simulation/UpdateBoundingBoxVisitor.h>
#include <sofa/helper/system/thread/CTime.h>
#include <sofa/helper/LCPcalc.h>
#include <sofa/helper/ScopedAdvancedTimer.h>


#include <sofa/core/ObjectFactory.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <sofa/simulation/UpdateInternalDataVisitor.h>
#include <sofa/simulation/UpdateContextVisitor.h>
#include <sofa/simulation/UpdateMappingVisitor.h>
#include <sofa/helper/ScopedAdvancedTimer.h>
#include <cmath>
#include <iostream>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <sofa/simulation/UpdateMappingVisitor.h>
#include <sofa/simulation/UpdateMappingEndEvent.h>
#include <sofa/simulation/UpdateBoundingBoxVisitor.h>
#include <sofa/helper/ScopedAdvancedTimer.h>
#include <cmath>
#include <iostream>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
******************************************************************************/
#include <SofaGeneralMeshCollision/DirectSAPNarrowPhase.h>
#include <sofa/core/collision/Intersection.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/helper/ScopedAdvancedTimer.h>
#include <sofa/core/visual/VisualParams.h>
#include <sofa/core/ObjectFactory.h>
#include <unordered_map>
Expand Down

0 comments on commit f413793

Please sign in to comment.