1 /*
 2  * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package org.openjdk.bench.valhalla.field.set;
24 
25 import org.openjdk.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.CompilerControl;
27 import org.openjdk.jmh.annotations.Fork;
28 import org.openjdk.jmh.annotations.Setup;
29 
30 @Fork(value = 3, jvmArgsAppend = {"--enable-preview"})
31 public class Value032 extends SetBase {
32 
33     public static class ValWrapper {
34         public ValueInt f;
35 
36         public ValWrapper(ValueInt f) {
37             this.f = f;
38         }
39     }
40 
41     public interface InterfaceInt {
42         public int value();
43     }
44 
45     public static value class ValueInt implements InterfaceInt {
46         public final int value;
47         public ValueInt(int value) {
48             this.value = value;
49         }
50         public int value() {
51             return value;
52         }
53     }
54 
55 
56     public static class RefState extends SizeState {
57         public ValWrapper[] arr;
58 
59         @Setup
60         public void setup() {
61             arr = new ValWrapper[size];
62             for (int i = 0; i < size; i++) {
63                 arr[i] = new ValWrapper(new ValueInt(i));
64             }
65         }
66     }
67 
68     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
69     public ValueInt get_val(int i) {
70         return new ValueInt(i);
71     }
72 
73     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
74     public void set_new(ValWrapper[] dst) {
75         for (int i = 0; i < dst.length; i++) {
76             dst[i].f = new ValueInt(i);
77         }
78     }
79 
80     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
81     public void set_call(ValWrapper[] dst) {
82         for (int i = 0; i < dst.length; i++) {
83             dst[i].f = get_val(i);
84         }
85     }
86 
87     @Benchmark
88     public void set_new_val(RefState st1) {
89         set_new(st1.arr);
90     }
91 
92     @Benchmark
93     public void set_call_val(RefState st1) {
94         set_call(st1.arr);
95     }
96 
97 }