Very little optimization by using a simpler scope_guard when possible
The version with the vector, that can be disabled etc, is “very” slow, so we use unique_ptr when we don’t need to disable it, and when it only contains one function
This commit is contained in:
@@ -103,8 +103,6 @@ void TCPSocketHandler::connect(const std::string& address, const std::string& po
|
|||||||
this->port = port;
|
this->port = port;
|
||||||
this->use_tls = tls;
|
this->use_tls = tls;
|
||||||
|
|
||||||
utils::ScopeGuard sg;
|
|
||||||
|
|
||||||
struct addrinfo* addr_res;
|
struct addrinfo* addr_res;
|
||||||
|
|
||||||
if (!this->connecting)
|
if (!this->connecting)
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ namespace utils
|
|||||||
{
|
{
|
||||||
// The given string MUST be a valid utf-8 string
|
// The given string MUST be a valid utf-8 string
|
||||||
unsigned char* res = new unsigned char[original.size()];
|
unsigned char* res = new unsigned char[original.size()];
|
||||||
ScopeGuard sg([&res]() { delete[] res;});
|
const auto sg = utils::make_scope_guard([&res](auto&&) { delete[] res;});
|
||||||
|
|
||||||
// pointer where we write valid chars
|
// pointer where we write valid chars
|
||||||
unsigned char* r = res;
|
unsigned char* r = res;
|
||||||
@@ -140,7 +140,7 @@ namespace utils
|
|||||||
else
|
else
|
||||||
throw std::runtime_error("Invalid UTF-8 passed to remove_invalid_xml_chars");
|
throw std::runtime_error("Invalid UTF-8 passed to remove_invalid_xml_chars");
|
||||||
}
|
}
|
||||||
return std::string(reinterpret_cast<char*>(res), r-res);
|
return {reinterpret_cast<char*>(res), static_cast<size_t>(r-res)};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string convert_to_utf8(const std::string& str, const char* charset)
|
std::string convert_to_utf8(const std::string& str, const char* charset)
|
||||||
@@ -152,7 +152,7 @@ namespace utils
|
|||||||
throw std::runtime_error("Cannot convert into UTF-8");
|
throw std::runtime_error("Cannot convert into UTF-8");
|
||||||
|
|
||||||
// Make sure cd is always closed when we leave this function
|
// Make sure cd is always closed when we leave this function
|
||||||
ScopeGuard sg([&]{ iconv_close(cd); });
|
const auto sg = utils::make_scope_guard([&](auto&&){ iconv_close(cd); });
|
||||||
|
|
||||||
size_t inbytesleft = str.size();
|
size_t inbytesleft = str.size();
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ namespace utils
|
|||||||
char* outbuf_ptr = outbuf;
|
char* outbuf_ptr = outbuf;
|
||||||
|
|
||||||
// Make sure outbuf is always deleted when we leave this function
|
// Make sure outbuf is always deleted when we leave this function
|
||||||
sg.add_callback([&]{ delete[] outbuf; });
|
const auto sg2 = utils::make_scope_guard([&](auto&&){ delete[] outbuf; });
|
||||||
|
|
||||||
bool done = false;
|
bool done = false;
|
||||||
while (done == false)
|
while (done == false)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -85,5 +86,11 @@ private:
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename F>
|
||||||
|
auto make_scope_guard(F&& f)
|
||||||
|
{
|
||||||
|
return std::unique_ptr<void, std::decay_t<F>>{(void*)1, std::forward<F>(f)};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user