Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move prepareClientToWrite out of loop for lrange command to reduce the redundant call. #860

Merged
merged 8 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,11 @@ void addReplyArrayLen(client *c, long length) {
addReplyAggregateLen(c, length, '*');
}

void addReplyArrayLenSkipPrepareClient(client *c, long length) {
serverAssert(length >= 0);
_addReplyLongLongWithPrefix(c, length, '*');
}

void addReplyMapLen(client *c, long length) {
int prefix = c->resp == 2 ? '*' : '%';
if (c->resp == 2) length *= 2;
Expand Down Expand Up @@ -1104,6 +1109,10 @@ void addReplyBulk(client *c, robj *obj) {
/* Add a C buffer as bulk reply */
void addReplyBulkCBuffer(client *c, const void *p, size_t len) {
if (prepareClientToWrite(c) != C_OK) return;
addReplyBulkCBufferSkipPrepareClient(c, p, len);
}

void addReplyBulkCBufferSkipPrepareClient(client *c, const void *p, size_t len) {
_addReplyLongLongWithPrefix(c, len, '$');
_addReplyToBufferOrList(c, p, len);
_addReplyToBufferOrList(c, "\r\n", 2);
Expand Down Expand Up @@ -1147,6 +1156,14 @@ void addReplyBulkLongLong(client *c, long long ll) {
addReplyBulkCBuffer(c, buf, len);
}

void addReplyBulkLongLongSkipPrepareClient(client *c, long long ll) {
lipzhu marked this conversation as resolved.
Show resolved Hide resolved
char buf[64];
int len;

len = ll2string(buf, 64, ll);
addReplyBulkCBufferSkipPrepareClient(c, buf, len);
}

/* Reply with a verbatim type having the specified extension.
*
* The 'ext' is the "extension" of the file, actually just a three
Expand Down
3 changes: 3 additions & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2774,7 +2774,9 @@ void AddReplyFromClient(client *c, client *src);
void addReplyBulk(client *c, robj *obj);
void addReplyBulkCString(client *c, const char *s);
void addReplyBulkCBuffer(client *c, const void *p, size_t len);
void addReplyBulkCBufferSkipPrepareClient(client *c, const void *p, size_t len);
void addReplyBulkLongLong(client *c, long long ll);
void addReplyBulkLongLongSkipPrepareClient(client *c, long long ll);
void addReply(client *c, robj *obj);
void addReplyStatusLength(client *c, const char *s, size_t len);
void addReplySds(client *c, sds s);
Expand All @@ -2796,6 +2798,7 @@ void addReplyBigNum(client *c, const char *num, size_t len);
void addReplyHumanLongDouble(client *c, long double d);
void addReplyLongLong(client *c, long long ll);
void addReplyArrayLen(client *c, long length);
void addReplyArrayLenSkipPrepareClient(client *c, long length);
lipzhu marked this conversation as resolved.
Show resolved Hide resolved
void addReplyMapLen(client *c, long length);
void addReplySetLen(client *c, long length);
void addReplyAttributeLen(client *c, long length);
Expand Down
15 changes: 8 additions & 7 deletions src/t_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ void listPopRangeAndReplyWithKey(client *c, robj *o, robj *key, int where, long
* Note that the purpose is to make the methods small so that the
* code in the loop can be inlined better to improve performance. */
void addListQuicklistRangeReply(client *c, robj *o, int from, int rangelen, int reverse) {
if (prepareClientToWrite(c) != C_OK) return;
/* Return the result in form of a multi-bulk reply */
addReplyArrayLen(c, rangelen);

Expand All @@ -659,9 +660,9 @@ void addListQuicklistRangeReply(client *c, robj *o, int from, int rangelen, int
quicklistEntry qe;
serverAssert(quicklistNext(iter, &qe)); /* fail on corrupt data */
if (qe.value) {
addReplyBulkCBuffer(c, qe.value, qe.sz);
addReplyBulkCBufferSkipPrepareClient(c, qe.value, qe.sz);
} else {
addReplyBulkLongLong(c, qe.longval);
addReplyBulkLongLongSkipPrepareClient(c, qe.longval);
}
}
quicklistReleaseIterator(iter);
Expand All @@ -671,21 +672,21 @@ void addListQuicklistRangeReply(client *c, robj *o, int from, int rangelen, int
* Note that the purpose is to make the methods small so that the
* code in the loop can be inlined better to improve performance. */
void addListListpackRangeReply(client *c, robj *o, int from, int rangelen, int reverse) {
if (prepareClientToWrite(c) != C_OK) return;
/* Return the result in form of a multi-bulk reply */
addReplyArrayLen(c, rangelen);
unsigned char *p = lpSeek(o->ptr, from);
unsigned char *vstr;
unsigned int vlen;
long long lval;

/* Return the result in form of a multi-bulk reply */
addReplyArrayLen(c, rangelen);

while (rangelen--) {
serverAssert(p); /* fail on corrupt data */
vstr = lpGetValue(p, &vlen, &lval);
if (vstr) {
addReplyBulkCBuffer(c, vstr, vlen);
addReplyBulkCBufferSkipPrepareClient(c, vstr, vlen);
} else {
addReplyBulkLongLong(c, lval);
addReplyBulkLongLongSkipPrepareClient(c, lval);
}
p = reverse ? lpPrev(o->ptr, p) : lpNext(o->ptr, p);
}
Expand Down
Loading