On Fri, 1 Jul 2022 18:32:18 GMT, Peter Levart <plev...@openjdk.org> wrote:
>> @plevart I've checked it with and without `@stable`, it's the same: >> >> with >> Benchmark Mode Cnt Score Error Units >> AccessParamsBenchmark.getParameter0 avgt 50 6,196 ± 0,142 ns/op >> AccessParamsBenchmark.getParameters avgt 50 4,636 ± 0,075 ns/op >> >> without >> Benchmark Mode Cnt Score Error Units >> AccessParamsBenchmark.getParameter0 avgt 50 6,196 ± 0,142 ns/op >> AccessParamsBenchmark.getParameters avgt 50 4,636 ± 0,075 ns/op >> >> This seems logical as effects of `@Stable` aren not propagated into cloned >> array. >> This is the benchmark I used: >> >> @State(Scope.Thread) >> @BenchmarkMode(Mode.AverageTime) >> @OutputTimeUnit(TimeUnit.NANOSECONDS) >> @Fork(jvmArgsAppend = {"-Xms1g", "-Xmx1g"}) >> public class AccessParamsBenchmark { >> >> private final Method method; >> >> public AccessParamsBenchmark() { >> try { >> method = getClass().getMethod("foo", int.class, String.class); >> } catch (NoSuchMethodException e) { >> throw new RuntimeException(e); >> } >> } >> >> @Benchmark >> public Object getParameters() { >> return method.getParameters(); >> } >> >> @Benchmark >> public Object getParameter0() { >> return method.getParameters()[0]; >> } >> >> public void foo(int parameter1, String parameter2) { >> } >> } >> >> >> So, should we rid the annotation from `parameters` field? > > @Stable is only effective if the path leading to @Stable value can be > constant-folded by JIT. In above test, you have an instance field Method > method. This can not be constant-folded, so neither can @stable fiels in the > Field object, nor array elements of a @stable array. You should replace that > with "static final Method method = ..." and re-run the test. With `static` and without `@Stable` the benchmark yields Benchmark Mode Cnt Score Error Units AccessParamsBenchmark.getParameter0 avgt 10 1,212 ± 0,083 ns/op AccessParamsBenchmark.getParameters avgt 10 2,493 ± 0,076 ns/op and with `@Stable` Benchmark Mode Cnt Score Error Units AccessParamsBenchmark.getParameter0 avgt 40 0,427 ± 0,007 ns/op AccessParamsBenchmark.getParameters avgt 40 2,123 ± 0,052 ns/op so the annotation is useful. ------------- PR: https://git.openjdk.org/jdk/pull/9143