-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Optimizations for Node class (CPU time and Memory usage) #3233
Optimizations for Node class (CPU time and Memory usage) #3233
Conversation
A HashSet uses more memory that a List and we can achieve an identical behavior by checking if the observer is present in the List before adding it.
ArrayList is much more efficient in read operations than LinkedList making the process of iterating thru the list faster.
72f8889
to
bc536ee
Compare
Codecov Report
@@ Coverage Diff @@
## master #3233 +/- ##
===============================================
+ Coverage 57.336% 57.340% +0.003%
Complexity 2570 2570
===============================================
Files 616 616
Lines 34306 34309 +3
Branches 5802 5802
===============================================
+ Hits 19670 19673 +3
Misses 12667 12667
Partials 1969 1969
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Merged :-) |
Why replacing
LinkedList
withArrayList
?Lets take a look at the table below which compares
LinkedList
with theArrayList
.Comparison
Source: Performance Analysis of ArrayList and LinkedList in Java by Anant Mishra
There are some situations where is preferred to use the
LinkedList
. This happens when the list is updated more time than it's read.Benefits
In our case
LinkedList
isn't the best choice since the fields are read more time then they are written to (ex:childNodes
is read every time someone callsNode#findAll(...)
). In this scenario, theArrayList
fits better, since we can iterate it with a complexity of O(1) instead of O(N), reducing the CPU time required to execute the operation.Another benefit of using
ArrayList
instead ofLinkedList
is the use of less memory overhead.LinkedList
requires additional information, pointing where the next node is in memory while theArrayList
uses an sequential Array to store all the values.Downsides
The insertions of nodes in a tree, now has a complexity of O(N) instead of the previous O(1).
Why was
HashSet
replaced withArrayList
?The ideia behind of
Node#observers
is to keep record of the list of observers to be notified when something happens on the node. The problem with the use of theHashSet
in this scenario it uses a lot of memory and it's functionality is not properly used. AnHashSet
it worth to be used when we need to use the HashSet#contains() that has complexity oif O(1). In this case we are not taking any advantage of this case property, since the method that is used more often isHashSet#foreach
. In this case, we can replace the use of aHashSet
with anArrayList
that is more CPU and memory friendly.Why was
ArrayList#trimToSize()
called after removing items from the list?This call tries to reduce the memory footprint when an item is removed from a node. When the items is removed from the
ArrayList
the Array size is the same. If we imagine the Array has space allocated for 30 elements but only stores 2, we are wasting precious memory space. Calling this method makes sure that we are only using the space we really need.Evaluation
The evaluation was made using the tools VisualVM and JavaParser Symbol Solver tests. In this evaluation, the execution time was reduced the execution time by +- 30 seconds.
Current:
Proposed: