You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2021/11/29 14:03:36 UTC

[GitHub] [rocketmq] XiaoyiPeng commented on pull request #3534: [ISSUE #3533] optimize: It is better to specify the initialCapacity when initializing a HashMap as a local variable that will not add elements in the future.

XiaoyiPeng commented on pull request #3534:
URL: https://github.com/apache/rocketmq/pull/3534#issuecomment-981662904


   > In my opinion, setting loadFactor=1 is not a good way, the default value can satisfy most scenarios.
   
   Here is using the style of the previous version code(like `org.apache.rocketmq.broker.filter.MessageEvaluationContext#keyValues()` ),as shown below:
   ```java
   public Map<String, Object> keyValues() {
           if (properties == null) {
               return null;
           }
   
           Map<String, Object> copy = new HashMap<String, Object>(properties.size(), 1);
   
           for (String key : properties.keySet()) {
               copy.put(key, properties.get(key));
           }
   
           return copy;
       }
   ```
   
    because we know exactly how many elements this map is going to put in, and we don't want  the map be expansioned(`resize`).
   
   


-- 
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.

To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org