close
Skip to content

Change Redis query type from String to an optional std::string_view#10391

Merged
Al2Klimov merged 4 commits intomasterfrom
redis-query-variant
Mar 4, 2026
Merged

Change Redis query type from String to an optional std::string_view#10391
Al2Klimov merged 4 commits intomasterfrom
redis-query-variant

Conversation

@Al2Klimov
Copy link
Copy Markdown
Member

(Find TL;DR below)

Image

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

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

@julianbrost
Copy link
Copy Markdown
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
Comment thread lib/icingadb/redisconnection.cpp Outdated
Comment thread lib/icingadb/icingadb-objects.cpp Outdated
Comment thread lib/icingadb/redisconnection.hpp Outdated
Comment thread lib/icingadb/redisconnection.hpp
@Al2Klimov Al2Klimov force-pushed the redis-query-variant branch 2 times, most recently from 050665e to cfc5532 Compare April 7, 2025 13:01
@Al2Klimov
Copy link
Copy Markdown
Member Author

Fine, @yhabteab, you have won...

Copy link
Copy Markdown
Member

@yhabteab yhabteab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use an appropriate line wrapping for your commit description in d8a0d08 (like 80-120), so that git log can render it properly, isntead of soft wrap it at random location because my screen is too small for it. Please also fix the new compiler warnings introduced with this PR.

Comment thread lib/icingadb/icingadb-objects.cpp Outdated
Comment thread lib/icingadb/redisconnection.hpp Outdated
Comment thread lib/icingadb/redisconnection.hpp Outdated
@yhabteab yhabteab changed the title Change RedisConnection::Query::value_type from String to std::variant<const char*,String> Change Redis query type from String to an optional std::string_view Feb 23, 2026
@Al2Klimov
Copy link
Copy Markdown
Member Author

Yeah, stupid git-log(1) cuts off at a random character in the middle of the word. Only hard wrapping helps here. @lippserd said 72 for title, 80 for description.

@Al2Klimov Al2Klimov force-pushed the redis-query-variant branch from d8a0d08 to 3d4e853 Compare March 2, 2026 11:44
to ease changing that type in the future.
This expresses what kind of vector it is
and allows to easily change those types in the future.
Comment thread lib/icingadb/redisconnection.hpp Outdated
@Al2Klimov Al2Klimov force-pushed the redis-query-variant branch from e9213ef to 66993aa Compare March 2, 2026 14:54
yhabteab
yhabteab previously approved these changes Mar 2, 2026
Copy link
Copy Markdown
Member

@yhabteab yhabteab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LFTM now, but please do not merge it yet!

@yhabteab yhabteab requested a review from jschmidt-icinga March 2, 2026 15:00
Copy link
Copy Markdown
Contributor

@jschmidt-icinga jschmidt-icinga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change request is for the broken move constructor. But I want to at least hear a good explanation about why this has to derive from std::string_view.

Comment thread lib/icingadb/redisconnection.cpp Outdated
Comment thread lib/icingadb/redisconnection.hpp Outdated
@Al2Klimov Al2Klimov force-pushed the redis-query-variant branch 2 times, most recently from 1bb2e0f to 7fc6db6 Compare March 3, 2026 10:17
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 Al2Klimov force-pushed the redis-query-variant branch from 7fc6db6 to 1702eeb Compare March 3, 2026 10:29
Copy link
Copy Markdown
Contributor

@jschmidt-icinga jschmidt-icinga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QueryArg looks good to me now 👍. I'm happy that it actually ended up not adding complexity, maybe even made the PR a bit simpler.

I haven't looked in detail at where it's used and why, but I trust @yhabteab's review on that.

@yhabteab yhabteab added this to the 2.16.0 milestone Mar 4, 2026
@Al2Klimov Al2Klimov merged commit 55d35b1 into master Mar 4, 2026
28 checks passed
@Al2Klimov Al2Klimov deleted the redis-query-variant branch March 4, 2026 09:38
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)

4 participants