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

Code before:
    }
    return;
}

static void replacePlaceholder(const size_t *indices, const size_t count, char *cmd, _Atomic uint64_t *key_counter) {
    if (count == 0) return;

    uint64_t key = 0;
    if (config.keyspacelen != 0) {
        if (config.sequential_replacement) {
            key = atomic_fetch_add_explicit(key_counter, 1, memory_order_relaxed);
        } else {
            key = random();
        }
        key %= config.keyspacelen;
    }

    /* convert key to string at first location */
    char *p = cmd + indices[0] + KEY_PLACEHOLDER_LEN - 1;
    for (size_t j = 0; j < KEY_PLACEHOLDER_LEN; j++) {
        *p = '0' + key % 10;
        key /= 10;
        p--;
    }

    /* copy the first instace to the other locations */
    for (size_t i = 1; i < count; i++) {
        char *placeholder = cmd + indices[i];
        memcpy(placeholder, cmd + indices[0], KEY_PLACEHOLDER_LEN);
    }
}

static void replacePlaceholders(char *cmd_data, int cmd_count) {
    static _Atomic uint64_t seq_key[PLACEHOLDER_COUNT] = {0};

    for (int cmd_index = 0; cmd_index < cmd_count; cmd_index++) {
        char *cmd = cmd_data + cmd_index * placeholders.cmd_len;

        /* for __rand_int__, multiple instances will have different values */
        size_t *indices = placeholders.indices[0];
        _Atomic uint64_t *key_counter = &seq_key[0];
        for (size_t i = 0; i < placeholders.count[0]; i++) {
            replacePlaceholder(indices + i, 1, cmd, key_counter);
        }

        /* For other placeholders, multiple occurences within the command will
         * have the same value */
        for (size_t placeholder = 1; placeholder < PLACEHOLDER_COUNT; placeholder++) {
            size_t *indices = placeholders.indices[placeholder];
            size_t count = placeholders.count[placeholder];
            _Atomic uint64_t *key_counter = &seq_key[placeholder];

Review comment (suggestion):
```suggestion
    /* copy the first instance to the other locations */
```

Code after:
            total_count++;
            p += PLACEHOLDER_LEN; // Move past the placeholder
        }
    }

    /* consolidate temp data into contiguous allocation */
    placeholders.index_data = zmalloc(sizeof(size_t) * total_count);
    size_t overall_index = 0;
    for (size_t placeholder = 0; placeholder < PLACEHOLDER_COUNT; placeholder++) {
        placeholders.indices[placeholder] = placeholders.index_data + overall_index;

        const size_t count = placeholders.count[placeholder];
        memcpy(placeholders.indices[placeholder], temp_indices[placeholder],
               sizeof(size_t) * count);
        overall_index += count;

        zfree(temp_indices[placeholder]);
    }
    return;
}

static void replacePlaceholder(const size_t *indices, const size_t count, char *cmd, _Atomic uint64_t *key_counter) {
    if (count == 0) return;

    uint64_t key = 0;
    if (config.keyspacelen != 0) {
        if (config.sequential_replacement) {
            key = atomic_fetch_add_explicit(key_counter, 1, memory_order_relaxed);
        } else {
            key = random();
        }
        key %= config.keyspacelen;
    }

    /* convert key to string at first location */
    char *p = cmd + indices[0] + PLACEHOLDER_LEN - 1;
    for (size_t j = 0; j < PLACEHOLDER_LEN; j++) {
        *p = '0' + key % 10;
        key /= 10;
        p--;
    }

    /* copy the first instance to the other locations */
    for (size_t i = 1; i < count; i++) {
        char *placeholder = cmd + indices[i];
        memcpy(placeholder, cmd + indices[0], PLACEHOLDER_LEN);
    }
}

static void replacePlaceholders(char *cmd_data, int cmd_count) {
    static _Atomic uint64_t seq_key[PLACEHOLDER_COUNT] = {0};