Skip to content

Commit 034d16a

Browse files
Increase embedded string threshold from 64 to 128 bytes (valkey-io#3397)
This PR increases the embedded string threshold to 128 bytes (2 cache lines) in order to improve memory overhead. I also fixed the tests that pertained to embedded values. Closes valkey-io#3025 --------- Signed-off-by: Nikhil Manglore <nmanglor@amazon.com>
1 parent 3ed6e5f commit 034d16a

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

src/object.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ static bool shouldEmbedStringObject(size_t val_len, const_sds key, long long exp
237237
}
238238
size += (expire != EXPIRY_NONE) * sizeof(long long);
239239
size += sdsReqSize(val_len, SDS_TYPE_8);
240-
return size <= 64;
240+
return size <= 128;
241241
}
242242

243243
/* Create a string object with EMBSTR encoding if it is small, otherwise RAW encoding */
@@ -274,7 +274,6 @@ void *objectGetVal(const robj *o) {
274274
data += 1 + hdr_size; /* +1 for header size byte */
275275
data += sdslen((const_sds)data) + 1; /* +1 for null terminator */
276276
}
277-
assert(o->encoding == OBJ_ENCODING_EMBSTR);
278277
return data + sdsHdrSize(SDS_TYPE_8);
279278
} else {
280279
return o->val_ptr;

src/unit/test_object.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,30 @@ TEST_F(ObjectTest, embedded_string_with_key) {
6161
sds key = sdsnew("k:123456789012345678901234567890");
6262
ASSERT_EQ(sdslen(key), 32u);
6363

64-
/* 32B key and 15B value should be embedded within 64B. Contents:
64+
/* 32B key and 79B value should be embedded within 128B. Contents:
6565
* - 8B robj (no ptr) + 1B key header size
6666
* - 3B key header + 32B key + 1B null terminator
67-
* - 3B val header + 15B val + 1B null terminator
67+
* - 3B val header + 79B val + 1B null terminator
6868
* because no pointers are stored, there is no difference for 32 bit builds*/
69-
const char *short_value = "123456789012345";
70-
ASSERT_EQ(strlen(short_value), 15u);
69+
const char *short_value = "1234567890123456789012345678901234567890123456789012345678901234567890123456789";
70+
ASSERT_EQ(strlen(short_value), 79u);
7171
robj *short_val_obj = createStringObject(short_value, strlen(short_value));
7272
robj *embstr_obj = objectSetKeyAndExpire(short_val_obj, key, -1);
7373
ASSERT_EQ(embstr_obj->encoding, (unsigned)OBJ_ENCODING_EMBSTR);
7474
ASSERT_EQ(sdslen(objectGetKey(embstr_obj)), 32u);
7575
ASSERT_EQ(sdscmp(objectGetKey(embstr_obj), key), 0);
76-
ASSERT_EQ(sdslen((sds)objectGetVal(embstr_obj)), 15u);
76+
ASSERT_EQ(sdslen((sds)objectGetVal(embstr_obj)), 79u);
7777
ASSERT_EQ(strcmp((const char *)objectGetVal(embstr_obj), short_value), 0);
7878

79-
/* value of length 16 cannot be embedded with other contents within 64B */
80-
const char *longer_value = "1234567890123456";
81-
ASSERT_EQ(strlen(longer_value), 16u);
79+
/* value of length 80 cannot be embedded with other contents within 128B */
80+
const char *longer_value = "12345678901234567890123456789012345678901234567890123456789012345678901234567890";
81+
ASSERT_EQ(strlen(longer_value), 80u);
8282
robj *longer_val_obj = createStringObject(longer_value, strlen(longer_value));
8383
robj *raw_obj = objectSetKeyAndExpire(longer_val_obj, key, -1);
8484
ASSERT_EQ(raw_obj->encoding, (unsigned)OBJ_ENCODING_RAW);
8585
ASSERT_EQ(sdslen(objectGetKey(raw_obj)), 32u);
8686
ASSERT_EQ(sdscmp(objectGetKey(raw_obj), key), 0);
87-
ASSERT_EQ(sdslen((sds)objectGetVal(raw_obj)), 16u);
87+
ASSERT_EQ(sdslen((sds)objectGetVal(raw_obj)), 80u);
8888
ASSERT_EQ(strcmp((const char *)objectGetVal(raw_obj), longer_value), 0);
8989

9090
sdsfree(key);
@@ -97,30 +97,30 @@ TEST_F(ObjectTest, embedded_string_with_key_and_expire) {
9797
sds key = sdsnew("k:123456789012345678901234567890");
9898
ASSERT_EQ(sdslen(key), 32u);
9999

100-
/* 32B key and 7B value should be embedded within 64B. Contents:
100+
/* 32B key and 71B value should be embedded within 128B. Contents:
101101
* - 8B robj (no ptr) + 8B expire + 1B key header size
102102
* - 3B key header + 32B key + 1B null terminator
103-
* - 3B val header + 7B val + 1B null terminator
103+
* - 3B val header + 71B val + 1B null terminator
104104
* because no pointers are stored, there is no difference for 32 bit builds*/
105-
const char *short_value = "1234567";
106-
ASSERT_EQ(strlen(short_value), 7u);
105+
const char *short_value = "12345678901234567890123456789012345678901234567890123456789012345678901";
106+
ASSERT_EQ(strlen(short_value), 71u);
107107
robj *short_val_obj = createStringObject(short_value, strlen(short_value));
108108
robj *embstr_obj = objectSetKeyAndExpire(short_val_obj, key, 128);
109109
ASSERT_EQ(embstr_obj->encoding, (unsigned)OBJ_ENCODING_EMBSTR);
110110
ASSERT_EQ(sdslen(objectGetKey(embstr_obj)), 32u);
111111
ASSERT_EQ(sdscmp(objectGetKey(embstr_obj), key), 0);
112-
ASSERT_EQ(sdslen((sds)objectGetVal(embstr_obj)), 7u);
112+
ASSERT_EQ(sdslen((sds)objectGetVal(embstr_obj)), 71u);
113113
ASSERT_EQ(strcmp((const char *)objectGetVal(embstr_obj), short_value), 0);
114114

115-
/* value of length 8 cannot be embedded with other contents within 64B */
116-
const char *longer_value = "12345678";
117-
ASSERT_EQ(strlen(longer_value), 8u);
115+
/* value of length 72 cannot be embedded with other contents within 128B */
116+
const char *longer_value = "123456789012345678901234567890123456789012345678901234567890123456789012";
117+
ASSERT_EQ(strlen(longer_value), 72u);
118118
robj *longer_val_obj = createStringObject(longer_value, strlen(longer_value));
119119
robj *raw_obj = objectSetKeyAndExpire(longer_val_obj, key, 128);
120120
ASSERT_EQ(raw_obj->encoding, (unsigned)OBJ_ENCODING_RAW);
121121
ASSERT_EQ(sdslen(objectGetKey(raw_obj)), 32u);
122122
ASSERT_EQ(sdscmp(objectGetKey(raw_obj), key), 0);
123-
ASSERT_EQ(sdslen((sds)objectGetVal(raw_obj)), 8u);
123+
ASSERT_EQ(sdslen((sds)objectGetVal(raw_obj)), 72u);
124124
ASSERT_EQ(strcmp((const char *)objectGetVal(raw_obj), longer_value), 0);
125125

126126
sdsfree(key);

0 commit comments

Comments
 (0)