redis-plus-plus icon indicating copy to clipboard operation
redis-plus-plus copied to clipboard

Remove extra memory allocation in parse()

Open mellis13 opened this issue 4 years ago • 3 comments

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.

mellis13 avatar Apr 02 '20 16:04 mellis13

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

sewenew avatar Apr 03 '20 16:04 sewenew

std::string_view from C++17?

BratSinot avatar Nov 05 '20 02:11 BratSinot

@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

sewenew avatar Nov 05 '20 14:11 sewenew