|
| 1 | +/* |
| 2 | + * This file is part of sync, licensed under the MIT License. |
| 3 | + * |
| 4 | + * Copyright (c) 2025 vectrix.space |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +package space.vectrix.sync.collections.fastutil; |
| 25 | + |
| 26 | +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; |
| 27 | +import it.unimi.dsi.fastutil.ints.Int2ObjectMaps; |
| 28 | +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; |
| 29 | +import java.util.SplittableRandom; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import org.openjdk.jmh.annotations.Benchmark; |
| 32 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 33 | +import org.openjdk.jmh.annotations.Fork; |
| 34 | +import org.openjdk.jmh.annotations.Level; |
| 35 | +import org.openjdk.jmh.annotations.Measurement; |
| 36 | +import org.openjdk.jmh.annotations.Mode; |
| 37 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 38 | +import org.openjdk.jmh.annotations.Param; |
| 39 | +import org.openjdk.jmh.annotations.Scope; |
| 40 | +import org.openjdk.jmh.annotations.Setup; |
| 41 | +import org.openjdk.jmh.annotations.State; |
| 42 | +import org.openjdk.jmh.annotations.Threads; |
| 43 | +import org.openjdk.jmh.annotations.Warmup; |
| 44 | +import org.openjdk.jmh.infra.Blackhole; |
| 45 | + |
| 46 | +@Fork(1) |
| 47 | +@Warmup(iterations = 5) |
| 48 | +@Measurement(iterations = 5) |
| 49 | +@BenchmarkMode(Mode.Throughput) |
| 50 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 51 | +public class Int2ObjectSyncMapBenchmark { |
| 52 | + @State(Scope.Benchmark) |
| 53 | + public static class Container { |
| 54 | + @Param({ "SyncMap", "SynchronizedMap" }) |
| 55 | + private String implementation; |
| 56 | + |
| 57 | + @Param({ "none", "presize", "prepopulate" }) |
| 58 | + private String mode; |
| 59 | + |
| 60 | + @Param({ "false", "true" }) |
| 61 | + private boolean prime; |
| 62 | + |
| 63 | + @Param({ "100000" }) |
| 64 | + private int size; |
| 65 | + |
| 66 | + private Int2ObjectMap<Integer> map; |
| 67 | + |
| 68 | + @Setup(Level.Iteration) |
| 69 | + public void setup() { |
| 70 | + final boolean presized = !"none".equalsIgnoreCase(this.mode); |
| 71 | + final boolean prepopulate = "prepopulate".equalsIgnoreCase(this.mode); |
| 72 | + |
| 73 | + switch(this.implementation) { |
| 74 | + case "SyncMap" -> this.map = presized ? new Int2ObjectSyncMap<>(Hashing.FASTEST_MIX, this.size) : new Int2ObjectSyncMap<>(Hashing.FASTEST_MIX); |
| 75 | + case "SynchronizedMap" -> this.map = presized |
| 76 | + ? Int2ObjectMaps.synchronize(new Int2ObjectOpenHashMap<>(this.size)) |
| 77 | + : Int2ObjectMaps.synchronize(new Int2ObjectOpenHashMap<>()); |
| 78 | + default -> throw new IllegalArgumentException("Unable to identify implementation: " + this.implementation); |
| 79 | + } |
| 80 | + |
| 81 | + if(prepopulate) { |
| 82 | + for(int i = 0; i < this.size; i++) { |
| 83 | + this.map.put(i, Integer.valueOf(i)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if(this.prime) { |
| 88 | + if(this.map instanceof final Int2ObjectSyncMap<Integer> syncMap) { |
| 89 | + syncMap.promote(); |
| 90 | + } else { |
| 91 | + for(int i = 0; i < this.size; i++) { |
| 92 | + this.map.get(i); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @State(Scope.Thread) |
| 100 | + public static class Sample { |
| 101 | + @Param({ "50" }) |
| 102 | + private int readPercentage; |
| 103 | + |
| 104 | + private int cursor; |
| 105 | + private int length; |
| 106 | + private boolean[] isRead; |
| 107 | + |
| 108 | + @Setup(Level.Iteration) |
| 109 | + public void setup(final Container container) { |
| 110 | + this.length = container.size; |
| 111 | + this.isRead = new boolean[this.length]; |
| 112 | + |
| 113 | + final SplittableRandom random = new SplittableRandom(Thread.currentThread().getId()); |
| 114 | + for(int i = 0; i < this.length; i++) { |
| 115 | + this.isRead[i] = random.nextInt(100) < this.readPercentage; |
| 116 | + } |
| 117 | + |
| 118 | + this.cursor = 0; |
| 119 | + } |
| 120 | + |
| 121 | + private int next() { |
| 122 | + final int i = this.cursor; |
| 123 | + this.cursor = (i + 1) % this.length; |
| 124 | + return i; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Benchmark |
| 129 | + @Threads(8) |
| 130 | + public void get_only(final Container container, final Sample sample, final Blackhole blackhole) { |
| 131 | + final int key = sample.next(); |
| 132 | + blackhole.consume(container.map.get(key)); |
| 133 | + } |
| 134 | + |
| 135 | + @Benchmark |
| 136 | + @Threads(8) |
| 137 | + public void put_only(final Container container, final Sample sample, final Blackhole blackhole) { |
| 138 | + final int key = sample.next(); |
| 139 | + blackhole.consume(container.map.put(key, Integer.valueOf(key))); |
| 140 | + } |
| 141 | + |
| 142 | + @Benchmark |
| 143 | + @Threads(8) |
| 144 | + public void get_put(final Container container, final Sample sample, final Blackhole blackhole) { |
| 145 | + final int key = sample.next(); |
| 146 | + |
| 147 | + if(sample.isRead[key]) { |
| 148 | + blackhole.consume(container.map.get(key)); |
| 149 | + } else { |
| 150 | + blackhole.consume(container.map.put(key, Integer.valueOf(key))); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments