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

Code before:
 * 3. Multiply by 10 and horizontally add adjacent pairs of intermediate values.
 *    Format (16 bytes, 8 bits each):
 *      00 00 00 00 00 00 00 00 00 00 00 12 34 56 78 90
 * 4. Multiply by 100 and horizontally add adjacent pairs of intermediate values.
 *    Format (8 bytes, 16 bits each):
 *      0000 0000 0000 0000 0000 0012 3456 7890
 * 5. Multiply by 10000 and horizontally add adjacent pairs of intermediate values.
 *    Format (4 bytes, 32 bits each):
 *      00000000 00000000 00000012 34567890
 * 6. Extract the final value.
 * Output: *value = 1234567890
 */
ATTRIBUTE_TARGET_AVX512
static int string2llAVX512(const char *s, unsigned long slen, long long *value) {
    const char *p = s;
    unsigned long plen = 0;
    int negative = 0;

    /* Abort if length indicates this cannot possibly be an int */
    if (slen == 0 || slen >= LONG_STR_SIZE) return 0;

    /* Special case: first and only digit is 0. */
    if (slen == 1 && p[0] == '0') {
        if (value != NULL) *value = 0;
        return 1;
    }

    if (p[0] == '-') {
        negative = 1;
        p++;
        plen++;

        /* Abort on only a negative sign. */
        if (plen == slen) return 0;
    }

    /* If first digit is 0, the string should just be 0. */
    if (p[0] == '0') {
        return 0;
    }
    const __m256i ascii0 = _mm256_set1_epi8('0');
    const __m256i nine = _mm256_set1_epi8(9);
    uint32_t mask = (uint32_t)(0xFFFFFFFF << (32 - slen + plen));
    __m256i input = _mm256_maskz_loadu_epi8(mask, s + slen - 32);
    /* Load the string into SIMD registers and subtract '0' to convert ASCII to integers */
    __m256i ascii_digits = _mm256_maskz_sub_epi8(mask, input, ascii0);
    /* Validate that all characters are digits (0-9) */
    uint32_t nondigits = _mm256_mask_cmpgt_epu8_mask(mask, ascii_digits, nine);
    if (nondigits) {
        return 0;
    }

Review comment (performance):
Could we modify this case to include all single-digit numbers without any performance impact?

In the hot path in command parsing, `string2ll()` is used in `processMultibulkBuffer()`. Here, I guess it's often single digit when command names are parsed and key names are parsed.

```suggestion
    /* Special case: first and only digit is 0. Also handle single-digit 1-9 here. */
    if (slen == 1 && p[0] >= '0' && p[0] <= '9') {
        if (value != NULL) *value = p[0] - '0';
        return 1;
    }
```

Code after:
 * 3. Multiply by 10 and horizontally add adjacent pairs of intermediate values.
 *    Format (16 bytes, 8 bits each):
 *      00 00 00 00 00 00 00 00 00 00 00 12 34 56 78 90
 * 4. Multiply by 100 and horizontally add adjacent pairs of intermediate values.
 *    Format (8 bytes, 16 bits each):
 *      0000 0000 0000 0000 0000 0012 3456 7890
 * 5. Multiply by 10000 and horizontally add adjacent pairs of intermediate values.
 *    Format (4 bytes, 32 bits each):
 *      00000000 00000000 00000012 34567890
 * 6. Extract the final value.
 * Output: *value = 1234567890
 */
ATTRIBUTE_TARGET_AVX512
static int string2llAVX512(const char *s, unsigned long slen, long long *value) {
    const char *p = s;
    unsigned long plen = 0;
    int negative = 0;

    /* Abort if length indicates this cannot possibly be an int */
    if (slen == 0 || slen >= LONG_STR_SIZE) return 0;

    /* Special case: first and only digit is 0. Also handle single-digit 1-9 here. */
    if (slen == 1 && p[0] >= '0' && p[0] <= '9') {
        if (value != NULL) *value = p[0] - '0';
        return 1;
    }

    if (p[0] == '-') {
        negative = 1;
        p++;
        plen++;

        /* Abort on only a negative sign. */
        if (plen == slen) return 0;
    }

    /* If first digit is 0, the string should just be 0. */
    if (unlikely(p[0] == '0')) {
        return 0;
    }

    const __m256i ascii0 = _mm256_set1_epi8('0');
    const __m256i nine = _mm256_set1_epi8(9);
    uint32_t mask = (uint32_t)(0xFFFFFFFF << (32 - slen + plen));
    __m256i input = _mm256_maskz_loadu_epi8(mask, s + slen - 32);
    /* Load the string into SIMD registers and subtract '0' to convert ASCII to integers */
    __m256i ascii_digits = _mm256_maskz_sub_epi8(mask, input, ascii0);
    /* Validate that all characters are digits (0-9) */
    uint32_t nondigits = _mm256_mask_cmpgt_epu8_mask(mask, ascii_digits, nine);
    if (nondigits) {
        return 0;