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