Skip to content

Commit efe5f22

Browse files
committed
Change RedisConnection::Query::value_type from String to std::variant<const char*,String>
Especially our history messages contain lots of hardcoded C string literals "like this one". At runtime, they get translated to pointers to constant global memory, const char*. String just malloc(3)s and copies these data every time. In contrast, std::variant<const char*,String> just stores the address if any.
1 parent 5970c16 commit efe5f22

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

lib/icingadb/icingadb-objects.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -710,12 +710,12 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
710710
auto id (HashValue(new Array({m_EnvironmentId, actionUrl})));
711711

712712
if (runtimeUpdate || m_DumpedGlobals.ActionUrl.IsNew(id)) {
713-
actionUrls.emplace_back(std::move(id));
713+
actionUrls.emplace_back(id);
714714
Dictionary::Ptr data = new Dictionary({{"environment_id", m_EnvironmentId}, {"action_url", actionUrl}});
715715
actionUrls.emplace_back(JsonEncode(data));
716716

717717
if (runtimeUpdate) {
718-
AddObjectDataToRuntimeUpdates(runtimeUpdates, actionUrls.at(actionUrls.size() - 2u), m_PrefixConfigObject + "action:url", data);
718+
AddObjectDataToRuntimeUpdates(runtimeUpdates, id, m_PrefixConfigObject + "action:url", data);
719719
}
720720
}
721721
}
@@ -725,12 +725,12 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
725725
auto id (HashValue(new Array({m_EnvironmentId, notesUrl})));
726726

727727
if (runtimeUpdate || m_DumpedGlobals.NotesUrl.IsNew(id)) {
728-
notesUrls.emplace_back(std::move(id));
728+
notesUrls.emplace_back(id);
729729
Dictionary::Ptr data = new Dictionary({{"environment_id", m_EnvironmentId}, {"notes_url", notesUrl}});
730730
notesUrls.emplace_back(JsonEncode(data));
731731

732732
if (runtimeUpdate) {
733-
AddObjectDataToRuntimeUpdates(runtimeUpdates, notesUrls.at(notesUrls.size() - 2u), m_PrefixConfigObject + "notes:url", data);
733+
AddObjectDataToRuntimeUpdates(runtimeUpdates, id, m_PrefixConfigObject + "notes:url", data);
734734
}
735735
}
736736
}
@@ -740,12 +740,12 @@ void IcingaDB::InsertObjectDependencies(const ConfigObject::Ptr& object, const S
740740
auto id (HashValue(new Array({m_EnvironmentId, iconImage})));
741741

742742
if (runtimeUpdate || m_DumpedGlobals.IconImage.IsNew(id)) {
743-
iconImages.emplace_back(std::move(id));
743+
iconImages.emplace_back(id);
744744
Dictionary::Ptr data = new Dictionary({{"environment_id", m_EnvironmentId}, {"icon_image", iconImage}});
745745
iconImages.emplace_back(JsonEncode(data));
746746

747747
if (runtimeUpdate) {
748-
AddObjectDataToRuntimeUpdates(runtimeUpdates, iconImages.at(iconImages.size() - 2u), m_PrefixConfigObject + "icon:image", data);
748+
AddObjectDataToRuntimeUpdates(runtimeUpdates, id, m_PrefixConfigObject + "icon:image", data);
749749
}
750750
}
751751
}

lib/icingadb/redisconnection.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,14 @@ void LogQuery(RedisConnection::Query& query, Log& msg)
9999
break;
100100
}
101101

102-
if (arg.GetLength() > 64) {
103-
msg << " '" << arg.SubStr(0, 61) << "...'";
102+
if (auto str (std::get_if<String>(&arg)); str) {
103+
if (str->GetLength() > 64) {
104+
msg << " '" << str->SubStr(0, 61) << "...'";
105+
} else {
106+
msg << " '" << *str << '\'';
107+
}
104108
} else {
105-
msg << " '" << arg << '\'';
109+
msg << " '" << std::get<const char*>(arg) << '\'';
106110
}
107111
}
108112
}

lib/icingadb/redisconnection.hpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <set>
4040
#include <stdexcept>
4141
#include <utility>
42+
#include <variant>
4243
#include <vector>
4344

4445
namespace icinga
@@ -53,7 +54,7 @@ namespace icinga
5354
public:
5455
DECLARE_PTR_TYPEDEFS(RedisConnection);
5556

56-
typedef std::vector<String> Query;
57+
typedef std::vector<std::variant<const char*, String>> Query;
5758
typedef std::vector<Query> Queries;
5859
typedef Value Reply;
5960
typedef std::vector<Reply> Replies;
@@ -667,7 +668,12 @@ void RedisConnection::WriteRESP(AsyncWriteStream& stream, const Query& query, bo
667668
msg << "*" << query.size() << "\r\n";
668669

669670
for (auto& arg : query) {
670-
msg << "$" << arg.GetLength() << "\r\n" << arg << "\r\n";
671+
if (auto str (std::get_if<String>(&arg)); str) {
672+
msg << "$" << str->GetLength() << "\r\n" << *str << "\r\n";
673+
} else {
674+
auto cstr (std::get<const char*>(arg));
675+
msg << "$" << strlen(cstr) << "\r\n" << cstr << "\r\n";
676+
}
671677
}
672678

673679
asio::async_write(stream, writeBuffer, yc);

0 commit comments

Comments
 (0)