On Fri, 1 Jul 2022 12:26:04 GMT, Сергей Цыпанов <[email protected]> wrote:
>> ... I can only see the array being cloned and not accessed directly. I don't
>> belive cloning a @stable array is any different in JIT-ed code as cloning
>> normal "mutable" array unless JIT "sees" through it and scalarizes the
>> values of the cloned array. For example, if you have the following:
>>
>>
>> static final Method method = ....;
>>
>> @Benchmark
>> public Object getParameter0() {
>> return method.getParameters()[0];
>> }
>>
>>
>> ...would it run faster when the parameters field was marked as @stable as
>> opposed to not?
>
> @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.
-------------
PR: https://git.openjdk.org/jdk/pull/9143