Skip to content

Comments

Change RedisConnection::Query::value_type from String to std::variant<const char*,String>#10391

Open
Al2Klimov wants to merge 4 commits intomasterfrom
redis-query-variant
Open

Change RedisConnection::Query::value_type from String to std::variant<const char*,String>#10391
Al2Klimov wants to merge 4 commits intomasterfrom
redis-query-variant

Conversation

@Al2Klimov
Copy link
Member

(Find TL;DR below)

Seriously speaking, 70ca88a pollutes the diff and should be viewed on its own. It just replaces std::vector<String> with RedisConnection::Query. As RedisConnection::Query already was a std::vector<String>, this is a no-op on its own. But it allows to just change RedisConnection::Query itself and affect all its usages at once.

Why do we have to change String to std::variant<const char*,String> at all? (efe5f22)

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 malloc(3)s and copies these data every time. In contrast, std::variant<const char*,String> just stores the address if any.

TL;DR

  • The JSON RPC message processing involves Icinga DB handlers
  • The Icinga DB handlers assemble Redis messages, std::vector<T>
  • The faster T is, the better for JSON RPC message processing
  • The fastest possible T is const char*, if any, which is just a pointer

fixes #10276
ref/NC/820479

@Al2Klimov Al2Klimov added area/icingadb New backend core/quality Improve code, libraries, algorithms, inline docs ref/NC labels Mar 25, 2025
@Al2Klimov Al2Klimov requested a review from lippserd March 25, 2025 16:58
@cla-bot cla-bot bot added the cla/signed label Mar 25, 2025
@Al2Klimov
Copy link
Member Author

Strange, it worked on my machine...

@Al2Klimov Al2Klimov marked this pull request as draft March 25, 2025 17:07
@Al2Klimov Al2Klimov removed the request for review from lippserd March 25, 2025 17:07
@julianbrost
Copy link
Member

The idea looks fine in general. Besides getting it to work, you might want to take a look at std::string_view. If you implement a conversion to it once, that should allow to avoid other case-distinctions (as the purpose of string_view is to abstract how exactly the backing string is stored).

@Al2Klimov
Copy link
Member Author

Like, class TODO : public std::variant<const char*,String> { operator std::string_view(); };?

@julianbrost
Copy link
Member

Probably something like that. A function for it might work too, but a small class could be nicer overall.

@Al2Klimov Al2Klimov force-pushed the redis-query-variant branch from ab1d887 to 7a5d6f3 Compare March 31, 2025 10:53
@Al2Klimov Al2Klimov marked this pull request as ready for review March 31, 2025 10:53
@Al2Klimov Al2Klimov force-pushed the redis-query-variant branch 2 times, most recently from 050665e to cfc5532 Compare April 7, 2025 13:01
to ease changing that type in the future.
…d::vector)

This expresses what kind of vector it is and allows to easily change those types in the future.
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 malloc(3)s and copies these data every time. In contrast, the new type just stores the address if any. (Actually, const char* is wrapped by std::string_view to not compute its length every time.)
@Al2Klimov
Copy link
Member Author

Fine, @yhabteab, you have won...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/icingadb New backend cla/signed core/quality Improve code, libraries, algorithms, inline docs ref/NC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Icinga DB Boost.Signal2 handlers: reduce malloc(3)

3 participants