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

Code before:
 */

typedef enum {
    ENTRY_ENC_EMB_VALUE = 0,
    ENTRY_ENC_PTR_VALUE = 1
} hashTypeEntryEnc;

#define FIELD_SDS_AUX_BIT_ENTRY_HAS_PTR_VALUE 0

/* Struct used for ENTRY_ENC_PTR_VALUE. */
typedef struct {
    sds value;
    char field_data[];
} hashTypeEntryPtrValue;

static inline hashTypeEntryEnc entryGetEncoding(const hashTypeEntry *entry) {
    return sdsGetAuxBit(entry, FIELD_SDS_AUX_BIT_ENTRY_HAS_PTR_VALUE);
}

/* Returns the containing struct for an entry without embedded value. */
static hashTypeEntryPtrValue *getEntryStruct(const hashTypeEntry *entry) {
    serverAssert(entryGetEncoding(entry) == ENTRY_ENC_PTR_VALUE);
    unsigned char *buf = sdsAllocPtr(entry);
    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);

Review comment (suggestion):
I understand the motivation but IMO we should use a more explicit name for this:

```suggestion
static inline sds *encodedPtrValueEntryGetValueref(const hashTypeEntry *entry) {
    serverAssert(entryGetEncoding(entry) == ENTRY_ENC_PTR_VALUE);
    return (sds *)(sdsAllocPtr((sds)entry)) - 1;
}
```

Code after:
 */

/* The maximum allocation size we want to use for entries with embedded
 * values. */
#define EMBED_VALUE_MAX_ALLOC_SIZE 128

/* SDS aux flag. If set, it indicates that the entry has an embedded value
 * pointer located in memory before the embedded field. If unset, the entry
 * instead has an embedded value located after the embedded field. */
#define FIELD_SDS_AUX_BIT_ENTRY_HAS_VALUE_PTR 0

static inline bool entryHasValuePtr(const hashTypeEntry *entry) {
    return sdsGetAuxBit(entry, FIELD_SDS_AUX_BIT_ENTRY_HAS_VALUE_PTR);
}

/* Returns the location of a pointer to a separately allocated value. Only for
 * an entry without an embedded value. */
static sds *hashTypeEntryGetValueRef(const hashTypeEntry *entry) {
    serverAssert(entryHasValuePtr(entry));
    char *field_data = sdsAllocPtr(entry);
    field_data -= sizeof(sds *);
    return (sds *)field_data;
}

/* 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 {