reducing condition checks in _addReplyToBuffer, inlining it, and avoid entering it when there are there already entries in the reply list

This commit is contained in:
fcostaoliveira 2024-11-14 00:48:38 +00:00
parent 09f6680ba0
commit 048bfe4eda
1 changed files with 11 additions and 15 deletions

View File

@ -317,27 +317,19 @@ int prepareClientToWrite(client *c) {
* Low level functions to add more data to output buffers. * Low level functions to add more data to output buffers.
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
/* Attempts to add the reply to the static buffer in the client struct. /* Adds reply to the static buffer in the client struct.
* Returns the length of data that is added to the reply buffer.
* *
* Sanitizer suppression: client->buf_usable_size determined by * Sanitizer suppression: client->buf_usable_size determined by
* zmalloc_usable_size() call. Writing beyond client->buf boundaries confuses * zmalloc_usable_size() call. Writing beyond client->buf boundaries confuses
* sanitizer and generates a false positive out-of-bounds error */ * sanitizer and generates a false positive out-of-bounds error */
REDIS_NO_SANITIZE("bounds") REDIS_NO_SANITIZE("bounds")
size_t _addReplyToBuffer(client *c, const char *s, size_t len) { static inline void _addReplyToBuffer(client *c, const char *s, size_t len) {
size_t available = c->buf_usable_size - c->bufpos; const size_t available = c->buf_usable_size - c->bufpos;
const size_t reply_len = len > available ? available : len;
/* If there already are entries in the reply list, we cannot
* add anything more to the static buffer. */
if (listLength(c->reply) > 0) return 0;
size_t reply_len = len > available ? available : len;
memcpy(c->buf+c->bufpos,s,reply_len); memcpy(c->buf+c->bufpos,s,reply_len);
c->bufpos+=reply_len; c->bufpos+=reply_len;
/* We update the buffer peak after appending the reply to the buffer */ /* We update the buffer peak after appending the reply to the buffer */
if(c->buf_peak < (size_t)c->bufpos) c->buf_peak = max(c->buf_peak,(size_t)c->bufpos);
c->buf_peak = (size_t)c->bufpos;
return reply_len;
} }
/* Adds the reply to the reply linked list. /* Adds the reply to the reply linked list.
@ -419,8 +411,12 @@ void _addReplyToBufferOrList(client *c, const char *s, size_t len) {
return; return;
} }
size_t reply_len = _addReplyToBuffer(c,s,len); /* If there already are entries in the reply list, we cannot
if (len > reply_len) _addReplyProtoToList(c,c->reply,s+reply_len,len-reply_len); * add anything more to the static buffer. */
if (listLength(c->reply) > 0)
_addReplyProtoToList(c,c->reply,s,len);
else
_addReplyToBuffer(c,s,len);
} }
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------