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

Reduce redundant call of prepareClientToWrite when call addReply* continuously. #670

Merged
merged 2 commits into from
Jun 25, 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
Next Next commit
Reduce redundant call of prepareClientToWrite when call addReply* con…
…tinuously.

Signed-off-by: Lipeng Zhu <[email protected]>
Co-authored-by: Wangyang Guo <[email protected]>
  • Loading branch information
lipzhu and guowangy committed Jun 22, 2024
commit b7b83bd915d2e57beda41a196516676686e01aa7
35 changes: 20 additions & 15 deletions src/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ void addReplyHumanLongDouble(client *c, long double d) {

/* Add a long long as integer reply or bulk len / multi bulk count.
* Basically this is used to output <prefix><long long><crlf>. */
void addReplyLongLongWithPrefix(client *c, long long ll, char prefix) {
void _addReplyLongLongWithPrefix(client *c, long long ll, char prefix) {
madolson marked this conversation as resolved.
Show resolved Hide resolved
char buf[128];
int len;

Expand All @@ -972,38 +972,41 @@ void addReplyLongLongWithPrefix(client *c, long long ll, char prefix) {
const int opt_hdr = ll < OBJ_SHARED_BULKHDR_LEN && ll >= 0;
const size_t hdr_len = OBJ_SHARED_HDR_STRLEN(ll);
if (prefix == '*' && opt_hdr) {
addReplyProto(c, shared.mbulkhdr[ll]->ptr, hdr_len);
_addReplyToBufferOrList(c, shared.mbulkhdr[ll]->ptr, hdr_len);
return;
} else if (prefix == '$' && opt_hdr) {
addReplyProto(c, shared.bulkhdr[ll]->ptr, hdr_len);
_addReplyToBufferOrList(c, shared.bulkhdr[ll]->ptr, hdr_len);
return;
} else if (prefix == '%' && opt_hdr) {
addReplyProto(c, shared.maphdr[ll]->ptr, hdr_len);
_addReplyToBufferOrList(c, shared.maphdr[ll]->ptr, hdr_len);
return;
} else if (prefix == '~' && opt_hdr) {
addReplyProto(c, shared.sethdr[ll]->ptr, hdr_len);
_addReplyToBufferOrList(c, shared.sethdr[ll]->ptr, hdr_len);
return;
}

buf[0] = prefix;
len = ll2string(buf + 1, sizeof(buf) - 1, ll);
buf[len + 1] = '\r';
buf[len + 2] = '\n';
addReplyProto(c, buf, len + 3);
_addReplyToBufferOrList(c, buf, len + 3);
}

void addReplyLongLong(client *c, long long ll) {
if (ll == 0)
addReply(c, shared.czero);
else if (ll == 1)
addReply(c, shared.cone);
else
addReplyLongLongWithPrefix(c, ll, ':');
else {
if (prepareClientToWrite(c) != C_OK) return;
_addReplyLongLongWithPrefix(c, ll, ':');
}
}

void addReplyAggregateLen(client *c, long length, int prefix) {
serverAssert(length >= 0);
addReplyLongLongWithPrefix(c, length, prefix);
if (prepareClientToWrite(c) != C_OK) return;
_addReplyLongLongWithPrefix(c, length, prefix);
}

void addReplyArrayLen(client *c, long length) {
Expand Down Expand Up @@ -1063,8 +1066,8 @@ void addReplyNullArray(client *c) {
/* Create the length prefix of a bulk reply, example: $2234 */
void addReplyBulkLen(client *c, robj *obj) {
size_t len = stringObjectLen(obj);

addReplyLongLongWithPrefix(c, len, '$');
if (prepareClientToWrite(c) != C_OK) return;
_addReplyLongLongWithPrefix(c, len, '$');
}

/* Add an Object as a bulk reply */
Expand All @@ -1076,14 +1079,16 @@ void addReplyBulk(client *c, robj *obj) {

/* Add a C buffer as bulk reply */
void addReplyBulkCBuffer(client *c, const void *p, size_t len) {
addReplyLongLongWithPrefix(c, len, '$');
addReplyProto(c, p, len);
addReplyProto(c, "\r\n", 2);
if (prepareClientToWrite(c) != C_OK) return;
_addReplyLongLongWithPrefix(c, len, '$');
_addReplyToBufferOrList(c, p, len);
_addReplyToBufferOrList(c, "\r\n", 2);
}

/* Add sds to reply (takes ownership of sds and frees it) */
void addReplyBulkSds(client *c, sds s) {
addReplyLongLongWithPrefix(c, sdslen(s), '$');
if (prepareClientToWrite(c) != C_OK) return;
_addReplyLongLongWithPrefix(c, sdslen(s), '$');
madolson marked this conversation as resolved.
Show resolved Hide resolved
addReplySds(c, s);
addReplyProto(c, "\r\n", 2);
}
Expand Down
1 change: 0 additions & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,6 @@ void addReplyErrorArity(client *c);
void addReplyErrorExpireTime(client *c);
void addReplyStatus(client *c, const char *status);
void addReplyDouble(client *c, double d);
void addReplyLongLongWithPrefix(client *c, long long ll, char prefix);
void addReplyBigNum(client *c, const char *num, size_t len);
void addReplyHumanLongDouble(client *c, long double d);
void addReplyLongLong(client *c, long long ll);
Expand Down
Loading