-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize CRC16 calculation (with optimization of slot decision)
- Loading branch information
1 parent
9b54744
commit 87f9938
Showing
2 changed files
with
10 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* CRC16 Implementation according to CCITT standard | ||
* Polynomial : 1021 (x^16 + x^12 + x^5 + 1) | ||
* | ||
* @see: http://redis.io/topics/cluster-spec | ||
* @see http://redis.io/topics/cluster-spec | ||
* Appendix A. CRC16 reference implementation in ANSI C | ||
*/ | ||
public class JedisClusterCRC16 { | ||
|
@@ -36,22 +36,24 @@ public static int getSlot(String key) { | |
key = key.substring(s+1, e); | ||
} | ||
} | ||
return getCRC16(key) % 16384; | ||
// optimization with modulo operator with power of 2 | ||
// equivalent to getCRC16(key) % 16384 | ||
return getCRC16(key) & (16384 - 1); | ||
} | ||
|
||
/** | ||
* Create a CRC16 checksum from the bytes. | ||
* implementation is from mp911de/lettuce | ||
* implementation is from mp911de/lettuce, modified with some more optimizations | ||
* | ||
* @author <a href="mailto:[email protected]">Mark Paluch</a> | ||
* @param bytes | ||
* @return CRC16 as integer value | ||
* @see https://github.com/xetorthio/jedis/pull/733#issuecomment-55840331 | ||
*/ | ||
public static int getCRC16(byte[] bytes) { | ||
int crc = 0x0000; | ||
|
||
for (byte b : bytes) { | ||
crc = ((crc << 8) ^ LOOKUP_TABLE[((crc >> 8) ^ (b & 0xFF)) & 0xFF]) & 0xFFFF; | ||
crc = ((crc << 8) ^ LOOKUP_TABLE[((crc >>> 8) ^ (b & 0xFF)) & 0xFF]); | ||
} | ||
return crc & 0xFFFF; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters