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

@@ -8,9 +8,8 @@
#define NAZARA_CORE_COMMANDLINEPARAMETERS_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <NazaraUtils/Algorithm.hpp>
#include <string>
#include <string_view>
#include <NazaraUtils/StringHash.hpp>
#include <NazaraUtils/TypeTraits.hpp>
#include <unordered_map>
#include <unordered_set>
@@ -19,9 +18,9 @@ namespace Nz
class CommandLineParameters
{
public:
inline bool GetParameter(const std::string& name, std::string_view* value) const;
inline bool GetParameter(std::string_view name, std::string_view* value) const;
inline bool HasFlag(const std::string& flag) const;
inline bool HasFlag(std::string_view flag) const;
inline bool operator==(const CommandLineParameters& params) const;
inline bool operator!=(const CommandLineParameters& params) const;
@@ -30,8 +29,8 @@ namespace Nz
static inline CommandLineParameters Parse(int argc, const Pointer<const char>* argv);
private:
std::unordered_map<std::string, std::string> m_parameters;
std::unordered_set<std::string> m_flags;
std::unordered_map<std::string, std::string, StringHash<>, std::equal_to<>> m_parameters;
std::unordered_set<std::string, StringHash<>, std::equal_to<>> m_flags;
};
}

View File

@@ -6,7 +6,7 @@
namespace Nz
{
inline bool CommandLineParameters::GetParameter(const std::string& name, std::string_view* value) const
inline bool CommandLineParameters::GetParameter(std::string_view name, std::string_view* value) const
{
auto it = m_parameters.find(name);
if (it == m_parameters.end())
@@ -18,9 +18,9 @@ namespace Nz
return true;
}
inline bool CommandLineParameters::HasFlag(const std::string& flag) const
inline bool CommandLineParameters::HasFlag(std::string_view flag) const
{
return m_flags.find(flag) != m_flags.end();
return m_flags.contains(flag);
}
inline bool CommandLineParameters::operator==(const CommandLineParameters& params) const

View File

@@ -7,6 +7,8 @@
#ifndef NAZARA_CORE_OBJECTLIBRARY_HPP
#define NAZARA_CORE_OBJECTLIBRARY_HPP
#include <NazaraUtils/StringHash.hpp>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
@@ -24,15 +26,15 @@ namespace Nz
void Clear();
std::shared_ptr<Type> Get(const std::string& name);
bool Has(const std::string& name);
std::shared_ptr<Type> Get(std::string_view name);
bool Has(std::string_view name);
void Register(const std::string& name, std::shared_ptr<Type> object);
std::shared_ptr<Type> Query(const std::string& name);
void Unregister(const std::string& name);
void Register(std::string name, std::shared_ptr<Type> object);
std::shared_ptr<Type> Query(std::string_view name);
void Unregister(std::string_view name);
private:
std::unordered_map<std::string, std::shared_ptr<Type>> m_library;
std::unordered_map<std::string, std::shared_ptr<Type>, StringHash<>, std::equal_to<>> m_library;
};
}

View File

@@ -31,7 +31,7 @@ namespace Nz
* \remark Produces a NazaraError if object not found
*/
template<typename Type>
std::shared_ptr<Type> ObjectLibrary<Type>::Get(const std::string& name)
std::shared_ptr<Type> ObjectLibrary<Type>::Get(std::string_view name)
{
std::shared_ptr<Type> ref = Query(name);
if (!ref)
@@ -45,9 +45,9 @@ namespace Nz
* \return true if it the case
*/
template<typename Type>
bool ObjectLibrary<Type>::Has(const std::string& name)
bool ObjectLibrary<Type>::Has(std::string_view name)
{
return m_library.find(name) != m_library.end();
return m_library.contains(name);
}
/*!
@@ -57,9 +57,9 @@ namespace Nz
* \param object Object to stock
*/
template<typename Type>
void ObjectLibrary<Type>::Register(const std::string& name, std::shared_ptr<Type> object)
void ObjectLibrary<Type>::Register(std::string name, std::shared_ptr<Type> object)
{
m_library.emplace(name, object);
m_library.emplace(std::move(name), object);
}
/*!
@@ -69,7 +69,7 @@ namespace Nz
* \param name Name of the object
*/
template<typename Type>
std::shared_ptr<Type> ObjectLibrary<Type>::Query(const std::string& name)
std::shared_ptr<Type> ObjectLibrary<Type>::Query(std::string_view name)
{
auto it = m_library.find(name);
if (it != m_library.end())
@@ -84,7 +84,7 @@ namespace Nz
* \param name Name of the object
*/
template<typename Type>
void ObjectLibrary<Type>::Unregister(const std::string& name)
void ObjectLibrary<Type>::Unregister(std::string_view name)
{
m_library.erase(name);
}

View File

@@ -11,6 +11,7 @@
#include <Nazara/Core/Color.hpp>
#include <NazaraUtils/MovablePtr.hpp>
#include <NazaraUtils/Result.hpp>
#include <NazaraUtils/StringHash.hpp>
#include <atomic>
#include <string>
#include <unordered_map>
@@ -33,19 +34,19 @@ namespace Nz
inline void ForEach(const std::function<bool(const ParameterList& list, const std::string& name)>& callback);
inline void ForEach(const std::function<void(const ParameterList& list, const std::string& name)>& callback) const;
Result<bool, Error> GetBooleanParameter(const std::string& name, bool strict = true) const;
Result<Color, Error> GetColorParameter(const std::string& name, bool strict = true) const;
Result<double, Error> GetDoubleParameter(const std::string& name, bool strict = true) const;
Result<long long, Error> GetIntegerParameter(const std::string& name, bool strict = true) const;
Result<ParameterType, Error> GetParameterType(const std::string& name) const;
Result<void*, Error> GetPointerParameter(const std::string& name, bool strict = true) const;
Result<std::string, Error> GetStringParameter(const std::string& name, bool strict = true) const;
Result<std::string_view, Error> GetStringViewParameter(const std::string& name, bool strict = true) const;
Result<void*, Error> GetUserdataParameter(const std::string& name, bool strict = true) const;
Result<bool, Error> GetBooleanParameter(std::string_view name, bool strict = true) const;
Result<Color, Error> GetColorParameter(std::string_view name, bool strict = true) const;
Result<double, Error> GetDoubleParameter(std::string_view name, bool strict = true) const;
Result<long long, Error> GetIntegerParameter(std::string_view name, bool strict = true) const;
Result<ParameterType, Error> GetParameterType(std::string_view name) const;
Result<void*, Error> GetPointerParameter(std::string_view name, bool strict = true) const;
Result<std::string, Error> GetStringParameter(std::string_view name, bool strict = true) const;
Result<std::string_view, Error> GetStringViewParameter(std::string_view name, bool strict = true) const;
Result<void*, Error> GetUserdataParameter(std::string_view name, bool strict = true) const;
bool HasParameter(const std::string& name) const;
bool HasParameter(std::string_view name) const;
void RemoveParameter(const std::string& name);
void RemoveParameter(std::string_view name);
void SetParameter(std::string name);
void SetParameter(std::string name, const Color& value);
@@ -111,7 +112,7 @@ namespace Nz
Parameter& CreateValue(std::string&& name);
void DestroyValue(Parameter& parameter);
using ParameterMap = std::unordered_map<std::string, Parameter>;
using ParameterMap = std::unordered_map<std::string, Parameter, StringHash<>, std::equal_to<>>;
ParameterMap m_parameters;
};

View File

@@ -31,7 +31,7 @@ namespace Nz
struct Entry;
using FormatSupport = std::function<bool(std::string_view format)>;
using FileSaver = std::function<bool(const Type& resource, const std::filesystem::path& filePath, const Parameters& parameters)>;
using StreamSaver = std::function<bool(const Type& resource, const std::string& format, Stream& stream, const Parameters& parameters)>;
using StreamSaver = std::function<bool(const Type& resource, std::string_view format, Stream& stream, const Parameters& parameters)>;
ResourceSaver() = default;
ResourceSaver(const ResourceSaver&) = delete;
@@ -43,7 +43,7 @@ namespace Nz
bool IsExtensionSupported(std::string_view extension) const;
bool SaveToFile(const Type& resource, const std::filesystem::path& filePath, const Parameters& parameters = Parameters()) const;
bool SaveToStream(const Type& resource, Stream& stream, const std::string& format, const Parameters& parameters = Parameters()) const;
bool SaveToStream(const Type& resource, Stream& stream, std::string_view format, const Parameters& parameters = Parameters()) const;
const Entry* RegisterSaver(Entry saver);
void UnregisterSaver(const Entry* saver);

View File

@@ -124,7 +124,7 @@ namespace Nz
* \see SaveToFile
*/
template<typename Type, typename Parameters>
bool ResourceSaver<Type, Parameters>::SaveToStream(const Type& resource, Stream& stream, const std::string& format, const Parameters& parameters) const
bool ResourceSaver<Type, Parameters>::SaveToStream(const Type& resource, Stream& stream, std::string_view format, const Parameters& parameters) const
{
NazaraAssert(stream.IsWritable(), "Stream is not writable");
NazaraAssert(parameters.IsValid(), "Invalid parameters");