Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve several map iteration #2938

Merged
merged 1 commit into from
Dec 12, 2018
Merged

Conversation

biyuhao
Copy link
Member

@biyuhao biyuhao commented Dec 11, 2018

What is the purpose of the change

Improve several places of map iteration

In JavaBeanSerializeUtil.java, Menu.java, DefaultFuture.java and several Test cases,
we have some code similar to

for (Key key : map.KeySet()) {
  Value value = map.get(key);
  ......
}

This pr will replace it using entrySet()

Brief changelog

source code and test code updated

Verifying this change

ut passwd

Follow this checklist to help us incorporate your contribution quickly and easily:

  • Make sure there is a GITHUB_issue field for the change (usually before you start working on it). Trivial changes like typos do not require a GITHUB issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [Dubbo-XXX] Fix UnknownException when host config not exist #XXX. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in test module.
  • Run mvn clean install -DskipTests & mvn clean test-compile failsafe:integration-test to make sure unit-test and integration-test pass.
  • If this contribution is large, please follow the Software Donation Guide.

@codecov-io
Copy link

Codecov Report

Merging #2938 into master will decrease coverage by 0.01%.
The diff coverage is 60%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2938      +/-   ##
==========================================
- Coverage   63.73%   63.72%   -0.02%     
==========================================
  Files         578      578              
  Lines       25959    25958       -1     
  Branches     4544     4542       -2     
==========================================
- Hits        16546    16541       -5     
- Misses       7241     7244       +3     
- Partials     2172     2173       +1
Impacted Files Coverage Δ
...e/dubbo/common/beanutil/JavaBeanSerializeUtil.java 79.31% <100%> (-0.08%) ⬇️
...dubbo/remoting/exchange/support/DefaultFuture.java 67.78% <33.33%> (ø) ⬆️
.../remoting/transport/netty4/NettyClientHandler.java 75% <0%> (-11.12%) ⬇️
...in/java/org/apache/dubbo/common/utils/JVMUtil.java 73.58% <0%> (-7.55%) ⬇️
.../apache/dubbo/remoting/transport/AbstractPeer.java 63.04% <0%> (-4.35%) ⬇️
...he/dubbo/remoting/transport/netty/NettyServer.java 67.85% <0%> (-3.58%) ⬇️
...rg/apache/dubbo/common/timer/HashedWheelTimer.java 58.88% <0%> (-1.75%) ⬇️
...he/dubbo/registry/multicast/MulticastRegistry.java 65.51% <0%> (+1.72%) ⬆️
...rpc/protocol/dubbo/telnet/InvokeTelnetHandler.java 57.95% <0%> (+3.4%) ⬆️
...e/dubbo/remoting/transport/netty/NettyChannel.java 62.35% <0%> (+4.7%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 795a840...768023e. Read the comment docs.

@beiwei30
Copy link
Member

@biyuhao, In my own opinion, it's nothing but code style. Both ways look good to me. Would you mind shine some light on me why your proposed way is better?

@biyuhao
Copy link
Member Author

biyuhao commented Dec 11, 2018

@beiwei30
I think this makes us a more unified code style and gain a better performance.
I did a little google search and got some betters answers than mine.

  1. "entrySet()" should be iterated when both the key and value are needed

When only the keys from a map are needed in a loop, iterating the keySet makes sense. But when both the key and the value are needed, it's more efficient to iterate the entrySet, which will give access to both the key and value, instead.

  1. Performance Comparison of Different Ways to Iterate over HashMap

Using entrySet() in for-each loop : 50
Using keySet() in for-each loop : 76
Using entrySet() and iterator : 50
Using keySet() and iterator : 75

  1. performance-considerations-for-keyset-and-entryset-of-map

Here you first need to get hold of the set of keys. This takes time proportional to the capacity of the map (as opposed to size for the LinkedHashMap). After this is done, you call get() once for each key. Sure, in the average case, with a good hashCode-implementation this takes constant time. However, it will inevitably require lots of .hashCode and .equals calls, which will obviously take more time than just doing a entry.value() call.

At last, I have not done such a profiling. So I think it's more about unifying code style here.
This is a trivial change, it's on your call. 😃

@beiwei30 beiwei30 merged commit 227a169 into apache:master Dec 12, 2018
@biyuhao biyuhao deleted the improve-map-iteration branch December 12, 2018 06:39
khanimteyaz pushed a commit to khanimteyaz/incubator-dubbo that referenced this pull request Dec 17, 2018
beiwei30 pushed a commit that referenced this pull request Dec 17, 2018
* added dubbo-rpc-api filter documentation for issue no #2935

* wrong @see java.io.File was added, removed this version of checkins

* Close all ports after tests finish (#2906)

* fix testCustomExecutor (#2904)

* Graceful shutdown enhancement in Spring (#2901)

* Simplify the code logic of the method AbstractClusterInvoker#reselect. (#2826)

* Simplify the code logic of the method AbstractClusterInvoker#reselect.

* Minor modification for code style.

* create AbstractRouter (#2909)

* create AbstractRouter

* router default method

* router default method

* router default method

* mockinvoker

* Added javadoc for dubbo-filter module dubbo github issue 2884 (#2921)

* Enhance unit test (#2920)

* Change Readme dubbo-sample hyperlink (#2927)

* Simply TagRouter (#2924)

* make telnet config work again (#2925)

* Remove the log to putRandomPort when one protocol use random port (#2931)

* optimize findConfigedPorts method of ServiceConfig to log only one time when userandom port

* move the log to method putRandomPort

* Fix DubboShutdownHook Memory Leak (#2922)

* Improve UT grammar and remove unnecessary braces. (#2930)

* Improve UT grammer, fix compiler warnings.

* Remove unnecessary braces.

* re-enable testCustomExecutor  (#2917)

* fix testCustomExecutor

* fix ci

* Fixing test-order dependency for FstObjectInputTest (#2815)

* re-enable testCustomExecutor (#2913)

* Resetting ExtensionLoader to remove test order dependencies in StickyTest (#2807)

* optimize the RondRobinLoadBalance and MockClusterInvoker (#2932)

delete unused logic and take the logger out.

* [Dubbo-2864] Fix build failed with -Prelease (#2923)

fixes #2864

* Fix telnet can not find method with enum type (#2803)

* [dubbo-2766] fix the bug of isMatch method of InvokeTelnetHandler (#2787)

* enhance org.apache.dubbo.rpc.protocol.dubbo.telnet.InvokeTelnetHandler#isMatch (#2941)

* enhance isMatch

* remove useless imports

* [Dubbo-2766]Fix 2766 and enhance the invoke command (#2801)

* add getter and setter for ServiceConfig's interfaceName property#2353

* add interfaceName to ignoreAttributeNames and change the unit test

* delete the demo source code and update the unit test

* unchange ServiceConfig

* update unit test

* update unit test

* fix #2798 and enhance invoke command

* Delete useless assignments (#2939)

* Replace anonymous class with method reference (#2929)

* Replace anonymous class with method reference

* Revert changes as per @beiwei30 code review

* Optimize retry for FailbackRegistry. (#2763)

* Abstract retry task

* Task for retry.

* Fix sth.

* Finish Optimize. fix ci failed.

* Optimize retry for FailbackRegistry.
The retry operation splits into specific operations, such as subscriptions and registrations. This approach allows for very precise retry control.

* Optimize retry for FailbackRegistry.
The retry operation splits into specific operations, such as subscriptions and registrations. This approach allows for very precise retry control.

* Optimize logger warn's msg.

* Optimize FailedNotifiedTask's run method.
Optimize addXXXTask, directly return if we already have a retry task.

* Optimize notify logic, just notify when the urls is not empty.

* Optimize notify logic, just notify when the urls is not empty.

* Optimize timer that use daemon thread.

* standardize semantics of all mergers,enhance mergeFactory and testcase (#2936)

* Modified to lower camel case (#2945)

* Improve several map iteration (#2938)

* added dubbo-rpc-api filter documentation for issue no #2935

* wrong @see java.io.File was added, removed this version of checkins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants