Optimize out a lot of std::string construction and allocations (#415)

Update CommandLineParameters.hpp

Update CommandLineParametersTests.cpp

Update WebContext.hpp

xmake check-files -f

Fix MaterialPassRegistry
This commit is contained in:
Jérôme Leclercq
2023-12-30 14:50:57 +01:00
committed by GitHub
parent f7c9060364
commit 79ec135af7
57 changed files with 219 additions and 210 deletions

View File

@@ -17,7 +17,7 @@ namespace Nz
{
struct AnimationImpl
{
std::unordered_map<std::string, std::size_t> sequenceMap;
std::unordered_map<std::string, std::size_t, StringHash<>, std::equal_to<>> sequenceMap;
std::vector<Sequence> sequences;
std::vector<SequenceJoint> sequenceJoints; // Uniquement pour les animations squelettiques
AnimationType type;
@@ -47,7 +47,7 @@ namespace Nz
Animation::Animation(Animation&&) noexcept = default;
Animation::~Animation() = default;
bool Animation::AddSequence(const Sequence& sequence)
bool Animation::AddSequence(Sequence sequence)
{
NazaraAssert(m_impl, "Animation not created");
NazaraAssert(sequence.frameCount > 0, "Sequence frame count must be over zero");
@@ -76,7 +76,7 @@ namespace Nz
m_impl->sequenceMap[sequence.name] = static_cast<std::size_t>(m_impl->sequences.size());
}
m_impl->sequences.push_back(sequence);
m_impl->sequences.emplace_back(std::move(sequence));
return true;
}
@@ -145,7 +145,7 @@ namespace Nz
return m_impl->jointCount;
}
Sequence* Animation::GetSequence(const std::string& sequenceName)
Sequence* Animation::GetSequence(std::string_view sequenceName)
{
NazaraAssert(m_impl, "Animation not created");
@@ -167,7 +167,7 @@ namespace Nz
return &m_impl->sequences[index];
}
const Sequence* Animation::GetSequence(const std::string& sequenceName) const
const Sequence* Animation::GetSequence(std::string_view sequenceName) const
{
NazaraAssert(m_impl, "Animation not created");
@@ -196,7 +196,7 @@ namespace Nz
return static_cast<std::size_t>(m_impl->sequences.size());
}
std::size_t Animation::GetSequenceIndex(const std::string& sequenceName) const
std::size_t Animation::GetSequenceIndex(std::string_view sequenceName) const
{
NazaraAssert(m_impl, "Animation not created");
@@ -233,7 +233,7 @@ namespace Nz
return m_impl->type;
}
bool Animation::HasSequence(const std::string& sequenceName) const
bool Animation::HasSequence(std::string_view sequenceName) const
{
NazaraAssert(m_impl, "Animation not created");
@@ -259,7 +259,7 @@ namespace Nz
return m_impl != nullptr;
}
void Animation::RemoveSequence(const std::string& identifier)
void Animation::RemoveSequence(std::string_view identifier)
{
NazaraAssert(m_impl, "Animation not created");

View File

@@ -262,7 +262,7 @@ namespace Nz
return true;
}
void MD5AnimParser::Error(const std::string& message)
void MD5AnimParser::Error(std::string_view message)
{
NazaraErrorFmt("{0} at line #{1}", message, m_lineCount);
}
@@ -504,14 +504,14 @@ namespace Nz
return true;
}
void MD5AnimParser::Warning(const std::string& message)
void MD5AnimParser::Warning(std::string_view message)
{
NazaraWarning(message + " at line #" + NumberToString(m_lineCount));
NazaraWarningFmt("{0} at line #{1}", message, m_lineCount);
}
void MD5AnimParser::UnrecognizedLine(bool error)
{
std::string message = "Unrecognized \"" + m_currentLine + '"';
std::string message = "unrecognized \"" + m_currentLine + '"';
if (error)
Error(message);

View File

@@ -209,7 +209,7 @@ namespace Nz
return true;
}
void MD5MeshParser::Error(const std::string& message)
void MD5MeshParser::Error(std::string_view message)
{
NazaraErrorFmt("{0} on line #{1}", message, m_lineCount);
}
@@ -444,14 +444,14 @@ namespace Nz
return true;
}
void MD5MeshParser::Warning(const std::string& message)
void MD5MeshParser::Warning(std::string_view message)
{
NazaraWarningFmt("{0} on line #{1}", message, m_lineCount);
}
void MD5MeshParser::UnrecognizedLine(bool error)
{
std::string message = "Unrecognized \"" + m_currentLine + '"';
std::string message = "unrecognized \"" + m_currentLine + '"';
if (error)
Error(message);

View File

@@ -14,7 +14,7 @@ namespace Nz
namespace
{
template<std::size_t N>
bool TestKeyword(const std::string& currentLine, const char(&keyword)[N], std::size_t& offset)
bool TestKeyword(std::string_view currentLine, const char(&keyword)[N], std::size_t& offset)
{
if (currentLine.size() > N && StartsWith(currentLine, keyword, CaseIndependent{}) && std::isspace(currentLine[N - 1]))
{

View File

@@ -57,7 +57,7 @@ namespace Nz
return (extension == ".obj");
}
bool SaveOBJToStream(const Mesh& mesh, const std::string& format, Stream& stream, const MeshParams& parameters)
bool SaveOBJToStream(const Mesh& mesh, std::string_view format, Stream& stream, const MeshParams& parameters)
{
NAZARA_USE_ANONYMOUS_NAMESPACE

View File

@@ -235,7 +235,7 @@ namespace Nz
return s_formatHandlers.find(extension) != s_formatHandlers.end();
}
bool SaveToStream(const Image& image, const std::string& format, Stream& stream, const ImageParams& parameters)
bool SaveToStream(const Image& image, std::string_view format, Stream& stream, const ImageParams& parameters)
{
NazaraUnused(parameters);
@@ -252,7 +252,7 @@ namespace Nz
return false;
}
auto it = s_formatHandlers.find(std::string_view(format));
auto it = s_formatHandlers.find(format);
NazaraAssert(it != s_formatHandlers.end(), "Invalid handler");
const FormatHandler& handler = it->second;

View File

@@ -1146,7 +1146,7 @@ namespace Nz
return utility->GetImageSaver().SaveToFile(*this, filePath, params);
}
bool Image::SaveToStream(Stream& stream, const std::string& format, const ImageParams& params)
bool Image::SaveToStream(Stream& stream, std::string_view format, const ImageParams& params)
{
Utility* utility = Utility::Instance();
NazaraAssert(utility, "Utility module has not been initialized");

View File

@@ -55,19 +55,18 @@ namespace Nz
InvalidateAABB();
}
void Mesh::AddSubMesh(const std::string& identifier, std::shared_ptr<SubMesh> subMesh)
void Mesh::AddSubMesh(std::string identifier, std::shared_ptr<SubMesh> subMesh)
{
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(!identifier.empty(), "Identifier is empty");
NazaraAssert(m_subMeshMap.find(identifier) == m_subMeshMap.end(), "SubMesh identifier \"" + identifier + "\" is already in use");
NazaraAssert(subMesh, "Invalid submesh");
NazaraAssert(!identifier.empty(), "empty identifier");
NazaraAssertFmt(!m_subMeshMap.contains(identifier), "SubMesh identifier \"{0}\" is already in use", identifier);
NazaraAssert(subMesh, "invalid submesh");
NazaraAssert(subMesh->GetAnimationType() == m_animationType, "Submesh animation type doesn't match mesh animation type");
std::size_t index = m_subMeshes.size();
AddSubMesh(std::move(subMesh));
m_subMeshMap[identifier] = static_cast<std::size_t>(index);
m_subMeshMap.emplace(std::move(identifier), index);
}
std::shared_ptr<SubMesh> Mesh::BuildSubMesh(const Primitive& primitive, const MeshParams& params)
@@ -404,12 +403,12 @@ namespace Nz
return &m_skeleton;
}
const std::shared_ptr<SubMesh>& Mesh::GetSubMesh(const std::string& identifier) const
const std::shared_ptr<SubMesh>& Mesh::GetSubMesh(std::string_view identifier) const
{
NazaraAssert(m_isValid, "Mesh should be created first");
auto it = m_subMeshMap.find(identifier);
NazaraAssert(it != m_subMeshMap.end(), "SubMesh " + identifier + " not found");
NazaraAssertFmt(it != m_subMeshMap.end(), "SubMesh {0} not found", identifier);
return m_subMeshes[it->second].subMesh;
}
@@ -429,12 +428,12 @@ namespace Nz
return static_cast<std::size_t>(m_subMeshes.size());
}
std::size_t Mesh::GetSubMeshIndex(const std::string& identifier) const
std::size_t Mesh::GetSubMeshIndex(std::string_view identifier) const
{
NazaraAssert(m_isValid, "Mesh should be created first");
auto it = m_subMeshMap.find(identifier);
NazaraAssert(it != m_subMeshMap.end(), "SubMesh " + identifier + " not found");
NazaraAssertFmt(it != m_subMeshMap.end(), "SubMesh {0} not found", identifier);
return it->second;
}
@@ -470,17 +469,15 @@ namespace Nz
OnMeshInvalidateAABB(this);
}
bool Mesh::HasSubMesh(const std::string& identifier) const
bool Mesh::HasSubMesh(std::string_view identifier) const
{
NazaraAssert(m_isValid, "Mesh should be created first");
return m_subMeshMap.find(identifier) != m_subMeshMap.end();
return m_subMeshMap.contains(identifier);
}
bool Mesh::HasSubMesh(std::size_t index) const
{
NazaraAssert(m_isValid, "Mesh should be created first");
return index < m_subMeshes.size();
}
@@ -523,7 +520,7 @@ namespace Nz
}
}
void Mesh::RemoveSubMesh(const std::string& identifier)
void Mesh::RemoveSubMesh(std::string_view identifier)
{
std::size_t index = GetSubMeshIndex(identifier);
RemoveSubMesh(index);
@@ -554,7 +551,7 @@ namespace Nz
return utility->GetMeshSaver().SaveToFile(*this, filePath, params);
}
bool Mesh::SaveToStream(Stream& stream, const std::string& format, const MeshParams& params)
bool Mesh::SaveToStream(Stream& stream, std::string_view format, const MeshParams& params)
{
Utility* utility = Utility::Instance();
NazaraAssert(utility, "Utility module has not been initialized");
@@ -584,7 +581,7 @@ namespace Nz
m_materialData.resize(matCount);
#ifdef NAZARA_DEBUG
#ifdef NAZARA_DEBUG
for (SubMeshData& data : m_subMeshes)
{
std::size_t matIndex = data.subMesh->GetMaterialIndex();
@@ -594,7 +591,7 @@ namespace Nz
NazaraWarning("SubMesh " + PointerToString(data.subMesh.get()) + " material index is over mesh new material count (" + NumberToString(matIndex) + " >= " + NumberToString(matCount) + "), setting it to first material");
}
}
#endif
#endif
}
void Mesh::Transform(const Matrix4f& matrix)

View File

@@ -4,6 +4,7 @@
#include <Nazara/Utility/Skeleton.hpp>
#include <Nazara/Utility/Joint.hpp>
#include <NazaraUtils/StringHash.hpp>
#include <unordered_map>
#include <Nazara/Utility/Debug.hpp>
@@ -11,7 +12,7 @@ namespace Nz
{
struct SkeletonImpl
{
std::unordered_map<std::string, std::size_t> jointMap;
std::unordered_map<std::string, std::size_t, StringHash<>, std::equal_to<>> jointMap;
std::vector<Joint> joints;
Boxf aabb;
bool aabbUpdated = false;
@@ -68,7 +69,7 @@ namespace Nz
return m_impl->aabb;
}
Joint* Skeleton::GetJoint(const std::string& jointName)
Joint* Skeleton::GetJoint(std::string_view jointName)
{
NazaraAssert(m_impl, "skeleton must have been created");
@@ -91,7 +92,7 @@ namespace Nz
return &m_impl->joints[index];
}
const Joint* Skeleton::GetJoint(const std::string& jointName) const
const Joint* Skeleton::GetJoint(std::string_view jointName) const
{
NazaraAssert(m_impl, "skeleton must have been created");
@@ -132,7 +133,7 @@ namespace Nz
return static_cast<std::size_t>(m_impl->joints.size());
}
std::size_t Skeleton::GetJointIndex(const std::string& jointName) const
std::size_t Skeleton::GetJointIndex(std::string_view jointName) const
{
NazaraAssert(m_impl, "skeleton must have been created");
@@ -259,9 +260,8 @@ namespace Nz
const std::string& name = m_impl->joints[i].GetName();
if (!name.empty())
{
NazaraAssert(m_impl->jointMap.find(name) == m_impl->jointMap.end(), "Joint name \"" + name + "\" is already present in joint map");
m_impl->jointMap[name] = static_cast<std::size_t>(i);
NazaraAssertFmt(!m_impl->jointMap.contains(name), "Joint name \{0}\" is already present in joint map", name);
m_impl->jointMap.emplace(name, i);
}
}