You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by otaviojava <gi...@git.apache.org> on 2018/11/20 12:02:59 UTC

[GitHub] tomee pull request #206: Uses Boolean.valueof instaed of new instance

GitHub user otaviojava opened a pull request:

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

    Uses Boolean.valueof instaed of new instance

    While I was studying the code, I found at SingletonEjbObjectHandler.java that they are creating a new Boolean instance instead of using the `Boolean.valueOf.` method.
    The boolean wrapper has a singleton instance to both true and false. However, it will use when the code uses the Boolean.valueOf method.
    
    Beyond the waste of memory, because they're creating a new `boolean` instance every time, it will be slower than the `valueOf` method.
    
    ```java
    @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
    @Measurement(iterations = 20, time = 1, timeUnit = TimeUnit.SECONDS)
    @Fork(3)
    @BenchmarkMode(Mode.AverageTime)
    @OutputTimeUnit(TimeUnit.NANOSECONDS)
    @State(Scope.Thread)
    public class FasterReflectionClientBenchmark {
    
        private ThreadLocalRandom random = ThreadLocalRandom.current();
    
        @Benchmark
        public Boolean newInstance() {
            return new Boolean(random.nextBoolean());
        }
    
        @Benchmark
        public Boolean valueOf() {
            return Boolean.valueOf(random.nextBoolean());
        }
    }
    ```
    
    |Benchmark|Mode|Cnt|Score| Obs|
    | ------------- |:------|-----|----:| ------:|
    |valueOf|      avgt|   60|  5,048| ---|
    |newInstance  |avgt|   60|  6,467|  (20% slower)|
    
    
    
    
    ps: The equals method returns a primitive value if we keep without the wrapper, it will automatically do a wrapper using the  `Boolean.valueOf.`

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

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

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

    https://github.com/apache/tomee/pull/206.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 #206
    
----
commit c520c456dad792dbc2fed65b509b358425e10ec7
Author: Otavio Santana <ot...@...>
Date:   2018-11-20T11:42:15Z

    Uses Boolean.valueof instaed of new instance

----


---

[GitHub] tomee pull request #206: Uses Boolean.valueof instaed of new instance

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

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


---