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.array.fill; 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.Scope; 29 import org.openjdk.jmh.annotations.Setup; 30 import org.openjdk.jmh.annotations.State; 31 32 import java.util.Arrays; 33 34 @Fork(value = 3, jvmArgsAppend = {"--enable-preview"}) 35 public class Value032 extends FillBase { 36 37 public interface InterfaceInt { 38 public int value(); 39 } 40 41 public static value class ValueInt implements InterfaceInt { 42 43 public final int value; 44 45 public ValueInt(int value) { 46 this.value = value; 47 } 48 49 public int value() { 50 return value; 51 } 52 53 } 54 55 public static class ValState extends SizeState { 56 public ValueInt[] arr; 57 58 @Setup 59 public void setup() { 60 arr = new ValueInt[size]; 61 for (int i = 0; i < size; i++) { 62 arr[i] = new ValueInt(i); 63 } 64 } 65 } 66 67 public static class StaticHolder { 68 public static ValueInt VALUE = new ValueInt(42); 69 } 70 71 @State(Scope.Thread) 72 public static class InstanceHolder { 73 public ValueInt VALUE = new ValueInt(42); 74 } 75 76 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 77 public ValueInt get_val(int i) { 78 return new ValueInt(i); 79 } 80 81 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 82 public void fill_new_val(ValueInt[] dst) { 83 for (int i = 0; i < dst.length; i++) { 84 dst[i] = new ValueInt(42); 85 } 86 } 87 88 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 89 public void arrayfill_new_val(ValueInt[] dst) { 90 Arrays.fill(dst, new ValueInt(42)); 91 } 92 93 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 94 public void fill_local_val(ValueInt[] dst) { 95 ValueInt local = get_val(42); 96 for (int i = 0; i < dst.length; i++) { 97 dst[i] = local; 98 } 99 } 100 101 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 102 public void arrayfill_local_val(ValueInt[] dst) { 103 Arrays.fill(dst, get_val(42)); 104 } 105 106 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 107 public void fill_static_val(ValueInt[] dst) { 108 for (int i = 0; i < dst.length; i++) { 109 dst[i] = StaticHolder.VALUE; 110 } 111 } 112 113 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 114 public void arrayfill_static_val(ValueInt[] dst) { 115 Arrays.fill(dst, StaticHolder.VALUE); 116 } 117 118 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 119 public void fill_instance_val(ValueInt[] dst, InstanceHolder ih) { 120 for (int i = 0; i < dst.length; i++) { 121 dst[i] = ih.VALUE; 122 } 123 } 124 125 @CompilerControl(CompilerControl.Mode.DONT_INLINE) 126 public void arrayfill_instance_val(ValueInt[] dst, InstanceHolder ih) { 127 Arrays.fill(dst, ih.VALUE); 128 } 129 130 @Benchmark 131 public void fill_new(ValState st1) { 132 fill_new_val(st1.arr); 133 } 134 135 @Benchmark 136 public void arrayfill_new(ValState st1) { 137 arrayfill_new_val(st1.arr); 138 } 139 140 @Benchmark 141 public void fill_local(ValState st1) { 142 fill_local_val(st1.arr); 143 } 144 145 @Benchmark 146 public void arrayfill_local(ValState st1) { 147 arrayfill_local_val(st1.arr); 148 } 149 150 @Benchmark 151 public void fill_static(ValState st1) { 152 fill_static_val(st1.arr); 153 } 154 155 @Benchmark 156 public void arrayfill_static(ValState st1) { 157 arrayfill_static_val(st1.arr); 158 } 159 160 @Benchmark 161 public void fill_instance(ValState st1, InstanceHolder ih) { 162 fill_instance_val(st1.arr, ih); 163 } 164 165 @Benchmark 166 public void arrayfill_instance(ValState st1, InstanceHolder ih) { 167 arrayfill_instance_val(st1.arr, ih); 168 } 169 170 }