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

Code before:
                if (start == -1) start = j;
            }
            if (start != -1 && (!bit || j == CLUSTER_SLOTS - 1)) {
                if (bit && j == CLUSTER_SLOTS - 1) j++;

                if (start == j - 1) {
                    ci = sdscatfmt(ci, " %i", start);
                } else {
                    ci = sdscatfmt(ci, " %i-%i", start, j - 1);
                }
                start = -1;
            }
        }
    }

    /* Just for MYSELF node we also dump info about slots that
     * we are migrating to other instances or importing from other
     * instances. */
    if (node->flags & CLUSTER_NODE_MYSELF) {
        char nodename[CLUSTER_NAMELEN + 1];

        for (j = 0; j < CLUSTER_SLOTS; j++) {
            if (server.cluster->migrating_slots_to[j]) {
                memcpy(nodename, server.cluster->migrating_slots_to[j]->name, CLUSTER_NAMELEN);
                nodename[CLUSTER_NAMELEN] = '\0';
                ci = sdscatfmt(ci, " [%i->-%s]", j, nodename);
            } else if (server.cluster->importing_slots_from[j]) {
                memcpy(nodename, server.cluster->importing_slots_from[j]->name, CLUSTER_NAMELEN);
                nodename[CLUSTER_NAMELEN] = '\0';
                ci = sdscatfmt(ci, " [%i-<-%s]", j, nodename);
            }
        }
    }
    return ci;
}

/* Generate the slot topology for all nodes and store the slot range information
 * in the slot_info_pairs array on the node. This is used to improve the efficiency
 * of clusterGenNodesDescription() because it removes looping of the slot space
 * for generating the slot info for each node individually. */
void clusterGenNodesSlotsInfo(int filter) {
    clusterNode *n = NULL;
    int start = -1;

    for (int i = 0; i <= CLUSTER_SLOTS; i++) {
        /* Find start node and slot id. */
        if (n == NULL) {
            if (i == CLUSTER_SLOTS) break;
            n = server.cluster->slots[i];
            start = i;
            continue;

Review comment (suggestion):
For these node names, I'm thinking we can maybe do something smarter to avoid copying the node name twice.

Disclaimer: untested :smile:

```suggestion
                ci = sdscatfmt(ci, " [%i->-", j);
                ci = sdscatlen(ci, server.cluster->migrating_slots_to[j]->name, CLUSTER_NAMELEN);
                ci = sdscat(ci, "]");
```

Code after:
                if (start == -1) start = j;
            }
            if (start != -1 && (!bit || j == CLUSTER_SLOTS - 1)) {
                if (bit && j == CLUSTER_SLOTS - 1) j++;

                if (start == j - 1) {
                    ci = sdscatfmt(ci, " %i", start);
                } else {
                    ci = sdscatfmt(ci, " %i-%i", start, j - 1);
                }
                start = -1;
            }
        }
    }

    /* Just for MYSELF node we also dump info about slots that
     * we are migrating to other instances or importing from other
     * instances. */
    if (node->flags & CLUSTER_NODE_MYSELF) {
        for (j = 0; j < CLUSTER_SLOTS; j++) {
            if (server.cluster->migrating_slots_to[j]) {
                ci = sdscatfmt(ci, " [%i->-", j);
                ci = sdscatlen(ci, server.cluster->migrating_slots_to[j]->name, CLUSTER_NAMELEN);
                ci = sdscat(ci, "]");
            } else if (server.cluster->importing_slots_from[j]) {
                ci = sdscatfmt(ci, " [%i-<-", j);
                ci = sdscatlen(ci, server.cluster->importing_slots_from[j]->name, CLUSTER_NAMELEN);
                ci = sdscat(ci, "]");
            }
        }
    }
    return ci;
}

/* Generate the slot topology for all nodes and store the slot range information
 * in the slot_info_pairs array on the node. This is used to improve the efficiency
 * of clusterGenNodesDescription() because it removes looping of the slot space
 * for generating the slot info for each node individually. */
void clusterGenNodesSlotsInfo(int filter) {
    clusterNode *n = NULL;
    int start = -1;

    for (int i = 0; i <= CLUSTER_SLOTS; i++) {
        /* Find start node and slot id. */
        if (n == NULL) {
            if (i == CLUSTER_SLOTS) break;
            n = server.cluster->slots[i];
            start = i;
            continue;
        }