|
| 1 | +/******************************************************************************* |
| 2 | + * ___ _ ____ ____ |
| 3 | + * / _ \ _ _ ___ ___| |_| _ \| __ ) |
| 4 | + * | | | | | | |/ _ \/ __| __| | | | _ \ |
| 5 | + * | |_| | |_| | __/\__ \ |_| |_| | |_) | |
| 6 | + * \__\_\\__,_|\___||___/\__|____/|____/ |
| 7 | + * |
| 8 | + * Copyright (c) 2014-2019 Appsicle |
| 9 | + * Copyright (c) 2019-2024 QuestDB |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + * |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +package org.questdb; |
| 26 | + |
| 27 | +import io.questdb.cairo.sql.SymbolTable; |
| 28 | +import io.questdb.cairo.vm.MemoryCARWImpl; |
| 29 | +import io.questdb.cairo.vm.api.MemoryARW; |
| 30 | +import io.questdb.cairo.wal.DirectCharSequenceIntHashMap; |
| 31 | +import io.questdb.std.CharSequenceIntHashMap; |
| 32 | +import io.questdb.std.Chars; |
| 33 | +import io.questdb.std.MemoryTag; |
| 34 | +import io.questdb.std.Numbers; |
| 35 | +import io.questdb.std.Rnd; |
| 36 | +import io.questdb.std.Unsafe; |
| 37 | +import io.questdb.std.str.DirectString; |
| 38 | +import org.openjdk.jmh.annotations.Benchmark; |
| 39 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 40 | +import org.openjdk.jmh.annotations.Level; |
| 41 | +import org.openjdk.jmh.annotations.Mode; |
| 42 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 43 | +import org.openjdk.jmh.annotations.Scope; |
| 44 | +import org.openjdk.jmh.annotations.Setup; |
| 45 | +import org.openjdk.jmh.annotations.State; |
| 46 | +import org.openjdk.jmh.annotations.TearDown; |
| 47 | +import org.openjdk.jmh.profile.GCProfiler; |
| 48 | +import org.openjdk.jmh.runner.Runner; |
| 49 | +import org.openjdk.jmh.runner.RunnerException; |
| 50 | +import org.openjdk.jmh.runner.options.Options; |
| 51 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 52 | + |
| 53 | +import java.util.concurrent.TimeUnit; |
| 54 | + |
| 55 | +@State(Scope.Thread) |
| 56 | +@BenchmarkMode(Mode.AverageTime) |
| 57 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 58 | +// This benchmark compares the speed of some hash maps implementation for the Wal Writer Symbols map. |
| 59 | +// In this use case, we are only appending new keys to the map, and at tx completion we are clearing the whole |
| 60 | +// map (thus the .clear in each Invocation). |
| 61 | +public class WalWriterSymbolBenchmark { |
| 62 | + private static final double loadFactor = 0.7; |
| 63 | + private final CharSequenceIntHashMap hmap = new CharSequenceIntHashMap(State.nSymbols, loadFactor, SymbolTable.VALUE_NOT_FOUND); |
| 64 | + private final DirectCharSequenceIntHashMap symbolHashMap = new DirectCharSequenceIntHashMap(State.nSymbols, loadFactor, SymbolTable.VALUE_NOT_FOUND); |
| 65 | + |
| 66 | + public static void main(String[] args) throws RunnerException { |
| 67 | + Options opt = new OptionsBuilder() |
| 68 | + .include(WalWriterSymbolBenchmark.class.getSimpleName()) |
| 69 | + .warmupIterations(2) |
| 70 | + .measurementIterations(3) |
| 71 | + .forks(0) |
| 72 | + .addProfiler(GCProfiler.class) |
| 73 | + .build(); |
| 74 | + |
| 75 | + new Runner(opt).run(); |
| 76 | + } |
| 77 | + |
| 78 | + @Benchmark |
| 79 | + public void CharSequenceIntHashMap(State state) { |
| 80 | + for (int i = 0; i < state.indices.length; i++) { |
| 81 | + CharSequence key = state.symbols[state.indices[i]]; |
| 82 | + final int index = hmap.keyIndex(key); |
| 83 | + if (index > -1) { |
| 84 | + hmap.putAt(index, key, i); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + int symbolCount = 0; |
| 89 | + long appendOffset = state.mem.getAppendOffset(); |
| 90 | + state.mem.putInt(0); |
| 91 | + |
| 92 | + for (int j = 0, n = hmap.size(); j < n; j++) { |
| 93 | + final CharSequence symbol = hmap.keys().getQuick(j); |
| 94 | + assert symbol != null; |
| 95 | + final int value = hmap.get(symbol); |
| 96 | + // Ignore symbols cached from symbolMapReader |
| 97 | + if (value >= state.minimumValue) { |
| 98 | + state.mem.putInt(value); |
| 99 | + state.mem.putStr(symbol); |
| 100 | + symbolCount++; |
| 101 | + } |
| 102 | + } |
| 103 | + state.mem.putInt(appendOffset, symbolCount); |
| 104 | + |
| 105 | + hmap.clear(); |
| 106 | + } |
| 107 | + |
| 108 | + @Benchmark |
| 109 | + public void DirectCharSequenceIntHashMap(State state) { |
| 110 | + for (int i = 0; i < state.indices.length; i++) { |
| 111 | + CharSequence key = state.symbols[state.indices[i]]; |
| 112 | + final int hashCode = Chars.hashCode(key); |
| 113 | + final int index = symbolHashMap.keyIndex(key, hashCode); |
| 114 | + if (index > -1) { |
| 115 | + symbolHashMap.putAt(index, key, i, hashCode); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + long appendOffset = state.mem.getAppendOffset(); |
| 120 | + state.mem.putInt(0); |
| 121 | + |
| 122 | + int copied = symbolHashMap.copyTo(state.mem, state.minimumValue); |
| 123 | + |
| 124 | + state.mem.putInt(appendOffset, copied); |
| 125 | + |
| 126 | + symbolHashMap.clear(); |
| 127 | + } |
| 128 | + |
| 129 | + @org.openjdk.jmh.annotations.State(Scope.Thread) |
| 130 | + public static class State { |
| 131 | + public static final int avgReadPerSymbol = 10; |
| 132 | + public static final int nSymbols = 1000; |
| 133 | + public static final int symbolLength = 32; |
| 134 | + public final MemoryARW mem; |
| 135 | + public final int minimumValue; |
| 136 | + private final Rnd rnd; |
| 137 | + public int[] indices; |
| 138 | + public CharSequence[] symbols; |
| 139 | + private long lo; |
| 140 | + |
| 141 | + public State() { |
| 142 | + this.rnd = new Rnd(); |
| 143 | + this.indices = new int[nSymbols * avgReadPerSymbol]; |
| 144 | + this.symbols = new CharSequence[nSymbols]; |
| 145 | + this.mem = new MemoryCARWImpl(Numbers.ceilPow2(4 + nSymbols * ((symbolLength << 1) + 8)), 1, MemoryTag.NATIVE_DEFAULT); |
| 146 | + this.minimumValue = nSymbols / 2; |
| 147 | + } |
| 148 | + |
| 149 | + @Setup(Level.Trial) |
| 150 | + public void setup() { |
| 151 | + // To have an apple-to-apple comparison, we cannot rely on String |
| 152 | + // instead we're using off-heap memory to store the Strings. |
| 153 | + this.lo = Unsafe.malloc(nSymbols * symbolLength * 2, MemoryTag.NATIVE_DEFAULT); |
| 154 | + |
| 155 | + for (int i = 0; i < nSymbols; i++) { |
| 156 | + CharSequence symbol = rnd.nextChars(symbolLength); |
| 157 | + for (int j = 0; j < symbolLength; j++) { |
| 158 | + Unsafe.getUnsafe().putChar(lo + (i * symbolLength + j) * 2, symbol.charAt(j)); |
| 159 | + } |
| 160 | + DirectString ds = new DirectString(); |
| 161 | + ds.of(lo + (i * symbolLength) * 2, symbolLength); |
| 162 | + symbols[i] = ds; |
| 163 | + } |
| 164 | + |
| 165 | + for (int i = 0; i < nSymbols * avgReadPerSymbol; i++) { |
| 166 | + indices[i] = rnd.nextInt(nSymbols); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Setup(Level.Invocation) |
| 171 | + public void setupInvocation() { |
| 172 | + mem.truncate(); |
| 173 | + } |
| 174 | + |
| 175 | + @TearDown(Level.Trial) |
| 176 | + public void tearDown() { |
| 177 | + if (this.lo != 0) { |
| 178 | + Unsafe.free(lo, nSymbols * symbolLength * 2, MemoryTag.NATIVE_DEFAULT); |
| 179 | + this.lo = 0; |
| 180 | + } |
| 181 | + mem.close(); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments