-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Inefficient Usage of Java Collection #1079
Conversation
Transform LinkedHashMap to HashMap
Transform LinkedHashMap to HashMap
Transform LinkedHashMap to HashMap
Transform LinkedHashMap to HashMap
Transform LinkedHashSet to HashSet
Transform LinkedHashSet to HashSet
Transform ArrayList to LinkedList
Transform ArrayList to LinkedList
Transform ArrayList to LinkedList
Transform ArrayList to LinkedList
Transform ArrayList to LinkedList
Hi @DittoTool, welcome to SOFAStack community, Please sign Contributor License Agreement! After you signed CLA, we will automatically sync the status of this pull request in 3 minutes. |
Codecov Report
@@ Coverage Diff @@
## master #1079 +/- ##
============================================
- Coverage 68.93% 68.93% -0.01%
- Complexity 825 826 +1
============================================
Files 409 409
Lines 17693 17693
Branches 2744 2744
============================================
- Hits 12197 12196 -1
- Misses 4079 4081 +2
+ Partials 1417 1416 -1
Continue to review full report at Codecov.
|
Thank you for your PR, we will check the changes later. |
@DittoTool Where can I get the Ditto tool? |
Currently, it is a commercial tool. It might be open-sourced later.:) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thank you for confirmation :) |
please see quarkusio/quarkus#19962 (comment)
|
I think there exists a gap between the design and implementation of these collections. The new version of JVM might introduce some optimizations upon ArrayList. However, from the complexity perspective, LinkedList is still better than ArrayList in some cases. Otherwise, why LinkedList still exists in JCF? Ditto receives the complexity specification as its input and is unaware of the JVM optimization. On the one hand, the result only suggests the potential inefficient usage. Also, dynamic profiling statistics can be introduced to make the recommendation more precise. On the other hand, we just take the complexity specification as our assumption. If we adopt the implementation in the real JVM, we can also recommend replacing the LinkedList objects with ArrayList objects in the program. Of course, such changes might not make big difference. The project sofa-rpc does not have serve cases of inefficient container usage. In the patch, we only mention three kinds of transformations: ArrayList->LinkedList, LinkedHashMap->HashMap, and LinkedHashSet->HashSet. The three patterns actually have little influence on complexity. However, in other cases, for example, a list can be used as a set, so Ditto will recommend replacing a list object with a HashSet object. The contains methods have totally different complexities in list and set. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Hi,
We find that there are several ArrayList objects which are not manipulated by random access. Due to the memory reallocation triggered in the successive insertions, the time complexity of add method of ArrayList is amortized O(1). We notice that these objects are only used for traversal, the retrieval, and removal of the first or the last element.
This functionality can be implemented by LinkedList. Moreover, the insertion of LinkedList is strictly O(1) time complexity because no memory reallocation occurs.
Meanwhile, we also found several LinkedHashSet and LinkedHashMap objects which are not necessary to maintain the order of insertions. To achieve the same functionality, HashHap and HashSet are enough. The replacement can also reduce the time cost in the modification of the map objects.
We discovered the above inefficient usage of containers by our tool Ditto. The patch is submitted. Could you please check and accept it? We have tested the patch on our PC. The patched program works well.
Bests
Ditto