You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by Otávio Gonçalves de Santana <os...@tomitribe.com> on 2018/11/20 12:03:18 UTC
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.
@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.
PR created: https://github.com/apache/tomee/pull/206
Re: Uses Boolean.valueof instaed of new instance
Posted by Jonathan Gallimore <jo...@gmail.com>.
Looks like the //NOPMD isn't needed after this change, so I've removed it.
Thanks for the patch.
Cheers
Jon
On Tue, Nov 20, 2018 at 5:02 PM Jonathan Gallimore <
jonathan.gallimore@gmail.com> wrote:
> Interesting. I'm curious about the //NOPMD. Has anyone tried removing that
> and seeing what the warning is?
>
> Jon
>
> On Tue, Nov 20, 2018 at 12:12 PM Bruno Baptista <br...@gmail.com>
> wrote:
>
>> Hi Otávio,
>>
>> I think that makes sense, It looks like It's the only place in the code
>> where we have that.
>>
>> Cheers
>>
>> Bruno Baptista
>> http://twitter.com/brunobat_
>>
>>
>> On 20/11/18 12:03, Otávio Gonçalves de Santana wrote:
>> > 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.
>> >
>> > @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.
>> > PR created: https://github.com/apache/tomee/pull/206
>> >
>>
>
Re: Uses Boolean.valueof instaed of new instance
Posted by Jonathan Gallimore <jo...@gmail.com>.
Interesting. I'm curious about the //NOPMD. Has anyone tried removing that
and seeing what the warning is?
Jon
On Tue, Nov 20, 2018 at 12:12 PM Bruno Baptista <br...@gmail.com> wrote:
> Hi Otávio,
>
> I think that makes sense, It looks like It's the only place in the code
> where we have that.
>
> Cheers
>
> Bruno Baptista
> http://twitter.com/brunobat_
>
>
> On 20/11/18 12:03, Otávio Gonçalves de Santana wrote:
> > 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.
> >
> > @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.
> > PR created: https://github.com/apache/tomee/pull/206
> >
>
Re: Uses Boolean.valueof instaed of new instance
Posted by Bruno Baptista <br...@gmail.com>.
Hi Otávio,
I think that makes sense, It looks like It's the only place in the code
where we have that.
Cheers
Bruno Baptista
http://twitter.com/brunobat_
On 20/11/18 12:03, Otávio Gonçalves de Santana wrote:
> 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.
>
> @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.
> PR created: https://github.com/apache/tomee/pull/206
>