| 1 |
/* |
| 2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
* you may not use this file except in compliance with the License. |
| 4 |
* You may obtain a copy of the License at |
| 5 |
* |
| 6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
* See the License for the specific language governing permissions and |
| 12 |
* limitations under the License. |
| 13 |
*/ |
| 14 |
package io.airlift.slice; |
| 15 |
|
| 16 |
import org.openjdk.jmh.annotations.BenchmarkMode; |
| 17 |
import org.openjdk.jmh.annotations.Fork; |
| 18 |
import org.openjdk.jmh.annotations.Benchmark; |
| 19 |
import org.openjdk.jmh.annotations.Measurement; |
| 20 |
import org.openjdk.jmh.annotations.Mode; |
| 21 |
import org.openjdk.jmh.annotations.Scope; |
| 22 |
import org.openjdk.jmh.annotations.Setup; |
| 23 |
import org.openjdk.jmh.annotations.State; |
| 24 |
import org.openjdk.jmh.annotations.Warmup; |
| 25 |
import org.openjdk.jmh.runner.Runner; |
| 26 |
import org.openjdk.jmh.runner.RunnerException; |
| 27 |
import org.openjdk.jmh.runner.options.Options; |
| 28 |
import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 29 |
import org.openjdk.jmh.runner.options.VerboseMode; |
| 30 |
|
| 31 |
import java.util.concurrent.ThreadLocalRandom; |
| 32 |
|
| 33 |
import static io.airlift.slice.JvmUtils.unsafe; |
| 34 |
|
| 35 |
@SuppressWarnings("restriction") |
| 36 |
@BenchmarkMode(Mode.Throughput) |
| 37 |
@Fork(5) |
| 38 |
@Warmup(iterations = 10) |
| 39 |
@Measurement(iterations = 10) |
| 40 |
public class MemoryCopyBenchmark |
| 41 |
{ |
| 42 |
static final int PAGE_SIZE = 4 * 1024; |
| 43 |
static final int N_PAGES = 256 * 1024; |
| 44 |
static final int ALLOC_SIZE = PAGE_SIZE * N_PAGES; |
| 45 |
|
| 46 |
@State(Scope.Thread) |
| 47 |
public static class Buffers |
| 48 |
{ |
| 49 |
Slice data; |
| 50 |
long startOffset; |
| 51 |
long destOffset; |
| 52 |
|
| 53 |
@Setup |
| 54 |
public void fillWithBogusData() |
| 55 |
{ |
| 56 |
data = Slices.allocate(ALLOC_SIZE); |
| 57 |
for (int idx = 0; idx < data.length() / 8; idx++) { |
| 58 |
data.setLong(idx, ThreadLocalRandom.current().nextLong()); |
| 59 |
} |
| 60 |
|
| 61 |
long startOffsetPages = ThreadLocalRandom.current().nextInt(N_PAGES / 4); |
| 62 |
long destOffsetPages = ThreadLocalRandom.current().nextInt(N_PAGES / 4) + N_PAGES / 2; |
| 63 |
|
| 64 |
startOffset = startOffsetPages * PAGE_SIZE; |
| 65 |
destOffset = destOffsetPages * PAGE_SIZE; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
@Benchmark |
| 70 |
public Slice b00sliceZero(Buffers buffers) |
| 71 |
{ |
| 72 |
return doCopy(buffers, CopyStrategy.SLICE, 0); |
| 73 |
} |
| 74 |
|
| 75 |
@Benchmark |
| 76 |
public Slice b01customLoopZero(Buffers buffers) |
| 77 |
{ |
| 78 |
return doCopy(buffers, CopyStrategy.CUSTOM_LOOP, 0); |
| 79 |
} |
| 80 |
|
| 81 |
@Benchmark |
| 82 |
public Slice b02unsafeZero(Buffers buffers) |
| 83 |
{ |
| 84 |
return doCopy(buffers, CopyStrategy.UNSAFE, 0); |
| 85 |
} |
| 86 |
|
| 87 |
@Benchmark |
| 88 |
public Slice b03slice32B(Buffers buffers) |
| 89 |
{ |
| 90 |
return doCopy(buffers, CopyStrategy.SLICE, 32); |
| 91 |
} |
| 92 |
|
| 93 |
@Benchmark |
| 94 |
public Slice b04customLoop32B(Buffers buffers) |
| 95 |
{ |
| 96 |
return doCopy(buffers, CopyStrategy.CUSTOM_LOOP, 32); |
| 97 |
} |
| 98 |
|
| 99 |
@Benchmark |
| 100 |
public Slice b05unsafe32B(Buffers buffers) |
| 101 |
{ |
| 102 |
return doCopy(buffers, CopyStrategy.UNSAFE, 32); |
| 103 |
} |
| 104 |
|
| 105 |
@Benchmark |
| 106 |
public Slice b06slice128B(Buffers buffers) |
| 107 |
{ |
| 108 |
return doCopy(buffers, CopyStrategy.SLICE, 128); |
| 109 |
} |
| 110 |
|
| 111 |
@Benchmark |
| 112 |
public Slice b07customLoop128B(Buffers buffers) |
| 113 |
{ |
| 114 |
return doCopy(buffers, CopyStrategy.CUSTOM_LOOP, 128); |
| 115 |
} |
| 116 |
|
| 117 |
@Benchmark |
| 118 |
public Slice b08unsafe128B(Buffers buffers) |
| 119 |
{ |
| 120 |
return doCopy(buffers, CopyStrategy.UNSAFE, 128); |
| 121 |
} |
| 122 |
|
| 123 |
@Benchmark |
| 124 |
public Slice b09slice512B(Buffers buffers) |
| 125 |
{ |
| 126 |
return doCopy(buffers, CopyStrategy.SLICE, 512); |
| 127 |
} |
| 128 |
|
| 129 |
@Benchmark |
| 130 |
public Slice b10customLoop512B(Buffers buffers) |
| 131 |
{ |
| 132 |
return doCopy(buffers, CopyStrategy.CUSTOM_LOOP, 512); |
| 133 |
} |
| 134 |
|
| 135 |
@Benchmark |
| 136 |
public Slice b11unsafe512B(Buffers buffers) |
| 137 |
{ |
| 138 |
return doCopy(buffers, CopyStrategy.UNSAFE, 512); |
| 139 |
} |
| 140 |
|
| 141 |
@Benchmark |
| 142 |
public Slice b12slice1K(Buffers buffers) |
| 143 |
{ |
| 144 |
return doCopy(buffers, CopyStrategy.SLICE, 1024); |
| 145 |
} |
| 146 |
|
| 147 |
@Benchmark |
| 148 |
public Slice b13customLoop1K(Buffers buffers) |
| 149 |
{ |
| 150 |
return doCopy(buffers, CopyStrategy.CUSTOM_LOOP, 1024); |
| 151 |
} |
| 152 |
|
| 153 |
@Benchmark |
| 154 |
public Slice b14unsafe1K(Buffers buffers) |
| 155 |
{ |
| 156 |
return doCopy(buffers, CopyStrategy.UNSAFE, 1024); |
| 157 |
} |
| 158 |
|
| 159 |
@Benchmark |
| 160 |
public Slice b15slice1M(Buffers buffers) |
| 161 |
{ |
| 162 |
return doCopy(buffers, CopyStrategy.SLICE, 1024 * 1024); |
| 163 |
} |
| 164 |
|
| 165 |
@Benchmark |
| 166 |
public Slice b16customLoop1M(Buffers buffers) |
| 167 |
{ |
| 168 |
return doCopy(buffers, CopyStrategy.CUSTOM_LOOP, 1024 * 1024); |
| 169 |
} |
| 170 |
|
| 171 |
@Benchmark |
| 172 |
public Slice b17unsafe1M(Buffers buffers) |
| 173 |
{ |
| 174 |
return doCopy(buffers, CopyStrategy.UNSAFE, 1024 * 1024); |
| 175 |
} |
| 176 |
|
| 177 |
@Benchmark |
| 178 |
public Slice b18slice128M(Buffers buffers) |
| 179 |
{ |
| 180 |
return doCopy(buffers, CopyStrategy.SLICE, 128 * 1024 * 1024); |
| 181 |
} |
| 182 |
|
| 183 |
@Benchmark |
| 184 |
public Slice b19customLoop128M(Buffers buffers) |
| 185 |
{ |
| 186 |
return doCopy(buffers, CopyStrategy.CUSTOM_LOOP, 128 * 1024 * 1024); |
| 187 |
} |
| 188 |
|
| 189 |
@Benchmark |
| 190 |
public Slice b20unsafe128M(Buffers buffers) |
| 191 |
{ |
| 192 |
return doCopy(buffers, CopyStrategy.UNSAFE, 128 * 1024 * 1024); |
| 193 |
} |
| 194 |
|
| 195 |
static Slice doCopy(Buffers buffers, CopyStrategy strategy, int length) |
| 196 |
{ |
| 197 |
assert buffers.startOffset >= 0 : "startOffset < 0"; |
| 198 |
assert buffers.destOffset >= 0 : "destOffset < 0"; |
| 199 |
assert buffers.startOffset + length < ALLOC_SIZE : "startOffset + length >= ALLOC_SIZE"; |
| 200 |
assert buffers.destOffset + length < ALLOC_SIZE : "destOffset + length >= ALLOC_SIZE"; |
| 201 |
|
| 202 |
strategy.doCopy(buffers.data, buffers.startOffset, buffers.destOffset, length); |
| 203 |
return buffers.data; |
| 204 |
} |
| 205 |
|
| 206 |
private enum CopyStrategy |
| 207 |
{ |
| 208 |
SLICE |
| 209 |
{ |
| 210 |
@Override |
| 211 |
public void doCopy(Slice data, long src, long dest, int length) |
| 212 |
{ |
| 213 |
data.setBytes((int) dest, data, (int) src, length); |
| 214 |
} |
| 215 |
}, |
| 216 |
|
| 217 |
CUSTOM_LOOP |
| 218 |
{ |
| 219 |
@Override |
| 220 |
public void doCopy(Slice data, long src, long dest, int length) |
| 221 |
{ |
| 222 |
Object base = data.getBase(); |
| 223 |
long offset = data.getAddress(); |
| 224 |
while (length >= SizeOf.SIZE_OF_LONG) { |
| 225 |
long srcLong = unsafe.getLong(base, src + offset); |
| 226 |
unsafe.putLong(base, dest + offset, srcLong); |
| 227 |
|
| 228 |
offset += SizeOf.SIZE_OF_LONG; |
| 229 |
length -= SizeOf.SIZE_OF_LONG; |
| 230 |
} |
| 231 |
|
| 232 |
while (length > 0) { |
| 233 |
byte srcByte = unsafe.getByte(base, src + offset); |
| 234 |
unsafe.putByte(base, dest + offset, srcByte); |
| 235 |
|
| 236 |
offset++; |
| 237 |
length--; |
| 238 |
} |
| 239 |
} |
| 240 |
}, |
| 241 |
|
| 242 |
UNSAFE |
| 243 |
{ |
| 244 |
@Override |
| 245 |
public void doCopy(Slice data, long srcOffset, long destOffset, int length) |
| 246 |
{ |
| 247 |
Object base = data.getBase(); |
| 248 |
srcOffset += data.getAddress(); |
| 249 |
destOffset += data.getAddress(); |
| 250 |
int bytesToCopy = length - (length % 8); |
| 251 |
unsafe.copyMemory(base, srcOffset, base, destOffset, bytesToCopy); |
| 252 |
unsafe.copyMemory(base, srcOffset + bytesToCopy, base, destOffset + bytesToCopy, length - bytesToCopy); |
| 253 |
} |
| 254 |
}; |
| 255 |
|
| 256 |
public abstract void doCopy(Slice data, long src, long dest, int length); |
| 257 |
} |
| 258 |
|
| 259 |
public static void main(String[] args) |
| 260 |
throws RunnerException |
| 261 |
{ |
| 262 |
Options options = new OptionsBuilder() |
| 263 |
.verbosity(VerboseMode.NORMAL) |
| 264 |
.include(".*" + MemoryCopyBenchmark.class.getSimpleName() + ".*") |
| 265 |
.build(); |
| 266 |
|
| 267 |
new Runner(options).run(); |
| 268 |
} |
| 269 |
} |