Skip to content

Commit

Permalink
Remove code for shared integer objects
Browse files Browse the repository at this point in the history
When key is embedded in object, all objects in the database need
to be unique so we can't use shared objects as values.

Signed-off-by: Viktor Söderqvist <[email protected]>
  • Loading branch information
zuiderkwast committed Nov 9, 2024
1 parent f2e28e9 commit 970ef65
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 32 deletions.
38 changes: 11 additions & 27 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,10 @@ robj *createStringObjectFromLongLong(long long value) {
return createStringObjectFromLongLongWithOptions(value, LL2STROBJ_AUTO);
}

/* The function avoids returning a shared integer when LFU/LRU info
* are needed, that is, when the object is used as a value in the key
* space(for instance when the INCR command is used), and the server is
* configured to evict based on LFU/LRU, so we want LFU/LRU values
* specific for each key. */
/* The function doesn't return a shared integer when the object is used as a
* value in the key space (for instance when the INCR command is used). */
robj *createStringObjectFromLongLongForValue(long long value) {
if (canUseSharedObject()) {
/* If the maxmemory policy permits, we can still return shared integers */
return createStringObjectFromLongLongWithOptions(value, LL2STROBJ_AUTO);
} else {
return createStringObjectFromLongLongWithOptions(value, LL2STROBJ_NO_SHARED);
}
return createStringObjectFromLongLongWithOptions(value, LL2STROBJ_NO_SHARED);
}

/* Create a string object that contains an sds inside it. That means it can't be
Expand Down Expand Up @@ -830,23 +822,15 @@ robj *tryObjectEncodingEx(robj *o, int try_trim) {
* representable as a 32 nor 64 bit integer. */
len = sdslen(s);
if (len <= 20 && string2l(s, len, &value)) {
/* This object is encodable as a long. Try to use a shared object.
* Note that we avoid using shared integers when maxmemory is used
* because every object needs to have a private LRU field for the LRU
* algorithm to work well. */
if (canUseSharedObject() && value >= 0 && value < OBJ_SHARED_INTEGERS) {
/* This object is encodable as a long. */
if (o->encoding == OBJ_ENCODING_RAW) {
sdsfree(o->ptr);
o->encoding = OBJ_ENCODING_INT;
o->ptr = (void *)value;
return o;
} else if (o->encoding == OBJ_ENCODING_EMBSTR) {
decrRefCount(o);
return shared.integers[value];
} else {
if (o->encoding == OBJ_ENCODING_RAW) {
sdsfree(o->ptr);
o->encoding = OBJ_ENCODING_INT;
o->ptr = (void *)value;
return o;
} else if (o->encoding == OBJ_ENCODING_EMBSTR) {
decrRefCount(o);
return createStringObjectFromLongLongForValue(value);
}
return createStringObjectFromLongLongForValue(value);
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -3012,10 +3012,6 @@ int collateStringObjects(const robj *a, const robj *b);
int equalStringObjects(robj *a, robj *b);
unsigned long long estimateObjectIdleTime(robj *o);
void trimStringObjectIfNeeded(robj *o, int trim_small_values);
static inline int canUseSharedObject(void) {
/* We can't use shared objects because we embed the key in the value (robj). */
return 0;
}
#define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR)

/* Objects with key attached, AKA valkey objects */
Expand Down
1 change: 0 additions & 1 deletion src/t_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,6 @@ void incrDecrCommand(client *c, long long incr) {
value += incr;

if (o && o->refcount == 1 && o->encoding == OBJ_ENCODING_INT &&
(!canUseSharedObject() || value < 0 || value >= OBJ_SHARED_INTEGERS) &&
value >= LONG_MIN && value <= LONG_MAX) {
new = o;
o->ptr = (void *)((long)value);
Expand Down

0 comments on commit 970ef65

Please sign in to comment.