mirror of
http://github.com/valkey-io/valkey
synced 2024-11-22 18:04:52 +00:00
SDS: improve sdsRemoveFreeSpace() to avoid useless data copy.
Since SDS v2, we no longer have a single header, so the function to rewrite the SDS in terms of the minimum space required, instead of just using realloc() and let the underlying allocator decide what to do, was doing an allocation + copy every time the minimum possible header needed to represent the string was different than the current one. This could be often a bit wasteful, because if we go, for instance, from the 32 bit fields header to the 16 bit fields header, the overhead of the header is normally very small. With this commit we call realloc instead, unless the change in header size is very significant in relation to the string length.
This commit is contained in:
parent
1ee6af4d87
commit
897d857115
17
src/sds.c
17
src/sds.c
@ -248,16 +248,23 @@ sds sdsMakeRoomFor(sds s, size_t addlen) {
|
||||
sds sdsRemoveFreeSpace(sds s) {
|
||||
void *sh, *newsh;
|
||||
char type, oldtype = s[-1] & SDS_TYPE_MASK;
|
||||
int hdrlen;
|
||||
int hdrlen, oldhdrlen = sdsHdrSize(oldtype);
|
||||
size_t len = sdslen(s);
|
||||
sh = (char*)s-sdsHdrSize(oldtype);
|
||||
sh = (char*)s-oldhdrlen;
|
||||
|
||||
/* Check what would be the minimum SDS header that is just good enough to
|
||||
* fit this string. */
|
||||
type = sdsReqType(len);
|
||||
hdrlen = sdsHdrSize(type);
|
||||
if (oldtype==type) {
|
||||
newsh = s_realloc(sh, hdrlen+len+1);
|
||||
|
||||
/* If the type is the same, or at least a large enough type is still
|
||||
* required, we just realloc(), letting the allocator to do the copy
|
||||
* only if really needed. Otherwise if the change is huge, we manually
|
||||
* reallocate the string to use the different header type. */
|
||||
if (oldtype==type || type > SDS_TYPE_8) {
|
||||
newsh = s_realloc(sh, oldhdrlen+len+1);
|
||||
if (newsh == NULL) return NULL;
|
||||
s = (char*)newsh+hdrlen;
|
||||
s = (char*)newsh+oldhdrlen;
|
||||
} else {
|
||||
newsh = s_malloc(hdrlen+len+1);
|
||||
if (newsh == NULL) return NULL;
|
||||
|
Loading…
Reference in New Issue
Block a user