Skip to content

Commit

Permalink
remove sun.misc.unsafe leftover in the test and benchmark code
Browse files Browse the repository at this point in the history
Removed the usage from a unit test that requires a predictable
ThreadLocalRandom result. This now uses reflection and the runner must
explicitly open the module for access.

Removed from a benchmark comparing table lookup mechanisms and the
cache currently uses the VarHandles approach,

Removed JDK 7's ConcurrentHashMap as this was only useful for
judging the performance benefit of the Java 8 rewrite when trying to
set baseline expectations during the library's initial development.
  • Loading branch information
ben-manes committed Dec 21, 2024
1 parent 8a680a3 commit 62f401c
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 1,839 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
forbiddenApis:
runs-on: ubuntu-latest
env:
JAVA_VERSION: 22
JAVA_VERSION: 23
steps:
- name: Harden Runner
uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # v2.10.2
Expand Down
7 changes: 4 additions & 3 deletions caffeine/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ tasks.named<CheckForbiddenApis>("forbiddenApisJavaPoet").configure {
}

tasks.named<CheckForbiddenApis>("forbiddenApisTest").configure {
bundledSignatures.add("jdk-deprecated-18")
bundledSignatures.addAll(listOf("jdk-deprecated-18", "jdk-unsafe"))
}

tasks.named<CheckForbiddenApis>("forbiddenApisJmh").configure {
bundledSignatures.addAll(listOf("jdk-deprecated-18", "jdk-reflection"))
bundledSignatures.addAll(listOf("jdk-deprecated-18", "jdk-reflection", "jdk-unsafe"))
}

tasks.register<JavaExec>("memoryOverhead") {
Expand Down Expand Up @@ -314,7 +314,8 @@ for (scenario in Scenario.all()) {
} else {
parallel = "methods"
excludeGroups("slow", "isolated", "lincheck")
jvmArgs("-XX:+UseG1GC", "-XX:+ParallelRefProcEnabled")
jvmArgs("-XX:+UseG1GC", "-XX:+ParallelRefProcEnabled",
"--add-opens", "java.base/java.lang=ALL-UNNAMED")
threadCount = max(6, Runtime.getRuntime().availableProcessors() - 1)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.LongStream;

import org.jctools.util.UnsafeAccess;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
Expand Down Expand Up @@ -55,7 +54,6 @@ public class SlotLookupBenchmark {
static final VarHandle PROBE;

ThreadLocal<Integer> threadLocal;
long probeOffset;
long[] array;

@Setup
Expand All @@ -75,11 +73,6 @@ public void setupBinarySearch() {
array = LongStream.range(0, ARENA_SIZE).toArray();
}

@Setup
public void setupStriped64() {
probeOffset = UnsafeAccess.fieldOffset(Thread.class, "threadLocalRandomProbe");
}

@Benchmark
public int threadLocal() {
// Emulates holding the arena slot in a thread-local
Expand Down Expand Up @@ -113,32 +106,6 @@ private static int mix32(int x) {
return (x >>> 16) ^ x;
}

@Benchmark
public long striped64_unsafe(Blackhole blackhole) {
// Emulates finding the arena slot by reusing the thread-local random seed (j.u.c.a.Striped64)
int hash = getProbe_unsafe();
if (hash == 0) {
blackhole.consume(ThreadLocalRandom.current()); // force initialization
hash = getProbe_unsafe();
}
advanceProbe_unsafe(hash);
int index = selectSlot(hash);
return array[index];
}

@SuppressWarnings("SunApi")
private int getProbe_unsafe() {
return UnsafeAccess.UNSAFE.getInt(Thread.currentThread(), probeOffset);
}

@SuppressWarnings("SunApi")
private void advanceProbe_unsafe(int probe) {
probe ^= probe << 13; // xorshift
probe ^= probe >>> 17;
probe ^= probe << 5;
UnsafeAccess.UNSAFE.putInt(Thread.currentThread(), probeOffset, probe);
}

@Benchmark
public long striped64_varHandle(Blackhole blackhole) {
// Emulates finding the arena slot by reusing the thread-local random seed (j.u.c.a.Striped64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.github.benmanes.caffeine.cache.impl.Cache2k;
import com.github.benmanes.caffeine.cache.impl.CaffeineCache;
import com.github.benmanes.caffeine.cache.impl.CoherenceCache;
import com.github.benmanes.caffeine.cache.impl.ConcurrentHashMapV7;
import com.github.benmanes.caffeine.cache.impl.ConcurrentMapCache;
import com.github.benmanes.caffeine.cache.impl.Ehcache3;
import com.github.benmanes.caffeine.cache.impl.GuavaCache;
Expand All @@ -47,12 +46,6 @@ public enum CacheType {

/* --------------- Unbounded --------------- */

ConcurrentHashMapV7 { // see OpenJDK/7u40-b43
@Override public <K, V> BasicCache<K, V> create(int maximumSize) {
return new ConcurrentMapCache<>(
new ConcurrentHashMapV7<>(maximumSize, 0.75f, CONCURRENCY_LEVEL));
}
},
ConcurrentHashMap {
@Override public <K, V> BasicCache<K, V> create(int maximumSize) {
return new ConcurrentMapCache<>(new ConcurrentHashMap<>(maximumSize));
Expand Down
Loading

0 comments on commit 62f401c

Please sign in to comment.