From 38b2e65e15b5be11ebefbaedeb761ece2066c747 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 27 Aug 2013 11:59:34 +0200 Subject: [PATCH] tryObjectEncoding(): optimize sds strings if possible. When no encoding is possible, at least try to reallocate the sds string with one that does not waste memory (with free space at the end of the buffer) when the string is large enough. --- src/object.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/object.c b/src/object.c index 75a4287b..b3acd2cc 100644 --- a/src/object.c +++ b/src/object.c @@ -363,7 +363,22 @@ robj *tryObjectEncoding(robj *o) { decrRefCount(o); return emb; } else { - /* Otherwise return the original object. */ + /* We can't encode the object... + * + * Do the last try, and at least optimize the SDS string inside + * the string object to require little space, in case there + * is more than 10% of free space at the end of the SDS string. + * + * We do that only for relatively large strings as this branch + * is only entered if the length of the string is greater than + * REDIS_ENCODING_EMBSTR_SIZE_LIMIT. */ + if (len > 64 && + o->encoding == REDIS_ENCODING_RAW && + sdsavail(s) > len/10) + { + o->ptr = sdsRemoveFreeSpace(o->ptr); + } + /* Return the original object. */ return o; } }