mirror of https://github.com/OpenMW/openmw.git
Optimize MWRender::Animation::hasAnimation
Use a set to check for group start existence. Reduce time taken from 2.6% to 0.08% and MWMechanics::MechanicsManager::update from 7% to 5% in relative CPU time usage for a scene with ~100 actors.pull/2862/head
parent
61fbc1cd0b
commit
a59e25e093
@ -0,0 +1,87 @@
|
||||
#ifndef OPENMW_COMPONENTS_NIFOSG_TEXTKEYMAP
|
||||
#define OPENMW_COMPONENTS_NIFOSG_TEXTKEYMAP
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
namespace NifOsg
|
||||
{
|
||||
class TextKeyMap
|
||||
{
|
||||
public:
|
||||
using ConstIterator = std::multimap<float, std::string>::const_iterator;
|
||||
|
||||
auto begin() const noexcept
|
||||
{
|
||||
return mTextKeyByTime.begin();
|
||||
}
|
||||
|
||||
auto end() const noexcept
|
||||
{
|
||||
return mTextKeyByTime.end();
|
||||
}
|
||||
|
||||
auto rbegin() const noexcept
|
||||
{
|
||||
return mTextKeyByTime.rbegin();
|
||||
}
|
||||
|
||||
auto rend() const noexcept
|
||||
{
|
||||
return mTextKeyByTime.rend();
|
||||
}
|
||||
|
||||
auto lowerBound(float time) const
|
||||
{
|
||||
return mTextKeyByTime.lower_bound(time);
|
||||
}
|
||||
|
||||
auto upperBound(float time) const
|
||||
{
|
||||
return mTextKeyByTime.upper_bound(time);
|
||||
}
|
||||
|
||||
void emplace(float time, std::string&& textKey)
|
||||
{
|
||||
const auto separator = textKey.find(": ");
|
||||
if (separator != std::string::npos)
|
||||
mGroups.emplace(textKey.substr(0, separator));
|
||||
|
||||
mTextKeyByTime.emplace(time, std::move(textKey));
|
||||
}
|
||||
|
||||
bool empty() const noexcept
|
||||
{
|
||||
return mTextKeyByTime.empty();
|
||||
}
|
||||
|
||||
auto findGroupStart(const std::string &groupName) const
|
||||
{
|
||||
return std::find_if(mTextKeyByTime.begin(), mTextKeyByTime.end(), IsGroupStart{groupName});
|
||||
}
|
||||
|
||||
bool hasGroupStart(const std::string &groupName) const
|
||||
{
|
||||
return mGroups.count(groupName) > 0;
|
||||
}
|
||||
|
||||
private:
|
||||
struct IsGroupStart
|
||||
{
|
||||
const std::string &mGroupName;
|
||||
|
||||
bool operator ()(const std::multimap<float, std::string>::value_type& value) const
|
||||
{
|
||||
return value.second.compare(0, mGroupName.size(), mGroupName) == 0 &&
|
||||
value.second.compare(mGroupName.size(), 2, ": ") == 0;
|
||||
}
|
||||
};
|
||||
|
||||
std::set<std::string> mGroups;
|
||||
std::multimap<float, std::string> mTextKeyByTime;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue