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
+2 -1
View File
@@ -6,6 +6,7 @@
#include <memory>
#include <unordered_map>
#include <utility>
namespace util {
class RunnablesList {
@@ -14,7 +15,7 @@ namespace util {
public:
observer_handler add(runnable callback) {
int id = nextid++;
runnables[id] = callback;
runnables[id] = std::move(callback);
return observer_handler(new int(id), [this](int* id) {
runnables.erase(*id);
delete id;
+2 -1
View File
@@ -12,6 +12,7 @@
#include <iostream>
#include <functional>
#include <condition_variable>
#include <utility>
namespace util {
@@ -106,7 +107,7 @@ public:
std::string name,
supplier<std::shared_ptr<Worker<T, R>>> workersSupplier,
consumer<R&> resultConsumer
) : logger(name), resultConsumer(resultConsumer) {
) : logger(std::move(name)), resultConsumer(resultConsumer) {
const uint num_threads = std::thread::hardware_concurrency();
for (uint i = 0; i < num_threads; i++) {
threads.emplace_back(&ThreadPool<T,R>::threadLoop, this, i, workersSupplier());
+3 -3
View File
@@ -128,7 +128,7 @@ extern uint32_t util::decode_utf8(uint& size, const char* chr) {
return code;
}
std::string util::wstr2str_utf8(const std::wstring ws) {
std::string util::wstr2str_utf8(const std::wstring& ws) {
std::vector<char> chars;
char buffer[4];
for (wchar_t wc : ws) {
@@ -140,7 +140,7 @@ std::string util::wstr2str_utf8(const std::wstring ws) {
return std::string(chars.data(), chars.size());
}
std::wstring util::str2wstr_utf8(const std::string s) {
std::wstring util::str2wstr_utf8(const std::string& s) {
std::vector<wchar_t> chars;
size_t pos = 0;
uint size = 0;
@@ -167,7 +167,7 @@ bool util::is_integer(const std::wstring& text) {
return true;
}
bool util::is_valid_filename(std::wstring name) {
bool util::is_valid_filename(const std::wstring& name) {
for (wchar_t c : name) {
if (c < 31 || c == '/' || c == '\\' || c == '<' || c == '>' ||
c == ':' || c == '"' || c == '|' || c == '?' || c == '*'){
+3 -3
View File
@@ -18,11 +18,11 @@ namespace util {
uint encode_utf8(uint32_t c, ubyte* bytes);
uint32_t decode_utf8(uint& size, const char* bytes);
std::string wstr2str_utf8(const std::wstring ws);
std::wstring str2wstr_utf8(const std::string s);
std::string wstr2str_utf8(const std::wstring &ws);
std::wstring str2wstr_utf8(const std::string &s);
bool is_integer(const std::string& text);
bool is_integer(const std::wstring& text);
bool is_valid_filename(std::wstring name);
bool is_valid_filename(const std::wstring &name);
void ltrim(std::string &s);
void rtrim(std::string &s);