Skip to content

Small improvement to SETBIT and SETRANGE for avoiding wasted RAM #1918

Closed

Description

@oranagra

As discussed in ticket https://github.com/antirez/redis/issues/548:
Normal SET operations only allocate the required memory, but Append operations allocate some extra memory to reduce the need for reallocation in later Append operations.

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.

Activity

  1. oranagra commented on Aug 7, 2014

    @oranagra
    MemberAuthor

    Here's a patch, in case it helps...

    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]); 
  2. mattsta commented on Aug 7, 2014

    @mattsta
    Contributor

    Thanks for your refactor suggestion!

    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.)
  3. oranagra commented on Oct 12, 2014

    @oranagra
    MemberAuthor

    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)

  4. added a commit that references this issue on Oct 29, 2014
    27cb61d
  5. added a commit that references this issue on Dec 11, 2014
    badf0f0
  6. oranagra commented on Feb 8, 2015

    @oranagra
    MemberAuthor

    Hi,
    I noticed this wasn't implemented yet in your main repository.
    will it help if i make a pull request out of it?

  7. mattsta commented on Feb 8, 2015

    @mattsta
    Contributor
  8. oranagra commented on Feb 8, 2015

    @oranagra
    MemberAuthor

    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)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions