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.hash;
 24 
 25 
 26 import org.openjdk.jmh.annotations.Benchmark;
 27 import org.openjdk.jmh.annotations.BenchmarkMode;
 28 import org.openjdk.jmh.annotations.Fork;
 29 import org.openjdk.jmh.annotations.Measurement;
 30 import org.openjdk.jmh.annotations.Mode;
 31 import org.openjdk.jmh.annotations.OutputTimeUnit;
 32 import org.openjdk.jmh.annotations.Scope;
 33 import org.openjdk.jmh.annotations.State;
 34 import org.openjdk.jmh.annotations.Warmup;
 35 
 36 import java.util.concurrent.TimeUnit;
 37 
 38 @Fork(value = 3, jvmArgsAppend = {"--enable-preview"})
 39 @Warmup(iterations = 5, time = 1)
 40 @Measurement(iterations = 5, time = 1)
 41 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 42 @BenchmarkMode(Mode.AverageTime)
 43 @State(Scope.Thread)
 44 public class Value8 {
 45 
 46     public static value class ValueInt8 {
 47 
 48         public final int v0, v1, v2, v3, v4, v5, v6, v7;
 49 
 50         public ValueInt8(int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7) {
 51             this.v0 = v0;
 52             this.v1 = v1;
 53             this.v2 = v2;
 54             this.v3 = v3;
 55             this.v4 = v4;
 56             this.v5 = v5;
 57             this.v6 = v6;
 58             this.v7 = v7;
 59         }
 60 
 61         public int value0() {
 62             return v0;
 63         }
 64 
 65     }
 66 
 67     public static value class ValueInt8Hash {
 68 
 69         public final int v0, v1, v2, v3, v4, v5, v6, v7;
 70 
 71         public ValueInt8Hash(int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7) {
 72             this.v0 = v0;
 73             this.v1 = v1;
 74             this.v2 = v2;
 75             this.v3 = v3;
 76             this.v4 = v4;
 77             this.v5 = v5;
 78             this.v6 = v6;
 79             this.v7 = v7;
 80         }
 81 
 82         public int value0() {
 83             return v0;
 84         }
 85 
 86         @Override
 87         public int hashCode() {
 88             return (((((((v0 * 31) + v1) * 31 + v2) * 31 + v3) * 31 + v4) * 31 + v5) * 31 + v6) * 31 + v7;
 89         }
 90     }
 91 
 92 
 93     @Benchmark
 94     public int explicit() {
 95         return new ValueInt8Hash(42, 43, 44, 45, 46, 47, 48, 49).hashCode();
 96     }
 97 
 98     @Benchmark
 99     public int implicit() {
100         return new ValueInt8(42, 43, 44, 45, 46, 47, 48, 49).hashCode();
101     }
102 
103     @Benchmark
104     public int direct() {
105         return System.identityHashCode(new ValueInt8(42, 43, 44, 45, 46, 47, 48, 49));
106     }
107 
108 }