Proposed API for JEP 259: Stack-Walking API

Peter Levart peter.levart at gmail.com
Sat Oct 31 19:23:14 UTC 2015



On 10/31/2015 07:29 PM, Remi Forax wrote:
>   also instead of Optional.orElse, orElseGet is better because it avoids to evaluate
>   Thread.currentThread().getClass() if not necessary.
>
>   So the example should be:
>     walk(s -> s.map(StackFrame::getDeclaringClass)
>                .findFirst()).orElseGet(() -> Thread.currentThread().getClass());

It might be hard to believe, but the evaluation of constant lambda 
expression is approx. equally expensive as the expression it 
encapsulates in this example, so it really is an overkill here:

Benchmark                                                 Mode  Cnt 
Score   Error  Units
LambdaVsCurrentThreadBench.getCurrentThreadClass          avgt   10 
2.202 ± 0.058  ns/op
LambdaVsCurrentThreadBench.getCurrentThreadClassSupplier  avgt   10 
2.196 ± 0.159  ns/op


@BenchmarkMode(Mode.AverageTime)
@Fork(1)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class LambdaVsCurrentThreadBench {
     @Benchmark
     public Class<?> getCurrentThreadClass() {
         return Thread.currentThread().getClass();
     }

     @Benchmark
     public Supplier<Class<?>> getCurrentThreadClassSupplier() {
         return () -> Thread.currentThread().getClass();
     }
}



Regards, Peter




More information about the core-libs-dev mailing list