Skip to content

Commit a4271ce

Browse files
pkarproot
authored andcommitted
Move dictEntry to PM
dictEntry instances will be allocated to PM. Involves only main dictionary entries.
1 parent d42e900 commit a4271ce

File tree

7 files changed

+46
-15
lines changed

7 files changed

+46
-15
lines changed

src/db.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,16 @@ robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply) {
177177
*
178178
* The program is aborted if the key already exists. */
179179
void dbAdd(redisDb *db, robj *key, robj *val) {
180-
sds copy;
180+
sds copy = NULL;
181181
if (server.keys_on_pmem)
182182
copy = sdsdupPM(key->ptr);
183183
else
184184
copy = sdsdup(key->ptr);
185-
int retval = dictAdd(db->dict, copy, val);
185+
int retval = 0;
186+
if (server.dictionary_entries_on_pmem)
187+
retval = dictAddPM(db->dict, copy, val);
188+
else
189+
retval = dictAdd(db->dict, copy, val);
186190

187191
serverAssertWithInfo(NULL,key,retval == DICT_OK);
188192
if (val->type == OBJ_LIST ||
@@ -1167,7 +1171,7 @@ void setExpire(client *c, redisDb *db, robj *key, long long when) {
11671171
/* Reuse the sds from the main dict in the expire dict */
11681172
kde = dictFind(db->dict,key->ptr);
11691173
serverAssertWithInfo(NULL,key,kde != NULL);
1170-
de = dictAddOrFind(db->expires,dictGetKey(kde));
1174+
de = dictAddOrFind(db->expires,dictGetKey(kde),server.dictionary_entries_on_pmem);
11711175
dictSetSignedIntegerVal(de,when);
11721176

11731177
int writable_slave = server.masterhost && server.repl_slave_ro == 0;

src/dict.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ static void _dictRehashStep(dict *d) {
264264
/* Add an element to the target hash table */
265265
int dictAdd(dict *d, void *key, void *val)
266266
{
267-
dictEntry *entry = dictAddRaw(d,key,NULL);
267+
dictEntry *entry = dictAddRaw(d,key,NULL,0);
268268

269269
if (!entry) return DICT_ERR;
270270
dictSetVal(d, entry, val);
@@ -289,7 +289,7 @@ int dictAdd(dict *d, void *key, void *val)
289289
*
290290
* If key was added, the hash entry is returned to be manipulated by the caller.
291291
*/
292-
dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing)
292+
dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing, int dictionaryEntriesOnPmem)
293293
{
294294
long index;
295295
dictEntry *entry;
@@ -307,7 +307,10 @@ dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing)
307307
* system it is more likely that recently added entries are accessed
308308
* more frequently. */
309309
ht = dictIsRehashing(d) ? &d->ht[1] : &d->ht[0];
310-
entry = zmalloc(sizeof(*entry));
310+
if (dictionaryEntriesOnPmem)
311+
entry = zmalloc_pmem(sizeof(*entry));
312+
else
313+
entry = zmalloc(sizeof(*entry));
311314
entry->next = ht->table[index];
312315
ht->table[index] = entry;
313316
ht->used++;
@@ -317,6 +320,29 @@ dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing)
317320
return entry;
318321
}
319322

323+
#ifdef USE_MEMKIND
324+
int dictAddPM(dict *d, void *key, void *val)
325+
{
326+
dictEntry *entry = dictAddRaw(d,key,NULL,1);
327+
328+
if (!entry) return DICT_ERR;
329+
dictSetVal(d, entry, val);
330+
return DICT_OK;
331+
}
332+
333+
#else
334+
int dictAddPM(dict *d, void *key, void *val)
335+
{
336+
(void)(d);
337+
(void)(key);
338+
(void)(val);
339+
printf("ERROR: dictAddPM is supported only by memkind\n");
340+
exit(1);
341+
/* unreachable */
342+
return NULL;
343+
}
344+
#endif
345+
320346
/* Add or Overwrite:
321347
* Add an element, discarding the old value if the key already exists.
322348
* Return 1 if the key was added from scratch, 0 if there was already an
@@ -328,7 +354,7 @@ int dictReplace(dict *d, void *key, void *val)
328354

329355
/* Try to add the element. If the key
330356
* does not exists dictAdd will succeed. */
331-
entry = dictAddRaw(d,key,&existing);
357+
entry = dictAddRaw(d,key,&existing,0);
332358
if (entry) {
333359
dictSetVal(d, entry, val);
334360
return 1;
@@ -352,9 +378,9 @@ int dictReplace(dict *d, void *key, void *val)
352378
* existing key is returned.)
353379
*
354380
* See dictAddRaw() for more information. */
355-
dictEntry *dictAddOrFind(dict *d, void *key) {
381+
dictEntry *dictAddOrFind(dict *d, void *key, int dictionaryEntriesOnPmem) {
356382
dictEntry *entry, *existing;
357-
entry = dictAddRaw(d,key,&existing);
383+
entry = dictAddRaw(d,key,&existing,dictionaryEntriesOnPmem);
358384
return entry ? entry : existing;
359385
}
360386

src/dict.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ typedef void (dictScanBucketFunction)(void *privdata, dictEntry **bucketref);
151151
dict *dictCreate(dictType *type, void *privDataPtr);
152152
int dictExpand(dict *d, unsigned long size);
153153
int dictAdd(dict *d, void *key, void *val);
154-
dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing);
155-
dictEntry *dictAddOrFind(dict *d, void *key);
154+
int dictAddPM(dict *d, void *key, void *val);
155+
dictEntry *dictAddRaw(dict *d, void *key, dictEntry **existing, int dictionaryEntriesOnPmem);
156+
dictEntry *dictAddOrFind(dict *d, void *key, int dictionaryEntriesOnPmem);
156157
int dictReplace(dict *d, void *key, void *val);
157158
int dictDelete(dict *d, const void *key);
158159
dictEntry *dictUnlink(dict *ht, const void *key);

src/expire.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void rememberSlaveKeyWithExpire(redisDb *db, robj *key) {
439439
}
440440
if (db->id > 63) return;
441441

442-
dictEntry *de = dictAddOrFind(slaveKeysWithExpire,key->ptr);
442+
dictEntry *de = dictAddOrFind(slaveKeysWithExpire,key->ptr,server.dictionary_entries_on_pmem);
443443
/* If the entry was just created, set it to a copy of the SDS string
444444
* representing the key: we don't want to need to take those keys
445445
* in sync with the main DB. The keys will be removed by expireSlaveKeys()

src/sentinel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3853,7 +3853,7 @@ int sentinelLeaderIncr(dict *counters, char *runid) {
38533853
dictEntry *existing, *de;
38543854
uint64_t oldval;
38553855

3856-
de = dictAddRaw(counters,runid,&existing);
3856+
de = dictAddRaw(counters,runid,&existing,0);
38573857
if (existing) {
38583858
oldval = dictGetUnsignedIntegerVal(existing);
38593859
dictSetUnsignedIntegerVal(existing,oldval+1);

src/t_set.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int setTypeAdd(robj *subject, sds value) {
5353
long long llval;
5454
if (subject->encoding == OBJ_ENCODING_HT) {
5555
dict *ht = subject->ptr;
56-
dictEntry *de = dictAddRaw(ht,value,NULL);
56+
dictEntry *de = dictAddRaw(ht,value,NULL,0);
5757
if (de) {
5858
dictSetKey(ht,de,sdsdup(value));
5959
dictSetVal(ht,de,NULL);

src/t_zset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,7 @@ void zunionInterGenericCommand(client *c, robj *dstkey, int op) {
23322332
if (isnan(score)) score = 0;
23332333

23342334
/* Search for this element in the accumulating dictionary. */
2335-
de = dictAddRaw(accumulator,zuiSdsFromValue(&zval),&existing);
2335+
de = dictAddRaw(accumulator,zuiSdsFromValue(&zval),&existing,0);
23362336
/* If we don't have it, we need to create a new entry. */
23372337
if (!existing) {
23382338
tmp = zuiNewSdsFromValue(&zval);

0 commit comments

Comments
 (0)