From 52c595d6499031d4f34ca21e118affb8d6283c03 Mon Sep 17 00:00:00 2001 From: Peter Hofer Date: Thu, 16 Feb 2023 10:27:39 +0100 Subject: [PATCH] Never burden @ValueBased objects with a monitor field, including boxed primitives. --- .../svm/hosted/HostedConfiguration.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/HostedConfiguration.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/HostedConfiguration.java index 9d17937e9641..1a0ab027517f 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/HostedConfiguration.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/HostedConfiguration.java @@ -24,6 +24,7 @@ */ package com.oracle.svm.hosted; +import java.lang.annotation.Annotation; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashSet; @@ -74,12 +75,15 @@ import com.oracle.svm.hosted.meta.HostedMetaAccess; import com.oracle.svm.hosted.meta.HostedUniverse; import com.oracle.svm.hosted.substitute.UnsafeAutomaticSubstitutionProcessor; +import com.oracle.svm.util.ReflectionUtil; import jdk.vm.ci.meta.JavaKind; import jdk.vm.ci.meta.MetaAccessProvider; import jdk.vm.ci.meta.ResolvedJavaField; public class HostedConfiguration { + @SuppressWarnings("unchecked") private static final Class VALUE_BASED_ANNOTATION = // + (Class) ReflectionUtil.lookupClass(false, "jdk.internal.ValueBased"); public HostedConfiguration() { } @@ -230,7 +234,10 @@ public AbstractAnalysisResultsBuilder createStaticAnalysisResultsBuilder(Inflati public void collectMonitorFieldInfo(BigBang bb, HostedUniverse hUniverse, Set immutableTypes) { /* First set the monitor field for types that always need it. */ - getForceMonitorSlotTypes(bb).forEach(type -> setMonitorField(hUniverse, type)); + for (AnalysisType type : getForceMonitorSlotTypes(bb)) { + assert !immutableTypes.contains(type); + setMonitorField(hUniverse, type); + } /* Then decide what other types may need it. */ processedSynchronizedTypes(bb, hUniverse, immutableTypes); @@ -253,11 +260,16 @@ protected void processedSynchronizedTypes(BigBang bb, HostedUniverse hUniverse, } /** - * Monitor fields on arrays would increase the array header too much. Also, types that must be - * immutable cannot have a monitor field. + * Monitor fields on arrays would enlarge the array header too much. + * + * Also, never burden @ValueBased classes with a monitor field, which in particular reduces + * sizes of boxed primitives. Using monitors with those types is discouraged (instances are + * often cached) and not guaranteed to work in the future. + * + * Types that must be immutable cannot have a monitor field. */ protected static void maybeSetMonitorField(HostedUniverse hUniverse, Set immutableTypes, AnalysisType type) { - if (!type.isArray() && !immutableTypes.contains(type)) { + if (!type.isArray() && !immutableTypes.contains(type) && !type.isAnnotationPresent(VALUE_BASED_ANNOTATION)) { setMonitorField(hUniverse, type); } }