Skip to content

Commit 372bc71

Browse files
author
michalbiesek
committed
Optimize looking for PMEM by adding OBJ_STRING_PMEM
- decrRefCount could point the correct zfree only with looking for a type field
1 parent 5fb70a6 commit 372bc71

20 files changed

+163
-62
lines changed

src/aof.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ int rewriteAppendOnlyFileRio(rio *aof) {
13451345
expiretime = getExpire(db,&key);
13461346

13471347
/* Save the key and associated value */
1348-
if (o->type == OBJ_STRING) {
1348+
if (o->type == OBJ_STRING || o->type == OBJ_STRING_PMEM) {
13491349
/* Emit a SET command */
13501350
char cmd[]="*3\r\n$3\r\nSET\r\n";
13511351
if (rioWrite(aof,cmd,sizeof(cmd)-1) == 0) goto werr;

src/bitops.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ robj *lookupStringForBitCommand(client *c, size_t maxbit) {
483483
o = createObject(OBJ_STRING,sdsnewlen(NULL, byte+1));
484484
dbAdd(c->db,c->argv[1],o);
485485
} else {
486-
if (checkType(c,o,OBJ_STRING)) return NULL;
486+
if (checkTypeStringvariant(c,o)) return NULL;
487487
o = dbUnshareStringValue(c->db,c->argv[1],o);
488488
o->ptr = sdsgrowzero(o->ptr,byte+1);
489489
}
@@ -504,7 +504,7 @@ robj *lookupStringForBitCommand(client *c, size_t maxbit) {
504504
* If the source object is NULL the function is guaranteed to return NULL
505505
* and set 'len' to 0. */
506506
unsigned char *getObjectReadOnlyString(robj *o, long *len, char *llbuf) {
507-
serverAssert(o->type == OBJ_STRING);
507+
serverAssert(o->type == OBJ_STRING || o->type == OBJ_STRING_PMEM);
508508
unsigned char *p = NULL;
509509

510510
/* Set the 'p' pointer to the string, that can be just a stack allocated
@@ -572,7 +572,7 @@ void getbitCommand(client *c) {
572572
return;
573573

574574
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
575-
checkType(c,o,OBJ_STRING)) return;
575+
checkTypeStringvariant(c,o)) return;
576576

577577
byte = bitoffset >> 3;
578578
bit = 7 - (bitoffset & 0x7);
@@ -635,7 +635,7 @@ void bitopCommand(client *c) {
635635
continue;
636636
}
637637
/* Return an error if one of the keys is not a string. */
638-
if (checkType(c,o,OBJ_STRING)) {
638+
if (checkTypeStringvariant(c,o)) {
639639
unsigned long i;
640640
for (i = 0; i < j; i++) {
641641
if (objects[i])
@@ -774,7 +774,7 @@ void bitcountCommand(client *c) {
774774

775775
/* Lookup, check for type, and return 0 for non existing keys. */
776776
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
777-
checkType(c,o,OBJ_STRING)) return;
777+
checkTypeStringvariant(c,o)) return;
778778
p = getObjectReadOnlyString(o,&strlen,llbuf);
779779

780780
/* Parse start/end range if any. */
@@ -838,7 +838,7 @@ void bitposCommand(client *c) {
838838
addReplyLongLong(c, bit ? -1 : 0);
839839
return;
840840
}
841-
if (checkType(c,o,OBJ_STRING)) return;
841+
if (checkTypeStringvariant(c,o)) return;
842842
p = getObjectReadOnlyString(o,&strlen,llbuf);
843843

844844
/* Parse start/end range if any. */
@@ -1000,7 +1000,7 @@ void bitfieldGeneric(client *c, int flags) {
10001000
/* Lookup for read is ok if key doesn't exit, but errors
10011001
* if it's not a string. */
10021002
o = lookupKeyRead(c->db,c->argv[1]);
1003-
if (o != NULL && checkType(c,o,OBJ_STRING)) {
1003+
if (o != NULL && checkTypeStringvariant(c,o)) {
10041004
zfree(ops);
10051005
return;
10061006
}

src/db.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ int dbDelete(redisDb *db, robj *key) {
350350
* using an sdscat() call to append some data, or anything else.
351351
*/
352352
robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o) {
353-
serverAssert(o->type == OBJ_STRING);
353+
serverAssert(o->type == OBJ_STRING || o->type == OBJ_STRING_PMEM);
354354
if (o->refcount != 1 || o->encoding != OBJ_ENCODING_RAW) {
355355
robj *decoded = getDecodedObject(o);
356356
o = createRawStringObject(decoded->ptr, sdslen(decoded->ptr));
@@ -926,6 +926,7 @@ char* getObjectTypeName(robj *o) {
926926
} else {
927927
switch(o->type) {
928928
case OBJ_STRING: type = "string"; break;
929+
case OBJ_STRING_PMEM: type = "string"; break;
929930
case OBJ_LIST: type = "list"; break;
930931
case OBJ_SET: type = "set"; break;
931932
case OBJ_ZSET: type = "zset"; break;

src/debug.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void xorObjectDigest(redisDb *db, robj *keyobj, unsigned char *digest, robj *o)
125125
char buf[128];
126126

127127
/* Save the key and associated value */
128-
if (o->type == OBJ_STRING) {
128+
if (o->type == OBJ_STRING || o->type == OBJ_STRING_PMEM) {
129129
mixStringObjectDigest(digest,o);
130130
} else if (o->type == OBJ_LIST) {
131131
listTypeIterator *li = listTypeInitIterator(o,0,LIST_TAIL);
@@ -553,7 +553,7 @@ NULL
553553
val = dictGetVal(de);
554554
key = dictGetKey(de);
555555

556-
if (val->type != OBJ_STRING || !sdsEncodedObject(val)) {
556+
if ((val->type != OBJ_STRING && val->type != OBJ_STRING_PMEM) || !sdsEncodedObject(val)) {
557557
addReplyError(c,"Not an sds encoded string.");
558558
} else {
559559
addReplyStatusFormat(c,
@@ -833,7 +833,7 @@ void _serverAssertPrintClientInfo(const client *c) {
833833
char buf[128];
834834
char *arg;
835835

836-
if (c->argv[j]->type == OBJ_STRING && sdsEncodedObject(c->argv[j])) {
836+
if ((c->argv[j]->type == OBJ_STRING || c->argv[j]->type == OBJ_STRING_PMEM) && sdsEncodedObject(c->argv[j])) {
837837
arg = (char*) c->argv[j]->ptr;
838838
} else {
839839
snprintf(buf,sizeof(buf),"Object type: %u, encoding: %u",
@@ -849,7 +849,7 @@ void serverLogObjectDebugInfo(const robj *o) {
849849
serverLog(LL_WARNING,"Object type: %d", o->type);
850850
serverLog(LL_WARNING,"Object encoding: %d", o->encoding);
851851
serverLog(LL_WARNING,"Object refcount: %d", o->refcount);
852-
if (o->type == OBJ_STRING && sdsEncodedObject(o)) {
852+
if ((o->type == OBJ_STRING || o->type == OBJ_STRING_PMEM) && sdsEncodedObject(o)) {
853853
serverLog(LL_WARNING,"Object raw string len: %zu", sdslen(o->ptr));
854854
if (sdslen(o->ptr) < 4096) {
855855
sds repr = sdscatrepr(sdsempty(),o->ptr,sdslen(o->ptr));

src/defrag.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ robj *activeDefragStringOb(robj* ob, long *defragged) {
109109
return NULL;
110110

111111
/* try to defrag robj (only if not an EMBSTR type (handled below). */
112-
if (ob->type!=OBJ_STRING || ob->encoding!=OBJ_ENCODING_EMBSTR) {
112+
if ((ob->type!=OBJ_STRING && ob->type!=OBJ_STRING_PMEM) || ob->encoding!=OBJ_ENCODING_EMBSTR) {
113113
if ((ret = activeDefragAlloc(ob))) {
114114
ob = ret;
115115
(*defragged)++;
116116
}
117117
}
118118

119119
/* try to defrag string object */
120-
if (ob->type == OBJ_STRING) {
120+
if ((ob->type == OBJ_STRING) || (ob->type == OBJ_STRING_PMEM)) {
121121
if(ob->encoding==OBJ_ENCODING_RAW) {
122122
sds newsds = activeDefragSds((sds)ob->ptr);
123123
if (newsds) {
@@ -833,7 +833,7 @@ long defragKey(redisDb *db, dictEntry *de) {
833833
ob = newob;
834834
}
835835

836-
if (ob->type == OBJ_STRING) {
836+
if ((ob->type == OBJ_STRING) || (ob->type == OBJ_STRING_PMEM)) {
837837
/* Already handled in activeDefragStringOb. */
838838
} else if (ob->type == OBJ_LIST) {
839839
if (ob->encoding == OBJ_ENCODING_QUICKLIST) {

src/gopher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void processGopherRequest(client *c) {
5353
robj *o = lookupKeyRead(c->db,keyname);
5454

5555
/* If there is no such key, return with a Gopher error. */
56-
if (o == NULL || o->type != OBJ_STRING) {
56+
if (o == NULL || (o->type != OBJ_STRING && o->type != OBJ_STRING_PMEM)) {
5757
char *errstr;
5858
if (o == NULL)
5959
errstr = "Error: no content at the specified key";

src/hyperloglog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ int isHLLObjectOrReply(client *c, robj *o) {
11501150
struct hllhdr *hdr;
11511151

11521152
/* Key exists, check type */
1153-
if (checkType(c,o,OBJ_STRING))
1153+
if (checkTypeStringvariant(c,o))
11541154
return C_ERR; /* Error already sent. */
11551155

11561156
if (!sdsEncodedObject(o)) goto invalid;

src/module.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,7 @@ int RM_KeyType(RedisModuleKey *key) {
20382038
* defines as desired. */
20392039
switch(key->value->type) {
20402040
case OBJ_STRING: return REDISMODULE_KEYTYPE_STRING;
2041+
case OBJ_STRING_PMEM: return REDISMODULE_KEYTYPE_STRING;
20412042
case OBJ_LIST: return REDISMODULE_KEYTYPE_LIST;
20422043
case OBJ_SET: return REDISMODULE_KEYTYPE_SET;
20432044
case OBJ_ZSET: return REDISMODULE_KEYTYPE_ZSET;
@@ -2057,6 +2058,7 @@ size_t RM_ValueLength(RedisModuleKey *key) {
20572058
if (key == NULL || key->value == NULL) return 0;
20582059
switch(key->value->type) {
20592060
case OBJ_STRING: return stringObjectLen(key->value);
2061+
case OBJ_STRING_PMEM: return stringObjectLen(key->value);
20602062
case OBJ_LIST: return listTypeLength(key->value);
20612063
case OBJ_SET: return setTypeSize(key->value);
20622064
case OBJ_ZSET: return zsetLength(key->value);
@@ -2202,7 +2204,7 @@ char *RM_StringDMA(RedisModuleKey *key, size_t *len, int mode) {
22022204
return emptystring;
22032205
}
22042206

2205-
if (key->value->type != OBJ_STRING) return NULL;
2207+
if (key->value->type != OBJ_STRING && key->value->type != OBJ_STRING_PMEM) return NULL;
22062208

22072209
/* For write access, and even for read access if the object is encoded,
22082210
* we unshare the string (that has the side effect of decoding it). */
@@ -2227,7 +2229,7 @@ char *RM_StringDMA(RedisModuleKey *key, size_t *len, int mode) {
22272229
* unless the new length value requested is zero. */
22282230
int RM_StringTruncate(RedisModuleKey *key, size_t newlen) {
22292231
if (!(key->mode & REDISMODULE_WRITE)) return REDISMODULE_ERR;
2230-
if (key->value && key->value->type != OBJ_STRING) return REDISMODULE_ERR;
2232+
if (key->value && key->value->type != OBJ_STRING && key->value->type != OBJ_STRING_PMEM) return REDISMODULE_ERR;
22312233
if (newlen > 512*1024*1024) return REDISMODULE_ERR;
22322234

22332235
/* Empty key and new len set to 0. Just return REDISMODULE_OK without

src/networking.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ size_t sdsZmallocSize(sds s) {
4949
/* Return the amount of memory used by the sds string at object->ptr
5050
* for a string object. */
5151
size_t getStringObjectSdsUsedMemory(robj *o) {
52-
serverAssertWithInfo(NULL,o,o->type == OBJ_STRING);
52+
serverAssertWithInfo(NULL,o,o->type == OBJ_STRING || o->type == OBJ_STRING_PMEM);
5353
switch(o->encoding) {
5454
case OBJ_ENCODING_RAW: return sdsZmallocSize(o->ptr);
5555
case OBJ_ENCODING_EMBSTR: return zmalloc_size(o)-sizeof(robj);
@@ -1718,7 +1718,7 @@ int processMultibulkBuffer(client *c) {
17181718
sdsclear(c->querybuf);
17191719
} else {
17201720
c->argv[c->argc++] =
1721-
createStringObject(c->querybuf+c->qb_pos,c->bulklen);
1721+
createStringObjectOptim(c->querybuf+c->qb_pos,c->bulklen);
17221722
c->qb_pos += c->bulklen+2;
17231723
}
17241724
c->bulklen = -1;

0 commit comments

Comments
 (0)