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

Code before:
    buf -= offsetof(hashTypeEntryPtrValue, field_data);
    return (void *)buf;
}

/* takes ownership of value, does not take ownership of field */
hashTypeEntry *hashTypeCreateEntry(sds field, sds value) {
    size_t field_len = sdslen(field);
    int field_sds_type = sdsReqType(field_len);
    size_t field_size = sdsReqSize(field_len, field_sds_type);
    size_t value_len = sdslen(value);
    size_t value_size = sdsReqSize(value_len, SDS_TYPE_8);
    sds embedded_field_sds;
    if (field_size + value_size <= CACHE_LINE_SIZE) {
        /* Embed field and value. Value is fixed to SDS_TYPE_8.
         *
         *     +--------------+---------------+
         *     | field        | value         |
         *     | hdr "foo" \0 | hdr8 "bar" \0 |
         *     +--------------+---------------+
         */
        size_t min_size = field_size + value_size;
        size_t buf_size;
        char *buf = zmalloc_usable(min_size, &buf_size);
        sdswrite(buf, field_size, field_sds_type, field, field_len);
        sdswrite(buf + field_size, buf_size - field_size, SDS_TYPE_8, value, value_len);
        embedded_field_sds = (sds)(buf + sdsHdrSize(field_sds_type));
        /* Field sds aux bits are zero, which we use for this entry encoding. */
        sdsSetAuxBit(embedded_field_sds, FIELD_SDS_AUX_BIT_ENTRY_HAS_PTR_VALUE, 0);
        serverAssert(entryGetEncoding(embedded_field_sds) == ENTRY_ENC_EMB_VALUE);
        sdsfree(value);
    } else {
        /* Embed field, but not value. Field must be >= SDS_TYPE_8 to encode to
         * indicate this type of entry.
         *
         *     +-------+---------------+
         *     | value | field         |
         *     | ptr   | hdr8 "foo" \0 |
         *     +-------+---------------+
         */
        char field_sds_type = sdsReqType(field_len);
        if (field_sds_type == SDS_TYPE_5) field_sds_type = SDS_TYPE_8;
        field_size = sdsReqSize(field_len, field_sds_type);
        size_t alloc_size = sizeof(void *) + field_size;
        hashTypeEntryPtrValue *entry = zmalloc(alloc_size);
        entry->value = value;
        sdswrite(entry->field_data, field_size, field_sds_type, field, field_len);
        embedded_field_sds = (sds)(entry->field_data + sdsHdrSize(field_sds_type));
        /* Store the entry encoding type in sds aux bits. */
        sdsSetAuxBit(embedded_field_sds, FIELD_SDS_AUX_BIT_ENTRY_HAS_PTR_VALUE, 1);
        serverAssert(entryGetEncoding(embedded_field_sds) == ENTRY_ENC_PTR_VALUE);
    }

Review comment (suggestion):
```suggestion
        embedded_field_sds = sdswrite(buf, field_size, field_sds_type, field, field_len);
        sdswrite(buf + field_size, buf_size - field_size, SDS_TYPE_8, value, value_len);
```

Code after:

/* takes ownership of value, does not take ownership of field */
hashTypeEntry *hashTypeCreateEntry(sds field, sds value) {
    size_t field_len = sdslen(field);
    int field_sds_type = sdsReqType(field_len);
    size_t field_size = sdsReqSize(field_len, field_sds_type);
    size_t value_len = sdslen(value);
    size_t value_size = sdsReqSize(value_len, SDS_TYPE_8);
    sds embedded_field_sds;
    if (field_size + value_size <= EMBED_VALUE_MAX_ALLOC_SIZE) {
        /* Embed field and value. Value is fixed to SDS_TYPE_8. Unused
         * allocation space is recorded in the embedded value's SDS header.
         *
         *     +--------------+---------------+
         *     | field        | value         |
         *     | hdr "foo" \0 | hdr8 "bar" \0 |
         *     +--------------+---------------+
         */
        size_t min_size = field_size + value_size;
        size_t buf_size;
        char *buf = zmalloc_usable(min_size, &buf_size);
        embedded_field_sds = sdswrite(buf, field_size, field_sds_type, field, field_len);
        sdswrite(buf + field_size, buf_size - field_size, SDS_TYPE_8, value, value_len);
        /* Field sds aux bits are zero, which we use for this entry encoding. */
        sdsSetAuxBit(embedded_field_sds, FIELD_SDS_AUX_BIT_ENTRY_HAS_VALUE_PTR, 0);
        serverAssert(!entryHasValuePtr(embedded_field_sds));
        sdsfree(value);
    } else {
        /* Embed field, but not value. Field must be >= SDS_TYPE_8 to encode to
         * indicate this type of entry.
         *
         *     +-------+---------------+
         *     | value | field         |
         *     | ptr   | hdr8 "foo" \0 |
         *     +-------+---------------+
         */
        char field_sds_type = sdsReqType(field_len);
        if (field_sds_type == SDS_TYPE_5) field_sds_type = SDS_TYPE_8;
        field_size = sdsReqSize(field_len, field_sds_type);
        size_t alloc_size = sizeof(sds *) + field_size;
        char *buf = zmalloc(alloc_size);
        *(sds *)buf = value;
        embedded_field_sds = sdswrite(buf + sizeof(sds *), field_size, field_sds_type, field, field_len);
        /* Store the entry encoding type in sds aux bits. */
        sdsSetAuxBit(embedded_field_sds, FIELD_SDS_AUX_BIT_ENTRY_HAS_VALUE_PTR, 1);
        serverAssert(entryHasValuePtr(embedded_field_sds));
    }
    return (void *)embedded_field_sds;
}

/* The entry pointer is the field sds, but that's an implementation detail. */