You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Small improvement to SETBIT and SETRANGE for avoiding wasted RAM #1918
I think that in the case of SETBIT and SETRANGE, with a small change to the code we can let the user save a lot of RAM without changing the API.
Currently, in both commands, if the key didn't pre-exist, an sds is created empty, and then a few lines later re-allocated to the right size (plus the overhead memory for future appends).
First, this allocation and then reallocation a few lines later is a waste.
But also, i wanna suggest that in case the key didn't pre-exist, we'll allocate the sds to exactly the right size without the SDS_MAX_PREALLOC overhead.
This will allow the user (if he's aware of that) to first set the last bit in his bit-field, and then set all the lower bits.
By doing this, he'll be able to avoid the overhead RAM consumption.
This is of course less friendly than changing the API and adding a new optional parameter, but i see no disadvantages in implementing my suggested change.
Either way when repeatedly appending more and more data to a key (e.g. setting the first bit and then the others), some operations will realloc and some won't (due to the extra allocated size).
So all that is changed is in case the user sets his bits from the first to the last, we'll do one more realloc after the first byte, which actually comes in place of the excess realloc at the creation time.
diff --git src/bitops.c src/bitops.c
index c49803f..fc189b4 100644
--- src/bitops.c+++ src/bitops.c@@ -217,19 +217,19 @@ void setbitCommand(redisClient *c) {
return;
}
+ byte = bitoffset >> 3;
o = lookupKeyWrite(c->db,c->argv[1]);
if (o == NULL) {
- o = createObject(REDIS_STRING,sdsempty());+ /* create an sds of the right size (with no overhead space reserved for future) */+ o = createObject(REDIS_STRING,sdsnewlen(NULL, byte+1));
dbAdd(c->db,c->argv[1],o);
} else {
if (checkType(c,o,REDIS_STRING)) return;
o = dbUnshareStringValue(c->db,c->argv[1],o);
+ /* Grow sds value to the right length if necessary + some overhead space reserved for future */+ o->ptr = sdsgrowzero(o->ptr,byte+1);
}
- /* Grow sds value to the right length if necessary */- byte = bitoffset >> 3;- o->ptr = sdsgrowzero(o->ptr,byte+1);-
/* Get current values */
byteval = ((uint8_t*)o->ptr)[byte];
bit = 7 - (bitoffset & 0x7);
diff --git src/t_string.c src/t_string.c
index 41e4b3b..41f6771
--- src/t_string.c+++ src/t_string.c@@ -194,7 +194,8 @@ void setrangeCommand(redisClient *c) {
if (checkStringLength(c,offset+sdslen(value)) != REDIS_OK)
return;
- o = createObject(REDIS_STRING,sdsempty());+ /* create an sds of the right size (with no overhead space reserved for future) */+ o = createObject(REDIS_STRING,sdsnewlen(NULL, offset+sdslen(value)));
dbAdd(c->db,c->argv[1],o);
} else {
size_t olen;
@@ -219,6 +220,7 @@ void setrangeCommand(redisClient *c) {
}
if (sdslen(value) > 0) {
+ /* Grow sds value to the right length if necessary + some overhead space reserved for future */
o->ptr = sdsgrowzero(o->ptr,offset+sdslen(value));
memcpy((char*)o->ptr+offset,value,sdslen(value));
signalModifiedKey(c->db,c->argv[1]);
Based on my initial testing of your change, we have:
[small offset] Zero difference in memory usage, but 2% faster than original: ./redis-benchmark -r 99999999999 -n 1000000 setbit __rand_int__ 17 1 (before and after patch, Redis used exactly 101.2 MB)
[larger offset] 42% less memory usage, and 6% faster than original: /redis-benchmark -r 99999999999 -n 1000000 setbit __rand_int__ 1700 1 (before patch: used_memory_human:512.92M; after patch: used_memory_human:299.40M)
[even larger offset] 44% less memory usage, and 8% faster than original: ./redis-benchmark -r 99999999999 -n 1000000 setbit __rand_int__ 17000 1 (before patch: used_memory_human:4.37G; after patch: used_memory_human:2.47G; also of note: the dump.rdb in this situation is a tiny 49 MB.)
BTW, i noticed that there are other places in redis with a similar inefficiency of allocating an empty string just in order to regrow it a few lines below.
e.g. there are two of them in networking.c (search for sdsMakeRoomFor)
ohh, sorry, i missed that, i thought you only took it to your repository.
is there anything holding it back? (AFAIK it has no downsides or side effects)...
Activity
oranagra commentedon Aug 7, 2014
Here's a patch, in case it helps...
mattsta commentedon Aug 7, 2014
Thanks for your refactor suggestion!
Based on my initial testing of your change, we have:
./redis-benchmark -r 99999999999 -n 1000000 setbit __rand_int__ 17 1(before and after patch, Redis used exactly 101.2 MB)/redis-benchmark -r 99999999999 -n 1000000 setbit __rand_int__ 1700 1(before patch:used_memory_human:512.92M; after patch:used_memory_human:299.40M)./redis-benchmark -r 99999999999 -n 1000000 setbit __rand_int__ 17000 1(before patch:used_memory_human:4.37G; after patch:used_memory_human:2.47G; also of note: the dump.rdb in this situation is a tiny 49 MB.)oranagra commentedon Oct 12, 2014
BTW, i noticed that there are other places in redis with a similar inefficiency of allocating an empty string just in order to regrow it a few lines below.
e.g. there are two of them in networking.c (search for sdsMakeRoomFor)
Bitops: Stop overallocating storage space on set
Bitops: Stop overallocating storage space on set
oranagra commentedon Feb 8, 2015
Hi,
I noticed this wasn't implemented yet in your main repository.
will it help if i make a pull request out of it?
mattsta commentedon Feb 8, 2015
It already is a pull request: https://github.com/antirez/redis/pull/2050
oranagra commentedon Feb 8, 2015
ohh, sorry, i missed that, i thought you only took it to your repository.
is there anything holding it back? (AFAIK it has no downsides or side effects)...