Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Give up on storing unused allocation space in embedded SDS headers
Browse files Browse the repository at this point in the history
Signed-off-by: Viktor Söderqvist <[email protected]>
zuiderkwast committed Jan 27, 2025
1 parent 6ba6ea5 commit 8f2f234
Showing 3 changed files with 8 additions and 18 deletions.
11 changes: 4 additions & 7 deletions src/object.c
Original file line number Diff line number Diff line change
@@ -95,8 +95,7 @@ robj *createObjectWithKeyAndExpire(int type, void *ptr, const sds key, long long
/* Copy embedded key. */
if (o->hasembkey) {
*data++ = sdsHdrSize(key_sds_type);
size_t remaining_space = bufsize - (data - (char *)(void *)o);
sdswrite(data, remaining_space, key_sds_type, key, key_sds_len);
sdswrite(data, key_sds_size, key_sds_type, key, key_sds_len);
}

return o;
@@ -151,16 +150,15 @@ static robj *createEmbeddedStringObjectWithKeyAndExpire(const char *val_ptr,
size_t key_sds_len = key != NULL ? sdslen(key) : 0;
char key_sds_type = key != NULL ? sdsReqType(key_sds_len) : 0;
size_t key_sds_size = key != NULL ? sdsReqSize(key_sds_len, key_sds_type) : 0;
size_t min_size = sizeof(robj);
size_t val_sds_size = sdsReqSize(val_len, SDS_TYPE_8);
size_t min_size = sizeof(robj) + val_sds_size;
if (expire != -1) {
min_size += sizeof(long long);
}
if (key != NULL) {
/* Size of embedded key, incl. 1 byte for prefixed sds hdr size. */
min_size += 1 + key_sds_size;
}
/* Size of embedded value (EMBSTR) including \0 term. */
min_size += sdsReqSize(val_len, SDS_TYPE_8);

/* Allocate and set the declared fields. */
size_t bufsize = 0;
@@ -196,8 +194,7 @@ static robj *createEmbeddedStringObjectWithKeyAndExpire(const char *val_ptr,
}

/* Copy embedded value (EMBSTR). */
size_t remaining_space = bufsize - (data - (char *)(void *)o);
o->ptr = sdswrite(data, remaining_space, SDS_TYPE_8, val_ptr, val_len);
o->ptr = sdswrite(data, val_sds_size, SDS_TYPE_8, val_ptr, val_len);

return o;
}
8 changes: 4 additions & 4 deletions src/sds.c
Original file line number Diff line number Diff line change
@@ -113,18 +113,18 @@ sds _sdsnewlen(const void *init, size_t initlen, int trymalloc) {
: s_malloc_usable(hdrlen + initlen + 1, &bufsize);
if (sh == NULL) return NULL;

adjustTypeIfNeeded(&type, &hdrlen, bufsize);
return sdswrite(sh, bufsize, type, init, initlen);
}

/* Writes an sds with type `type` into a buffer `buf` of size `bufsize`. Returns
* an sds handle to the string within the buffer. Use `sdsReqType(len)` to
* an sds handle to the string within the buffer. Use `sdsReqType(length)` to
* compute the type and `sdsReqSize(length, type)` to compute the required
* buffer size. You can use a larger `bufsize` than required, but usable size
* can't be greater than `sdsTypeMaxSize(type)`. */
sds sdswrite(char *buf, size_t bufsize, char type, const char *init, size_t initlen) {
int hdrlen = sdsHdrSize(type);
adjustTypeIfNeeded(&type, &hdrlen, bufsize);
assert(bufsize >= sdsReqSize(initlen, type));
int hdrlen = sdsHdrSize(type);
size_t usable = bufsize - hdrlen - 1;
sds s = buf + hdrlen;
unsigned char *fp = ((unsigned char *)s) - 1; /* flags pointer. */
@@ -429,7 +429,7 @@ size_t sdsAllocSize(const_sds s) {
if (type == SDS_TYPE_5) {
return s_malloc_usable_size(sdsAllocPtr(s));
} else {
return sdsMemUsage(s);
return sdsHdrSize(type) + sdsalloc(s) + 1;
}
}

7 changes: 0 additions & 7 deletions src/sds.h
Original file line number Diff line number Diff line change
@@ -247,13 +247,6 @@ sds sdsResize(sds s, size_t size, int would_regrow);
size_t sdsAllocSize(const_sds s);
void *sdsAllocPtr(const_sds s);

/* Returns the size of the sds representation in memory. Typically, this is the
* same as the allocation size, except for sds type 5 and for sds strings
* embedded in other objects. */
static inline size_t sdsMemUsage(const_sds s) {
return sdsHdrSize(sdsType(s)) + sdsalloc(s) + 1;
}

/* Returns the minimum required size to store an sds string of the given length
* and type. */
static inline size_t sdsReqSize(size_t len, char type) {

0 comments on commit 8f2f234

Please sign in to comment.