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.
This commit is contained in:
antirez 2014-10-03 11:53:57 +01:00
parent de10b8d9cc
commit 16559b4615

View File

@ -337,11 +337,21 @@ void incrDecrCommand(redisClient *c, long long incr) {
return; return;
} }
value += incr; value += incr;
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); new = createStringObjectFromLongLong(value);
if (o) if (o) {
dbOverwrite(c->db,c->argv[1],new); dbOverwrite(c->db,c->argv[1],new);
else } else {
dbAdd(c->db,c->argv[1],new); dbAdd(c->db,c->argv[1],new);
}
}
signalModifiedKey(c->db,c->argv[1]); signalModifiedKey(c->db,c->argv[1]);
notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"incrby",c->argv[1],c->db->id); notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"incrby",c->argv[1],c->db->id);
server.dirty++; server.dirty++;