From 16559b461521625271430d7d4926b632f3a326dd Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 3 Oct 2014 11:53:57 +0100 Subject: [PATCH] INCR: Modify incremented object in-place when possible. However we don't try to do this if the integer is already inside a range representable with a shared integer. The performance gain appears to be around ~15% in micro benchmarks, however in the long run this also helps to improve locality, so should have more, hard to measure, benefits. --- src/t_string.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/t_string.c b/src/t_string.c index f0669381..74c05793 100644 --- a/src/t_string.c +++ b/src/t_string.c @@ -337,11 +337,21 @@ void incrDecrCommand(redisClient *c, long long incr) { return; } value += incr; - new = createStringObjectFromLongLong(value); - if (o) - dbOverwrite(c->db,c->argv[1],new); - else - dbAdd(c->db,c->argv[1],new); + + if (o && o->refcount == 1 && o->encoding == REDIS_ENCODING_INT && + (value < 0 || value >= REDIS_SHARED_INTEGERS) && + value >= LONG_MIN && value <= LONG_MAX) + { + new = o; + o->ptr = (void*)((long)value); + } else { + new = createStringObjectFromLongLong(value); + if (o) { + dbOverwrite(c->db,c->argv[1],new); + } else { + dbAdd(c->db,c->argv[1],new); + } + } signalModifiedKey(c->db,c->argv[1]); notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"incrby",c->argv[1],c->db->id); server.dirty++;