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

Code before:
        }

        /* Otherwise, do a match against all items in the dictionary. */
        di = dictGetIterator(configs);

        while ((de = dictNext(di)) != NULL) {
            standardConfig *config = dictGetVal(de);
            /* Note that hidden configs require an exact match (not a pattern) */
            if (config->flags & HIDDEN_CONFIG) continue;
            if (dictFind(matches, config->name)) continue;
            if (stringmatch(name, dictGetKey(de), 1)) {
                dictAdd(matches, dictGetKey(de), config);
            }
        }
        dictReleaseIterator(di);
    }

    di = dictGetIterator(matches);
    unsigned long matchesSize = dictSize(matches);
    addReplyMapLen(c, matchesSize);

    struct {
        const char *key;
        sds value;
    } *sorted = zmalloc(sizeof(*sorted) * matchesSize);
    unsigned long sortedIndex = 0;
    while ((de = dictNext(di)) != NULL) {
        standardConfig *config = (standardConfig *)dictGetVal(de);
        sorted[sortedIndex].key = dictGetKey(de);
        sorted[sortedIndex].value = config->interface.get(config);
        sortedIndex++;
    }
    qsort(sorted, matchesSize, sizeof(*sorted), configKeyCompare);

    for (sortedIndex = 0; sortedIndex < matchesSize; sortedIndex++) {
        addReplyBulkCString(c, sorted[sortedIndex].key);
        addReplyBulkSds(c, sorted[sortedIndex].value);
    }
    zfree(sorted);
    dictReleaseIterator(di);
    dictRelease(matches);
}

/*-----------------------------------------------------------------------------
 * CONFIG REWRITE implementation
 *----------------------------------------------------------------------------*/

#define CONFIG_REWRITE_SIGNATURE "# Generated by CONFIG REWRITE"

/* Sentinel config rewriting is implemented inside sentinel.c by
 * rewriteConfigSentinelOption(). */

Review comment (suggestion):
For local variables, we normally use snake case like `sorted_index` or just short names like `i`. Since the scope is pretty small here, I don't mind that we reuse the `i` variable already declared in this function.

Also matchesSize could be named `matches_size`, `num_matches` or just `n`.

If we use `i` and `n`, then the for loop below will be the typical short C style `for (i = 0; i < n; i++)`. I kind of like it. (Of course this brief style only works in simple functions without risk of confusion, so here it works well IMHO.)

Code after:
        }

        /* Otherwise, do a match against all items in the dictionary. */
        di = dictGetIterator(configs);

        while ((de = dictNext(di)) != NULL) {
            standardConfig *config = dictGetVal(de);
            /* Note that hidden configs require an exact match (not a pattern) */
            if (config->flags & HIDDEN_CONFIG) continue;
            if (dictFind(matches, config->name)) continue;
            if (stringmatch(name, dictGetKey(de), 1)) {
                dictAdd(matches, dictGetKey(de), config);
            }
        }
        dictReleaseIterator(di);
    }

    di = dictGetIterator(matches);
    int n = dictSize(matches);
    addReplyMapLen(c, n);

    struct {
        const char *key;
        sds value;
    } *sorted = zmalloc(sizeof(*sorted) * n);

    i = 0;
    while ((de = dictNext(di)) != NULL) {
        standardConfig *config = (standardConfig *)dictGetVal(de);
        sorted[i].key = dictGetKey(de);
        sorted[i].value = config->interface.get(config);
        i++;
    }
    dictReleaseIterator(di);
    dictRelease(matches);

    qsort(sorted, n, sizeof(*sorted), configKeyCompare);
    for (i = 0; i < n; i++) {
        addReplyBulkCString(c, sorted[i].key);
        addReplyBulkSds(c, sorted[i].value);
    }
    zfree(sorted);
}

/*-----------------------------------------------------------------------------
 * CONFIG REWRITE implementation
 *----------------------------------------------------------------------------*/

#define CONFIG_REWRITE_SIGNATURE "# Generated by CONFIG REWRITE"

/* Sentinel config rewriting is implemented inside sentinel.c by