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

Code before:

                /* For each consumer we also need to add the basic data
                 * structures and the PEL memory usage. */
                raxIterator cri;
                raxStart(&cri, cg->consumers);
                raxSeek(&cri, "^", NULL, 0);
                size_t inner_samples = 0;
                size_t inner_elesize = 0;
                while (inner_samples < sample_size && raxNext(&cri)) {
                    streamConsumer *consumer = cri.data;
                    inner_elesize += sizeof(*consumer);
                    inner_elesize += sdslen(consumer->name);
                    inner_elesize += raxAllocSize(consumer->pel);
                    /* Don't count NACKs again, they are shared with the
                     * consumer group PEL. */
                    inner_samples++;
                }
                raxStop(&cri);
                if (inner_samples) elesize += (double)inner_elesize / inner_samples * raxSize(cg->consumers);
                samples++;
            }
            raxStop(&ri);
            if (samples) asize += (double)elesize / samples * raxSize(s->cgroups);
        }
    } else if (o->type == OBJ_MODULE) {
        asize = zmalloc_size((void *)o) + moduleGetMemUsage(key, o, sample_size, dbid);
    } else {
        serverPanic("Unknown object type");
    }
    return asize;
}

/* Release data obtained with getMemoryOverheadData(). */
void freeMemoryOverheadData(struct serverMemOverhead *mh) {
    zfree(mh->db);
    zfree(mh);
}

/* Return a struct serverMemOverhead filled with memory overhead
 * information used for the MEMORY OVERHEAD and INFO command. The returned
 * structure pointer should be freed calling freeMemoryOverheadData(). */
struct serverMemOverhead *getMemoryOverheadData(void) {
    int j;
    size_t mem_total = 0;
    size_t mem = 0;
    size_t zmalloc_used = zmalloc_used_memory();
    struct serverMemOverhead *mh = zcalloc(sizeof(*mh));

    mh->total_allocated = zmalloc_used;
    mh->startup_allocated = server.initial_memory_usage;
    mh->peak_allocated = server.stat_peak_memory;

Review comment (suggestion):
It looks like `zmalloc_size((void *)o)` is part of every case in this long else-if chain. We could factor it out and initialize `size_t asize = zmalloc_size((void *)o);` once for all cases.

Code after:
                raxIterator cri;
                raxStart(&cri, cg->consumers);
                raxSeek(&cri, "^", NULL, 0);
                size_t inner_samples = 0;
                size_t inner_elesize = 0;
                while (inner_samples < sample_size && raxNext(&cri)) {
                    streamConsumer *consumer = cri.data;
                    inner_elesize += sizeof(*consumer);
                    inner_elesize += sdslen(consumer->name);
                    inner_elesize += raxAllocSize(consumer->pel);
                    /* Don't count NACKs again, they are shared with the
                     * consumer group PEL. */
                    inner_samples++;
                }
                raxStop(&cri);
                if (inner_samples) elesize += (double)inner_elesize / inner_samples * raxSize(cg->consumers);
                samples++;
            }
            raxStop(&ri);
            if (samples) asize += (double)elesize / samples * raxSize(s->cgroups);
        }
    } else if (o->type == OBJ_MODULE) {
        asize += moduleGetMemUsage(key, o, sample_size, dbid);
    } else {
        serverPanic("Unknown object type");
    }
    return asize;
}

/* Release data obtained with getMemoryOverheadData(). */
void freeMemoryOverheadData(struct serverMemOverhead *mh) {
    zfree(mh->db);
    zfree(mh);
}

/* Return a struct serverMemOverhead filled with memory overhead
 * information used for the MEMORY OVERHEAD and INFO command. The returned
 * structure pointer should be freed calling freeMemoryOverheadData(). */
struct serverMemOverhead *getMemoryOverheadData(void) {
    int j;
    size_t mem_total = 0;
    size_t mem = 0;
    size_t zmalloc_used = zmalloc_used_memory();
    struct serverMemOverhead *mh = zcalloc(sizeof(*mh));

    mh->total_allocated = zmalloc_used;
    mh->startup_allocated = server.initial_memory_usage;
    mh->peak_allocated = server.stat_peak_memory;
    mh->total_frag = (float)server.cron_malloc_stats.process_rss / server.cron_malloc_stats.zmalloc_used;
    mh->total_frag_bytes = server.cron_malloc_stats.process_rss - server.cron_malloc_stats.zmalloc_used;
    mh->allocator_frag =