summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-05-05 22:14:23 +0200
committerantirez <antirez@gmail.com>2014-05-07 16:12:32 +0200
commit0ef4f44c5a6b8d8900444c852baff223c647d5cb (patch)
tree069c1e01b60ac8dee8fef1240ca393e75b50fc70
parent8226be61ecb4e35a4e4f6845aa13eebd9c182b65 (diff)
downloadredis-0ef4f44c5a6b8d8900444c852baff223c647d5cb.tar.gz
Scripting: luaRedisGenericCommand() fast path for buffer-only replies.
When the reply is only contained in the client static output buffer, use a fast path avoiding the dynamic allocation of an SDS string to concatenate the client reply objects.
-rw-r--r--src/scripting.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/scripting.c b/src/scripting.c
index 1202c5c2f..bd42f5369 100644
--- a/src/scripting.c
+++ b/src/scripting.c
@@ -306,13 +306,21 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
/* Convert the result of the Redis command into a suitable Lua type.
* The first thing we need is to create a single string from the client
* output buffers. */
- reply = sdsnewlen(c->buf,c->bufpos);
- c->bufpos = 0;
- while(listLength(c->reply)) {
- robj *o = listNodeValue(listFirst(c->reply));
+ if (listLength(c->reply) == 0 && c->bufpos < REDIS_REPLY_CHUNK_BYTES) {
+ /* This is a fast path for the common case of a reply inside the
+ * client static buffer. Don't create an SDS string but just use
+ * the client buffer directly. */
+ c->buf[c->bufpos] = '\0';
+ reply = c->buf;
+ } else {
+ reply = sdsnewlen(c->buf,c->bufpos);
+ c->bufpos = 0;
+ while(listLength(c->reply)) {
+ robj *o = listNodeValue(listFirst(c->reply));
- reply = sdscatlen(reply,o->ptr,sdslen(o->ptr));
- listDelNode(c->reply,listFirst(c->reply));
+ reply = sdscatlen(reply,o->ptr,sdslen(o->ptr));
+ listDelNode(c->reply,listFirst(c->reply));
+ }
}
if (raise_error && reply[0] != '-') raise_error = 0;
redisProtocolToLuaType(lua,reply);
@@ -322,7 +330,7 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
(reply[0] == '*' && reply[1] != '-')) {
luaSortArray(lua);
}
- sdsfree(reply);
+ if (reply != c->buf) sdsfree(reply);
c->reply_bytes = 0;
cleanup: