Optimize parameter passing to avoid unnecessary copying

This commit is contained in:
Pugemon
2024-06-07 04:00:38 +03:00
parent 46ca1bb04a
commit f25a425cb9
123 changed files with 578 additions and 522 deletions
+10 -9
View File
@@ -8,29 +8,30 @@
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <utility>
namespace fs = std::filesystem;
void GLSLExtension::setVersion(std::string version) {
this->version = version;
this->version = std::move(version);
}
void GLSLExtension::setPaths(const ResPaths* paths) {
this->paths = paths;
}
void GLSLExtension::loadHeader(std::string name) {
void GLSLExtension::loadHeader(const std::string& name) {
fs::path file = paths->find("shaders/lib/"+name+".glsl");
std::string source = files::read_string(file);
addHeader(name, source);
}
void GLSLExtension::addHeader(std::string name, std::string source) {
headers[name] = source;
void GLSLExtension::addHeader(const std::string& name, std::string source) {
headers[name] = std::move(source);
}
void GLSLExtension::define(std::string name, std::string value) {
defines[name] = value;
void GLSLExtension::define(const std::string& name, std::string value) {
defines[name] = std::move(value);
}
const std::string& GLSLExtension::getHeader(const std::string& name) const {
@@ -57,7 +58,7 @@ bool GLSLExtension::hasHeader(const std::string& name) const {
return headers.find(name) != headers.end();
}
void GLSLExtension::undefine(std::string name) {
void GLSLExtension::undefine(const std::string& name) {
if (hasDefine(name)) {
defines.erase(name);
}
@@ -66,7 +67,7 @@ void GLSLExtension::undefine(std::string name) {
inline std::runtime_error parsing_error(
const fs::path& file,
uint linenum,
const std::string message) {
const std::string& message) {
return std::runtime_error("file "+file.string()+": "+message+
" at line "+std::to_string(linenum));
}
@@ -74,7 +75,7 @@ inline std::runtime_error parsing_error(
inline void parsing_warning(
const fs::path& file,
uint linenum, const
std::string message) {
std::string& message) {
std::cerr << "file "+file.string()+": warning: "+message+
" at line "+std::to_string(linenum) << std::endl;
}
+4 -4
View File
@@ -14,14 +14,14 @@ class GLSLExtension {
std::string version = "330 core";
const ResPaths* paths = nullptr;
void loadHeader(std::string name);
void loadHeader(const std::string& name);
public:
void setPaths(const ResPaths* paths);
void setVersion(std::string version);
void define(std::string name, std::string value);
void undefine(std::string name);
void addHeader(std::string name, std::string source);
void define(const std::string& name, std::string value);
void undefine(const std::string& name);
void addHeader(const std::string& name, std::string source);
const std::string& getHeader(const std::string& name) const;
const std::string getDefine(const std::string& name) const;
+2 -2
View File
@@ -15,7 +15,7 @@ inline double power(double base, int64_t power) {
}
parsing_error::parsing_error(
std::string message,
const std::string& message,
std::string_view filename,
std::string_view source,
uint pos,
@@ -332,6 +332,6 @@ std::string BasicParser::parseString(char quote, bool closeRequired) {
return ss.str();
}
parsing_error BasicParser::error(std::string message) {
parsing_error BasicParser::error(const std::string& message) {
return parsing_error(message, filename, source, pos, line, linestart);
}
+2 -2
View File
@@ -60,7 +60,7 @@ public:
uint linestart;
parsing_error(
std::string message,
const std::string& message,
std::string_view filename,
std::string_view source,
uint pos,
@@ -92,7 +92,7 @@ protected:
dynamic::Value parseNumber(int sign);
std::string parseString(char chr, bool closeRequired=true);
parsing_error error(std::string message);
parsing_error error(const std::string& message);
public:
std::string_view readUntil(char c);
+2 -2
View File
@@ -80,7 +80,7 @@ class OggStream : public PCMStream {
size_t totalSamples = 0;
bool seekable;
public:
OggStream(OggVorbis_File vf) : vf(std::move(vf)) {
OggStream(OggVorbis_File vf) : vf(vf) {
vorbis_info* info = ov_info(&vf, -1);
channels = info->channels;
sampleRate = info->rate;
@@ -158,5 +158,5 @@ std::unique_ptr<PCMStream> ogg::create_stream(const fs::path& file) {
if ((code = ov_fopen(file.u8string().c_str(), &vf))) {
throw std::runtime_error("vorbis: "+vorbis_error_message(code));
}
return std::make_unique<OggStream>(std::move(vf));
return std::make_unique<OggStream>(vf);
}
+12 -11
View File
@@ -5,12 +5,13 @@
#include <charconv>
#include <stdexcept>
#include <sstream>
#include <utility>
using namespace xml;
Attribute::Attribute(std::string name, std::string text)
: name(name),
text(text) {
: name(std::move(name)),
text(std::move(text)) {
}
const std::string& Attribute::getName() const {
@@ -104,14 +105,14 @@ glm::vec4 Attribute::asColor() const {
}
}
Node::Node(std::string tag) : tag(tag) {
Node::Node(std::string tag) : tag(std::move(tag)) {
}
void Node::add(xmlelement element) {
void Node::add(const xmlelement& element) {
elements.push_back(element);
}
void Node::set(std::string name, std::string text) {
void Node::set(const std::string& name, const std::string &text) {
attrs[name] = Attribute(name, text);
}
@@ -157,11 +158,11 @@ const xmlelements_map& Node::getAttributes() const {
}
Document::Document(std::string version, std::string encoding)
: version(version),
encoding(encoding) {
: version(std::move(version)),
encoding(std::move(encoding)) {
}
void Document::setRoot(xmlelement element) {
void Document::setRoot(const xmlelement &element) {
this->root = element;
}
@@ -336,7 +337,7 @@ xmldocument Parser::parse() {
return document;
}
xmldocument xml::parse(std::string filename, std::string source) {
xmldocument xml::parse(const std::string& filename, const std::string& source) {
Parser parser(filename, source);
return parser.parse();
}
@@ -357,7 +358,7 @@ inline void newline(
static void stringifyElement(
std::stringstream& ss,
const xmlelement element,
const xmlelement& element,
bool nice,
const std::string& indentStr,
int indent
@@ -414,7 +415,7 @@ static void stringifyElement(
}
std::string xml::stringify(
const xmldocument document,
const xmldocument& document,
bool nice,
const std::string& indentStr
) {
+5 -5
View File
@@ -46,12 +46,12 @@ namespace xml {
Node(std::string tag);
/// @brief Add sub-element
void add(xmlelement element);
void add(const xmlelement& element);
/// @brief Set attribute value. Creates attribute if does not exists
/// @param name attribute name
/// @param text attribute value
void set(std::string name, std::string text);
void set(const std::string& name, const std::string &text);
/// @brief Get element tag
const std::string& getTag() const;
@@ -101,7 +101,7 @@ namespace xml {
public:
Document(std::string version, std::string encoding);
void setRoot(xmlelement element);
void setRoot(const xmlelement &element);
xmlelement getRoot() const;
const std::string& getVersion() const;
@@ -129,7 +129,7 @@ namespace xml {
/// @param indentStr indentation characters sequence (default - 4 spaces)
/// @return XML string
extern std::string stringify(
const xmldocument document,
const xmldocument& document,
bool nice=true,
const std::string& indentStr=" "
);
@@ -138,7 +138,7 @@ namespace xml {
/// @param filename file name will be shown in error messages
/// @param source xml source code string
/// @return xml document
extern xmldocument parse(std::string filename, std::string source);
extern xmldocument parse(const std::string& filename, const std::string& source);
}
#endif // CODERS_XML_HPP_