ahuazhu commented on a change in pull request #1267: 采用java8 hashmap的散列算法
hashcode右移16位后与hashcode异或 散列效果更好
URL: https://github.com/apache/rocketmq/pull/1267#discussion_r294203051
##########
File path:
client/src/main/java/org/apache/rocketmq/client/producer/selector/SelectMessageQueueByHash.java
##########
@@ -25,12 +26,12 @@
@Override
public MessageQueue select(List<MessageQueue> mqs, Message msg, Object
arg) {
- int value = arg.hashCode();
- if (value < 0) {
- value = Math.abs(value);
- }
+ return mqs.get(indexForQueue(mqs.size(), arg));
+ }
- value = value % mqs.size();
- return mqs.get(value);
+ private int indexForQueue(int length, Object key) {
+ int h = (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
+ return h & (length - 1);
Review comment:
Maybe a bug :-)
When mqs.size() is not power of 2, some queue will never be selected.
for example. when mqs.size() is 5, only queue-0 and queue-4 could be
selected.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services