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

Code before:
/* The hashTypeEntry pointer is the field sds. We encode the entry layout in the
 * field sds field for unused space, so sdsavail(entry) gives the entry encoding.
 *
 * ENTRY_ENC_EMB_VALUE, used when it fits in a cache line:
 *
 *     +--------------+----------------+--------------+
 *     | field        | 1 byte         | value        |
 *     | hdr "foo" \0 | value-hdr-size | hdr "bar" \0 |
 *     +------^-------+----------------+--------------+
 *            |
 *            |
 *          entry pointer = field sds
 *
 * ENTRY_ENC_PTR_VALUE, used for larger fields and values:
 *
 *     +-------+--------------+
 *     | value | field        |
 *     | ptr   | hdr "foo" \0 |
 *     +-------+------^-------+
 *                    |
 *                    |
 *                 entry pointer = field sds
 */

typedef enum {
    ENTRY_ENC_EMB_VALUE = 0,
    ENTRY_ENC_PTR_VALUE
} hashTypeEntryEnc;

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

static inline hashTypeEntryEnc entryGetEncoding(const hashTypeEntry *entry) {
    return sdsavail(entry);
}

/* 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;
}

static inline bool canUseEmbeddedValueEntry(const_sds field, const_sds value) {
    size_t field_size = sdscopytobuffer(NULL, 0, field, NULL);
    size_t value_size = sdscopytobuffer(NULL, 0, value, NULL);
    return field_size + 1 + value_size <= CACHE_LINE_SIZE;

Review comment (suggestion):
Sorry for turning into Scrooge McDuck.
I actually thought about this the opposite way around. I think we can FORCE non sds5 fields only when the value IS embedded.
So the question of: canUseEmbeddedValueEntry can take into account the new field sds header length and the value sds size.
This may also allow us to use the alloc of embedded fields to hold the value header size, so when the field sds alloc is 0 I actually know this is a ENTRY_ENC_PTR_VALUE and in case it is not 0 I know it is ENTRY_ENC_EMB_VALUE.

Code after:
/* The hashTypeEntry pointer is the field sds. We encode the entry layout type
 * in the field SDS header. Field type SDS_TYPE_5 doesn't have any spare bits to
 * encode this so we use it only for the first layout type.
 *
 * Entry with embedded value, used for small sizes. The value is stored as
 * SDS_TYPE_8. The field can use any SDS type.
 *
 *     +--------------+---------------+
 *     | field        | value         |
 *     | hdr "foo" \0 | hdr8 "bar" \0 |
 *     +------^-------+---------------+
 *            |
 *            |
 *          entry pointer = field sds
 *
 * Entry with value pointer, used for larger fields and values. The field is SDS
 * type 8 or higher.
 *
 *     +-------+--------------+
 *     | value | field        |
 *     | ptr   | hdr "foo" \0 |
 *     +-------+------^-------+
 *                    |
 *                    |
 *                 entry pointer = field sds
 */

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