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

Code before:

/* Create a new hash table */
dict *dictCreate(dictType *type) {
    size_t metasize = type->dictMetadataBytes ? type->dictMetadataBytes(NULL) : 0;
    dict *d = zmalloc(sizeof(*d) + metasize);
    if (metasize > 0) {
        memset(dictMetadata(d), 0, metasize);
    }
    _dictInit(d, type);
    return d;
}

/* Initialize the hash table */
int _dictInit(dict *d, dictType *type) {
    _dictReset(d, 0);
    _dictReset(d, 1);
    d->type = type;
    d->rehashidx = -1;
    d->pauserehash = 0;
    d->pauseAutoResize = 0;
    return DICT_OK;
}

/* Resize or create the hash table,
 * when malloc_failed is non-NULL, it'll avoid panic if malloc fails (in which case it'll be set to 1).
 * Returns DICT_OK if resize was performed, and DICT_ERR if skipped. */
int _dictResize(dict *d, unsigned long size, int *malloc_failed) {
    if (malloc_failed) *malloc_failed = 0;

    /* We can't rehash twice if rehashing is ongoing. */
    assert(!dictIsRehashing(d));

    /* the new hash table */
    dictEntry **new_ht_table;
    unsigned long new_ht_used;
    signed char new_ht_size_exp = _dictNextExp(size);

    /* Detect overflows */
    size_t newsize = DICTHT_SIZE(new_ht_size_exp);
    if (newsize < size || newsize * sizeof(dictEntry *) < newsize) return DICT_ERR;

    /* Rehashing to the same table size is not useful. */
    if (new_ht_size_exp == d->ht_size_exp[0]) return DICT_ERR;

    /* Allocate the new hash table and initialize all pointers to NULL */
    if (malloc_failed) {
        new_ht_table = ztrycalloc(newsize * sizeof(dictEntry *));
        *malloc_failed = new_ht_table == NULL;
        if (*malloc_failed) return DICT_ERR;
    } else
        new_ht_table = zcalloc(newsize * sizeof(dictEntry *));

Review comment (suggestion):
```suggestion
    return !entryIsNoValue(de);
```

Code after:
static inline dictEntry *createEmbeddedEntry(void *key, dictEntry *next, dictType *dt) {
    size_t key_len = dt->embedKey(NULL, 0, key, NULL);
    embeddedDictEntry *entry = zmalloc(compactSizeEmbeddedDictEntry() + key_len);
    dt->embedKey(entry->key_buf, key_len, key, &entry->key_header_size);
    entry->next = next;
    return encodeMaskedPtr(entry, ENTRY_PTR_EMBEDDED);
}

static inline void *getEmbeddedKey(const dictEntry *de) {
    embeddedDictEntry *entry = (embeddedDictEntry *)decodeMaskedPtr(de);
    return &entry->key_buf[entry->key_header_size];
}

/* Decodes the pointer to an entry without value, when you know it is an entry
 * without value. Hint: Use entryIsNoValue to check. */
static inline dictEntryNoValue *decodeEntryNoValue(const dictEntry *de) {
    return decodeMaskedPtr(de);
}

static inline embeddedDictEntry *decodeEmbeddedEntry(const dictEntry *de) {
    return decodeMaskedPtr(de);
}

/* Returns 1 if the entry has a value field and 0 otherwise. */
static inline int entryHasValue(const dictEntry *de) {
    return entryIsNormal(de) || entryIsEmbedded(de);
}

/* ----------------------------- API implementation ------------------------- */

/* Reset hash table parameters already initialized with _dictInit()*/
static void _dictReset(dict *d, int htidx) {
    d->ht_table[htidx] = NULL;
    d->ht_size_exp[htidx] = -1;
    d->ht_used[htidx] = 0;
}

/* Create a new hash table */
dict *dictCreate(dictType *type) {
    validateDictType(type);
    size_t metasize = type->dictMetadataBytes ? type->dictMetadataBytes(NULL) : 0;
    dict *d = zmalloc(sizeof(*d) + metasize);
    if (metasize > 0) {
        memset(dictMetadata(d), 0, metasize);
    }
    _dictInit(d, type);
    return d;
}

/* Initialize the hash table */
int _dictInit(dict *d, dictType *type) {