GitHub user otaviojava opened a pull request:

    https://github.com/apache/tomee/pull/235

    Improve performance using Collections in TomEE container

    This PR has the Goal to improve performance and save memory on the 
collections code. To get this goal, we went to different improvements:
    
    
    1) Use the Constructor in the collection instead of add
    
    ```java
    Set<String> set = new HashSet<>();
    set.addAll(Arrays.asList("alpha", "beta", "gamma"));
    ```
    ```java
    Set<String> set = new HashSet<>(Arrays.asList("alpha", "beta", "gamma"));
    ```
    
     These constructs method is replaced with a single call to a parametrized 
constructor which simplifies the code. Also for more performant such as 
[HashSet](https://github.com/dmlloyd/openjdk/blob/jdk/jdk/src/java.base/share/classes/java/util/HashSet.java#L119).
    
    2) Use the addAll instead of add for interactions
    
    Replaces add method to calling a bulk method (e.g. 
collection.addAll(listOfX). This will produce improvements in the code. Such as 
[ArrayList](https://github.com/dmlloyd/openjdk/blob/jdk/jdk/src/java.base/share/classes/java/util/ArrayList.java#L700)
 and 
[HashMap](https://github.com/dmlloyd/openjdk/blob/jdk/jdk/src/java.base/share/classes/java/util/HashMap.java#L495)
    
    3) Replace Arrays with one element to Collections.singletonList() which 
will save some memory.
    
    4) Uses EnumSet instead of HashSet
    
    From the 
[documentation](https://docs.oracle.com/javase/8/docs/api/java/util/EnumSet.html)
 that says: 
    > A specialized Set implementation for use with enum types. All of the 
elements in an enum set must come from a single enum type that is specified, 
explicitly or implicitly, when the set is created. Enum sets are represented 
internally as bit vectors. This representation is extremely compact and 
efficient. The space and time performance of this class should be good enough 
to allow its use as a high-quality, typesafe alternative to traditional 
int-based "bit flags." Even bulk operations (such as containsAll and retainAll) 
should run very quickly if their argument is also an enum set.
    
    5) Uses the entrySet method:
    
    Replaces interaction over the keySet() of a java.util.Map instance, where 
the iterated keys are used to retrieve the values from the map. Such iteration 
may be more efficiently replaced by iteration over the entrySet() of the map. 
The 
[Map.Entry](https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html) 
is an entry within Map, that avoid the get method several times once the key 
and the value are in the object.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/otaviojava/tomee improve_collections

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/tomee/pull/235.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #235
    
----

----


---

Reply via email to