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

Code before:
        uint8_t field_hdr_size;
        sdscopytobuffer(buf, field_size, field, &field_hdr_size);
        embedded_field_sds = (sds)(buf + field_hdr_size);
        size_t value_buf_size = buf_size - field_size - 1;
        sdscopytobuffer(buf + field_size + 1, value_buf_size, value, buf + field_size);
        /* Unused space in the field sds is zero. We use this to encode that the
         * entry is an embedded-value entry. For sds5, there is no unused space,
         * which is why the value of ENTRY_ENC_EMB_VALUE was chosen to be 0. */
        serverAssert(entryGetEncoding(embedded_field_sds) == ENTRY_ENC_EMB_VALUE);
        sdsfree(value);
    } else {
        /* Embed field, but not value. */
        unsigned char flags = field[-1];
        bool field_is_sds5 = (flags & SDS_TYPE_MASK) == SDS_TYPE_5;
        if (field_is_sds5) field_size += 2;
        size_t alloc_size = sizeof(void *) + field_size;
        hashTypeEntryPtrValue *entry = zmalloc(alloc_size);
        entry->value = value;
        if (field_is_sds5) {
            /* We can't use SDS_TYPE_5 to encode extra information in the unused
             * allocation size, so convert to SDS_TYPE_8. */
            struct sdshdr8 *sh = (void *)entry->field_data;
            sh->flags = SDS_TYPE_8;
            sh->len = sdslen(field);
            embedded_field_sds = (sds)(entry->field_data + sizeof(struct sdshdr8));
            memcpy(embedded_field_sds, field, sdslen(field) + 1);
        } else {
            uint8_t hdr_size;
            sdscopytobuffer(entry->field_data, field_size, field, &hdr_size);
            embedded_field_sds = (sds)(entry->field_data + hdr_size);
        }
        /* Flag that this is an entry with a value-pointer, not embedded value. */
        sdssetalloc(embedded_field_sds, sdslen(embedded_field_sds) + ENTRY_ENC_PTR_VALUE);
    }
    return (void *)embedded_field_sds;
}

/* The entry pointer is the field sds, but that's an implementation detail. */
sds hashTypeEntryGetField(const hashTypeEntry *entry) {
    return (sds)entry;
}

sds hashTypeEntryGetValue(const hashTypeEntry *entry) {
    switch (entryGetEncoding(entry)) {
    case ENTRY_ENC_EMB_VALUE: {
        /* To find the embedded value sds content, skip field, field null term,
         * value hdr_size and hdr. */
        size_t offset = sdslen(entry) + 1;
        char *buf = (char *)entry + offset;
        char hdr_size = buf[0];
        return buf + 1 + hdr_size;

Review comment (suggestion):
May I suggest something?
We can add another type of sdscopytobuffer which forces a specific type and can also be used by sdscopytobuffer:

```
/* This method copies the sds `s` into `buf` which is the target character buffer. */
size_t sdscopytobufferwithtype(unsigned char *buf, size_t buf_len, char type, const sds s, uint8_t *hdr_size) {
    assert((s[-1] & SDS_TYPE_MASK) <= type);
    size_t header_len = sdsHdrSize(type);
    size_t str_len = sdslen(s);
    size_t alloc_size = header_len + str_len + 1;
    if (hdr_size) *hdr_size = sdsHdrSize(type);
    if (buf == NULL) {
        return alloc_size;
    }
    assert(buf_len >= alloc_size);
    sds embed_sds = (sds)(buf + header_len);
    embed_sds[-1] = type & SDS_TYPE_MASK;
    sdssetlen(embed_sds, str_len);
    sdssetalloc(embed_sds, alloc_size);
    memcpy(embed_sds, s, str_len + 1);
    return alloc_size;
}
```

Code after:
        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. */
sds hashTypeEntryGetField(const hashTypeEntry *entry) {
    return (sds)entry;
}

sds hashTypeEntryGetValue(const hashTypeEntry *entry) {
    if (entryHasValuePtr(entry)) {
        return *hashTypeEntryGetValueRef(entry);
    } else {
        /* Skip field content, field null terminator and value sds8 hdr. */
        size_t offset = sdslen(entry) + 1 + sdsHdrSize(SDS_TYPE_8);
        return (char *)entry + offset;
    }
}

/* Returns the address of the entry allocation. */
static void *hashTypeEntryAllocPtr(hashTypeEntry *entry) {
    char *buf = sdsAllocPtr(entry);
    if (entryHasValuePtr(entry)) {
        buf -= sizeof(sds *);
    }
    return buf;