redis-plus-plus
redis-plus-plus copied to clipboard
Remove extra memory allocation in parse()
When using redis-plus-plus to send large messages, I noticed during memory profiling with valgrind massif that a heap allocation occurs when then redisReply
object is transformed into a std::string
(i.e. return std::string(reply.str, reply.len);
). Is there anyway to restructure this function or API slightly to eliminate this extra copy? This would really help manage peak memory utilization when sending large messages through redis-plus-plus.
Hi @mellis13
First of all, it's not a good idea to get/set large message from/to Redis. It's bad for Redis performance.
Since we need to convert a C char-array to std::string
, we have to do the copy. If you want to avoid it, you can use one of the Redis::command
override method to get the redisReply
object:
auto redis = Redis("tcp://127.0.0.1");
auto r = redis.command("get", "key");
if (reply::is_nil(*r)) {
// key doesn't exist
} else {
if (reply::is_string(*r) && r->str != nullptr) {
// process the value with `r->str`, and `r->len`
} else {
throw exception;
}
}
It's much more complicate than a simple Redis::get
, but you can avoid the copy.
If you still have any problem, feel free to let me know.
Regards
std::string_view
from C++17?
@BratSinot Good try! However, we cannot return std::string_view
from a redis command, such as Redis::get
. Because std::string_view
doesn't own the underlying memory, and once the command returns, the underlying memory will be released, and make the std::string_view
object invalid.
Regards