Skip to content

Commit 25424b8

Browse files
committed
svm: adopt "JDK-8319447: Improve performance of delayed task handling"
1 parent 7fbc537 commit 25424b8

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_util_concurrent_CompletableFuture.java

+49-3
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,54 @@
3333
import com.oracle.svm.core.annotate.Alias;
3434
import com.oracle.svm.core.annotate.InjectAccessors;
3535
import com.oracle.svm.core.annotate.TargetClass;
36+
import com.oracle.svm.core.annotate.TargetElement;
3637
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
3738
import com.oracle.svm.core.feature.InternalFeature;
39+
import com.oracle.svm.core.util.VMError;
40+
41+
import jdk.graal.compiler.serviceprovider.JavaVersionUtil;
3842

3943
@TargetClass(java.util.concurrent.CompletableFuture.class)
4044
final class Target_java_util_concurrent_CompletableFuture {
4145
// Checkstyle: stop
4246
@Alias @InjectAccessors(CompletableFutureUseCommonPoolAccessor.class) //
47+
@TargetElement(onlyWith = JDK21OrEarlier.class) //
4348
private static boolean USE_COMMON_POOL;
4449

4550
@Alias @InjectAccessors(CompletableFutureAsyncPoolAccessor.class) //
51+
@TargetElement(onlyWith = JDK21OrEarlier.class) //
4652
private static Executor ASYNC_POOL;
4753
// Checkstyle: resume
4854
}
4955

56+
@TargetClass(className = "java.util.concurrent.DelayScheduler", onlyWith = JDKLatest.class)
57+
final class Target_java_util_concurrent_DelayScheduler {
58+
@Alias @InjectAccessors(DelaySchedulerNanoTimeOffsetAccessor.class) //
59+
private static long nanoTimeOffset;
60+
}
61+
62+
class DelaySchedulerNanoTimeOffsetAccessor {
63+
static long get() {
64+
return DelaySchedulerNanoTimeOffsetHolder.NANO_TIME_OFFSET;
65+
}
66+
}
67+
68+
/**
69+
* Holder for {@link DelaySchedulerNanoTimeOffsetHolder#NANO_TIME_OFFSET}. Initialized at runtime
70+
* via {@link CompletableFutureFeature}.
71+
*/
72+
class DelaySchedulerNanoTimeOffsetHolder {
73+
74+
public static final long NANO_TIME_OFFSET;
75+
76+
static {
77+
if (SubstrateUtil.HOSTED) {
78+
throw VMError.shouldNotReachHere(DelaySchedulerNanoTimeOffsetHolder.class.getName() + " must only be initialized at runtime");
79+
}
80+
NANO_TIME_OFFSET = Math.min(System.nanoTime(), 0L) + Long.MIN_VALUE;
81+
}
82+
}
83+
5084
class CompletableFutureUseCommonPoolAccessor {
5185
static boolean get() {
5286
return CompletableFutureFieldHolder.USE_COMMON_POOL;
@@ -65,11 +99,22 @@ class CompletableFutureFieldHolder {
6599

66100
static final boolean USE_COMMON_POOL = ForkJoinPool.getCommonPoolParallelism() > 1;
67101

68-
static final Executor ASYNC_POOL = USE_COMMON_POOL ? ForkJoinPool.commonPool()
69-
: SubstrateUtil.cast(new Target_java_util_concurrent_CompletableFuture_ThreadPerTaskExecutor(), Executor.class);
102+
static final Executor ASYNC_POOL;
103+
104+
static {
105+
if (JavaVersionUtil.JAVA_SPEC <= 21) {
106+
if (USE_COMMON_POOL) {
107+
ASYNC_POOL = ForkJoinPool.commonPool();
108+
} else {
109+
ASYNC_POOL = SubstrateUtil.cast(new Target_java_util_concurrent_CompletableFuture_ThreadPerTaskExecutor(), Executor.class);
110+
}
111+
} else {
112+
ASYNC_POOL = null;
113+
}
114+
}
70115
}
71116

72-
@TargetClass(value = java.util.concurrent.CompletableFuture.class, innerClass = "ThreadPerTaskExecutor")
117+
@TargetClass(value = java.util.concurrent.CompletableFuture.class, innerClass = "ThreadPerTaskExecutor", onlyWith = JDK21OrEarlier.class)
73118
final class Target_java_util_concurrent_CompletableFuture_ThreadPerTaskExecutor {
74119
}
75120

@@ -78,5 +123,6 @@ class CompletableFutureFeature implements InternalFeature {
78123
@Override
79124
public void duringSetup(DuringSetupAccess access) {
80125
RuntimeClassInitialization.initializeAtRunTime(CompletableFutureFieldHolder.class);
126+
RuntimeClassInitialization.initializeAtRunTime(DelaySchedulerNanoTimeOffsetHolder.class);
81127
}
82128
}

0 commit comments

Comments
 (0)