Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sofa.Helper] Fix and micro-optimize AdvancedTimer #2349

Merged
merged 6 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is not the topic of the PR and it is more about general c++ coding practice but including thousand of dependencies lines to get something that in fact is an integer is really problematic. I wonder what kind of design pattern should be used instead of these inner typedefs.


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