Repository: valkey-io/valkey
File: src/server.c (C)

Code before:
}

/* Collect all output from a caching client (both buffer and reply list) */
static sds collectCachedResponse(client *c) {
    sds response = sdsempty();
    
    /* First, collect from the fixed buffer if any */
    if (c->bufpos > 0) {
        response = sdscatlen(response, c->buf, c->bufpos);
    }
    
    /* Then, collect from the reply list */
    listIter li;
    listNode *ln;
    clientReplyBlock *val_block;
    listRewind(c->reply, &li);
    while ((ln = listNext(&li)) != NULL) {
        val_block = (clientReplyBlock *)listNodeValue(ln);
        response = sdscatlen(response, val_block->buf, val_block->used);
    }
    
    return response;
}

/* Forward declaration */
void addReplyCommandInfo(client *c, struct serverCommand *cmd);

/* Generate and cache the command info response for a given protocol version */
static void cacheCommandInfo(struct serverCommand *cmd, int resp) {
    client *caching_client = createCachedResponseClient(resp);
    
    int firstkey = 0, lastkey = 0, keystep = 0;
    if (cmd->legacy_range_key_spec.begin_search_type != KSPEC_BS_INVALID) {
        firstkey = cmd->legacy_range_key_spec.bs.index.pos;
        lastkey = cmd->legacy_range_key_spec.fk.range.lastkey;
        if (lastkey >= 0) lastkey += firstkey;
        keystep = cmd->legacy_range_key_spec.fk.range.keystep;
    }

    addReplyArrayLen(caching_client, 10);
    addReplyBulkCBuffer(caching_client, cmd->fullname, sdslen(cmd->fullname));
    addReplyLongLong(caching_client, cmd->arity);
    addReplyFlagsForCommand(caching_client, cmd);
    addReplyLongLong(caching_client, firstkey);
    addReplyLongLong(caching_client, lastkey);
    addReplyLongLong(caching_client, keystep);
    addReplyCommandCategories(caching_client, cmd);
    addReplyCommandTips(caching_client, cmd);
    addReplyCommandKeySpecs(caching_client, cmd);
    addReplyCommandSubCommands(caching_client, cmd, addReplyCommandInfo, 0);


Review comment (performance):
Just move `cacheCommandInfo` after `addReplyCommandInfo`, avoiding the need for a forward declaration.

Code after:
    if (!is_equal)
        serverLog(LL_WARNING, "\ngenerated_response:\n%s\n\ncached_response:\n%s", generated_response, cached_response);
    sdsfree(generated_response);
    return is_equal;
}

/* Output the representation of a server command. Used by the COMMAND command and COMMAND INFO. */
void addReplyCommandInfo(client *c, struct serverCommand *cmd) {
    if (!cmd) {
        addReplyNull(c);
    } else {
        /* Use cached response if available for the client's protocol version */
        int cache_idx = RESP_CACHE_INDEX(c->resp);
        sds cache = cmd->info_cache[cache_idx];

        if (cache == NULL) {
            cache = generateCommandInfoResponse(cmd, c->resp);
            cmd->info_cache[cache_idx] = cache;
        } else {
            debugServerAssertWithInfo(c, NULL, verifyCachedCommandInfoResponse(cmd, cache, c->resp));
        }

        addReplyProto(c, cache, sdslen(cache));
    }
}

/* Output the representation of a server command. Used by the COMMAND DOCS. */
void addReplyCommandDocs(client *c, struct serverCommand *cmd) {
    /* Count our reply len so we don't have to use deferred reply. */
    long maplen = 1;
    if (cmd->summary) maplen++;
    if (cmd->since) maplen++;
    if (cmd->flags & CMD_MODULE) maplen++;
    if (cmd->complexity) maplen++;
    if (cmd->doc_flags) maplen++;
    if (cmd->deprecated_since) maplen++;
    if (cmd->replaced_by) maplen++;
    if (cmd->history) maplen++;
#ifdef LOG_REQ_RES
    if (cmd->reply_schema) maplen++;
#endif
    if (cmd->args) maplen++;
    if (cmd->subcommands_ht) maplen++;
    addReplyMapLen(c, maplen);

    if (cmd->summary) {
        addReplyBulkCString(c, "summary");
        addReplyBulkCString(c, cmd->summary);
    }
    if (cmd->since) {
        addReplyBulkCString(c, "since");