From 04056b767fbcdc1df6fc536467234c04d88bee9d Mon Sep 17 00:00:00 2001 From: sundb Date: Tue, 1 Dec 2020 05:03:53 +0800 Subject: [PATCH] BITOP speedup when or/and output is 0/255, stop processing further keys (#8110) when performing the and operation, if the output is 0, we can jump out of the loop. when performing an or operation, if the output is 0xff, we can jump out of the loop. --- src/bitops.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/bitops.c b/src/bitops.c index 5550b60d1..5e996679b 100644 --- a/src/bitops.c +++ b/src/bitops.c @@ -735,12 +735,23 @@ void bitopCommand(client *c) { output = (len[0] <= j) ? 0 : src[0][j]; if (op == BITOP_NOT) output = ~output; for (i = 1; i < numkeys; i++) { + int skip = 0; byte = (len[i] <= j) ? 0 : src[i][j]; switch(op) { - case BITOP_AND: output &= byte; break; - case BITOP_OR: output |= byte; break; + case BITOP_AND: + output &= byte; + skip = (output == 0); + break; + case BITOP_OR: + output |= byte; + skip = (output == 0xff); + break; case BITOP_XOR: output ^= byte; break; } + + if (skip) { + break; + } } res[j] = output; }