antirez/redis — Issue #252: More judicious pre-allocation in sds.c
Author: antirez
Created: 2011-12-15 10:56:29+00:00
State: open
Labels: non critical bug

Currently sds.c will always double the size of the buffer needed for an append operation, so that asymptotically there are no allocation costs appending to an sds.c string. However when the string starts to get bigger and bigger this preallocation cost can be too large. This mostly affect Redis users storing very big string objects (or very big elements of an aggregate data type).

The proposed fix is to double the size of a string that is less than 1MB in size, and instead only pre-allocate 1MB ahead if the string is equal or more than 1MB in size:

```
@@ -116,7 +116,11 @@
if (free >= addlen) return s;
len = sdslen(s);
sh = (void*) (s-(sizeof(struct sdshdr)));
- newlen = (len+addlen)*2;
+ newlen = (len+addlen);
+ if (newlen < 1024*1024)
+ newlen *= 2;
+ else
+ newlen += 1024*1024;
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
#ifdef SDS_ABORT_ON_OOM
if (newsh == NULL) sdsOomAbort();
```