提交 b1f3bf88 编写于 作者: A antirez

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.
上级 473a8bd7
...@@ -306,13 +306,21 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) { ...@@ -306,13 +306,21 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
/* Convert the result of the Redis command into a suitable Lua type. /* 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 * The first thing we need is to create a single string from the client
* output buffers. */ * output buffers. */
reply = sdsnewlen(c->buf,c->bufpos); if (listLength(c->reply) == 0 && c->bufpos < REDIS_REPLY_CHUNK_BYTES) {
c->bufpos = 0; /* This is a fast path for the common case of a reply inside the
while(listLength(c->reply)) { * client static buffer. Don't create an SDS string but just use
robj *o = listNodeValue(listFirst(c->reply)); * 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)); reply = sdscatlen(reply,o->ptr,sdslen(o->ptr));
listDelNode(c->reply,listFirst(c->reply)); listDelNode(c->reply,listFirst(c->reply));
}
} }
if (raise_error && reply[0] != '-') raise_error = 0; if (raise_error && reply[0] != '-') raise_error = 0;
redisProtocolToLuaType(lua,reply); redisProtocolToLuaType(lua,reply);
...@@ -322,7 +330,7 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) { ...@@ -322,7 +330,7 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
(reply[0] == '*' && reply[1] != '-')) { (reply[0] == '*' && reply[1] != '-')) {
luaSortArray(lua); luaSortArray(lua);
} }
sdsfree(reply); if (reply != c->buf) sdsfree(reply);
c->reply_bytes = 0; c->reply_bytes = 0;
cleanup: cleanup:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册