From badf0f008bede268d3235412243fc62d618e323c Mon Sep 17 00:00:00 2001 From: Matt Stancliff Date: Thu, 7 Aug 2014 09:43:16 -0400 Subject: Bitops: Stop overallocating storage space on set Previously the string was created empty then re-sized to fit the offset, but sds resize causes the sds to over-allocate by at least 1 MB (which is a lot when you are operating at bit-level access). This also improves the speed of initial sets by 2% to 6% based on quick testing. Patch logic provided by @oranagra Fixes #1918 --- src/bitops.c | 8 +++----- src/t_string.c | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/bitops.c b/src/bitops.c index 4c8662244..ec912bc24 100644 --- a/src/bitops.c +++ b/src/bitops.c @@ -229,19 +229,17 @@ void setbitCommand(redisClient *c) { return; } + byte = bitoffset >> 3; o = lookupKeyWrite(c->db,c->argv[1]); if (o == NULL) { - o = createObject(REDIS_STRING,sdsempty()); + 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); + 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 a/src/t_string.c b/src/t_string.c index e3c1e5f4a..b61589961 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -194,7 +194,7 @@ void setrangeCommand(redisClient *c) { if (checkStringLength(c,offset+sdslen(value)) != REDIS_OK) return; - o = createObject(REDIS_STRING,sdsempty()); + o = createObject(REDIS_STRING,sdsnewlen(NULL, offset+sdslen(value))); dbAdd(c->db,c->argv[1],o); } else { size_t olen; -- cgit v1.2.1