Skip to content

Commit

Permalink
Encode all metadata in a single array
Browse files Browse the repository at this point in the history
  • Loading branch information
loicottet committed Mar 7, 2022
1 parent 1c8ecae commit fa190bc
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@
import com.oracle.svm.core.annotate.TargetElement;
import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.annotate.UnknownObjectField;
import com.oracle.svm.core.annotate.UnknownPrimitiveField;
import com.oracle.svm.core.classinitialization.ClassInitializationInfo;
import com.oracle.svm.core.classinitialization.EnsureClassInitializedNode;
import com.oracle.svm.core.jdk.JDK11OrEarlier;
import com.oracle.svm.core.jdk.JDK17OrLater;
import com.oracle.svm.core.jdk.Resources;
import com.oracle.svm.core.meta.SharedType;
import com.oracle.svm.core.reflect.MethodMetadataDecoder;
import com.oracle.svm.core.reflect.ReflectionMetadataDecoder;
import com.oracle.svm.core.reflect.Target_java_lang_reflect_RecordComponent;
import com.oracle.svm.core.reflect.Target_jdk_internal_reflect_ConstantPool;
import com.oracle.svm.core.util.LazyFinalReference;
Expand Down Expand Up @@ -323,24 +324,24 @@ public void setModule(Module module) {
@TargetElement(onlyWith = JDK11OrEarlier.class) //
private Class<?> newInstanceCallerCache;

@UnknownObjectField(types = {byte[].class}) private byte[] enclosingMethodInfo;
@UnknownPrimitiveField private int enclosingMethodInfoIndex;

@UnknownObjectField(types = {byte[].class}) private byte[] annotations;
@UnknownPrimitiveField private int annotationsIndex;

@UnknownObjectField(types = {byte[].class}) private byte[] typeAnnotations;
@UnknownPrimitiveField private int typeAnnotationsIndex;

@UnknownObjectField(types = {byte[].class}) byte[] fieldsEncoding;
@UnknownPrimitiveField int fieldsEncodingIndex;

@UnknownObjectField(types = {byte[].class}) byte[] methodsEncoding;
@UnknownPrimitiveField int methodsEncodingIndex;

@UnknownObjectField(types = {byte[].class}) byte[] constructorsEncoding;
@UnknownPrimitiveField int constructorsEncodingIndex;

@UnknownObjectField(types = {byte[].class}) byte[] classesEncoding;
@UnknownPrimitiveField int classesEncodingIndex;

@TargetElement(onlyWith = JDK17OrLater.class)//
@UnknownObjectField(types = {byte[].class}) byte[] recordComponentsEncoding;
@UnknownPrimitiveField int recordComponentsEncodingIndex;

@UnknownObjectField(types = {byte[].class}) byte[] permittedSubclassesEncoding;
@UnknownPrimitiveField int permittedSubclassesEncodingIndex;

@Platforms(Platform.HOSTED_ONLY.class)
public DynamicHub(Class<?> hostedJavaClass, String name, HubType hubType, ReferenceType referenceType, Object isLocalClass, Object isAnonymousClass, DynamicHub superType, DynamicHub componentHub,
Expand Down Expand Up @@ -475,48 +476,48 @@ public String getSignature() {
}

@Platforms(Platform.HOSTED_ONLY.class)
public void setFieldsEncoding(byte[] encoding) {
this.fieldsEncoding = encoding;
public void setFieldsEncodingIndex(int encodingIndex) {
this.fieldsEncodingIndex = encodingIndex;
}

@Platforms(Platform.HOSTED_ONLY.class)
public void setMethodsEncoding(byte[] encoding) {
this.methodsEncoding = encoding;
public void setMethodsEncodingIndex(int encodingIndex) {
this.methodsEncodingIndex = encodingIndex;
}

@Platforms(Platform.HOSTED_ONLY.class)
public void setConstructorsEncoding(byte[] encoding) {
this.constructorsEncoding = encoding;
public void setConstructorsEncodingIndex(int encodingIndex) {
this.constructorsEncodingIndex = encodingIndex;
}

@Platforms(Platform.HOSTED_ONLY.class)
public void setClassesEncoding(byte[] encoding) {
this.classesEncoding = encoding;
public void setClassesEncodingIndex(int encodingIndex) {
this.classesEncodingIndex = encodingIndex;
}

@Platforms(Platform.HOSTED_ONLY.class)
public void setRecordComponentsEncoding(byte[] encoding) {
this.recordComponentsEncoding = encoding;
public void setRecordComponentsEncodingIndex(int encodingIndex) {
this.recordComponentsEncodingIndex = encodingIndex;
}

@Platforms(Platform.HOSTED_ONLY.class)
public void setPermittedSubclassesEncoding(byte[] encoding) {
this.permittedSubclassesEncoding = encoding;
public void setPermittedSubclassesEncodingIndex(int encodingIndex) {
this.permittedSubclassesEncodingIndex = encodingIndex;
}

@Platforms(Platform.HOSTED_ONLY.class)
public void setAnnotationsEncoding(byte[] encoding) {
this.annotations = encoding;
public void setAnnotationsEncodingIndex(int encodingIndex) {
this.annotationsIndex = encodingIndex;
}

@Platforms(Platform.HOSTED_ONLY.class)
public void setTypeAnnotationsEncoding(byte[] encoding) {
this.typeAnnotations = encoding;
public void setTypeAnnotationsEncodingIndex(int encodingIndex) {
this.typeAnnotationsIndex = encodingIndex;
}

@Platforms(Platform.HOSTED_ONLY.class)
public void setEnclosingMethodInfo(byte[] encoding) {
this.enclosingMethodInfo = encoding;
public void setEnclosingMethodInfoIndex(int encodingIndex) {
this.enclosingMethodInfoIndex = encodingIndex;
}

/** Executed at runtime. */
Expand Down Expand Up @@ -1001,12 +1002,12 @@ public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)
@Substitute
@TargetElement(onlyWith = JDK17OrLater.class)
private Target_java_lang_reflect_RecordComponent[] getRecordComponents0() {
if (recordComponentsEncoding == null) {
if (recordComponentsEncodingIndex == ReflectionMetadataDecoder.NULL_ARRAY) {
/* See ReflectionDataBuilder.buildRecordComponents() for details. */
throw VMError.unsupportedFeature("Record components not available for record class " + getTypeName() + ". " +
"All record component accessor methods of this record class must be included in the reflection configuration at image build time, then this method can be called.");
}
return ImageSingletons.lookup(MethodMetadataDecoder.class).parseRecordComponents(this, recordComponentsEncoding);
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseRecordComponents(this, recordComponentsEncodingIndex);
}

@KeepOriginal
Expand Down Expand Up @@ -1329,10 +1330,10 @@ public Optional<?> describeConstable() {

@Substitute
private Object[] getEnclosingMethod0() {
if (enclosingMethodInfo == null) {
if (enclosingMethodInfoIndex == ReflectionMetadataDecoder.NULL_ARRAY) {
return null;
}
Object[] enclosingMethod = ImageSingletons.lookup(MethodMetadataDecoder.class).parseEnclosingMethod(enclosingMethodInfo);
Object[] enclosingMethod = ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseEnclosingMethod(enclosingMethodInfoIndex);
if (enclosingMethod != null) {
PredefinedClassesSupport.throwIfUnresolvable((Class<?>) enclosingMethod[0], getClassLoader0());
}
Expand Down Expand Up @@ -1365,12 +1366,12 @@ private String getGenericSignature0() {

@Substitute
byte[] getRawAnnotations() {
return annotations;
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseByteArray(annotationsIndex);
}

@Substitute
byte[] getRawTypeAnnotations() {
return typeAnnotations;
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseByteArray(typeAnnotationsIndex);
}

@Substitute
Expand All @@ -1380,22 +1381,22 @@ Target_jdk_internal_reflect_ConstantPool getConstantPool() {

@Substitute
private Field[] getDeclaredFields0(boolean publicOnly) {
return ImageSingletons.lookup(MethodMetadataDecoder.class).parseFields(this, fieldsEncoding, publicOnly, true);
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseFields(this, fieldsEncodingIndex, publicOnly, true);
}

@Substitute
private Method[] getDeclaredMethods0(boolean publicOnly) {
return ImageSingletons.lookup(MethodMetadataDecoder.class).parseMethods(this, methodsEncoding, publicOnly, true);
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseMethods(this, methodsEncodingIndex, publicOnly, true);
}

@Substitute
private Constructor<?>[] getDeclaredConstructors0(boolean publicOnly) {
return ImageSingletons.lookup(MethodMetadataDecoder.class).parseConstructors(this, constructorsEncoding, publicOnly, true);
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseConstructors(this, constructorsEncodingIndex, publicOnly, true);
}

@Substitute
private Class<?>[] getDeclaredClasses0() {
Class<?>[] declaredClasses = ImageSingletons.lookup(MethodMetadataDecoder.class).parseClasses(classesEncoding);
Class<?>[] declaredClasses = ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseClasses(classesEncodingIndex);
for (Class<?> clazz : declaredClasses) {
PredefinedClassesSupport.throwIfUnresolvable(clazz, getClassLoader0());
}
Expand Down Expand Up @@ -1447,10 +1448,10 @@ private native <T> Target_java_lang_Class_ReflectionData<T> newReflectionData(So
@Substitute
@TargetElement(onlyWith = JDK17OrLater.class)
private Class<?>[] getPermittedSubclasses0() {
if (permittedSubclassesEncoding.length == 0) {
if (permittedSubclassesEncodingIndex == ReflectionMetadataDecoder.NULL_ARRAY) {
return null;
}
Class<?>[] permittedSubclasses = ImageSingletons.lookup(MethodMetadataDecoder.class).parseClasses(permittedSubclassesEncoding);
Class<?>[] permittedSubclasses = ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseClasses(permittedSubclassesEncodingIndex);
for (Class<?> clazz : permittedSubclasses) {
PredefinedClassesSupport.throwIfUnresolvable(clazz, getClassLoader0());
}
Expand Down Expand Up @@ -1478,7 +1479,7 @@ private Class<?>[] getPermittedSubclasses0() {
private static Method[] filterHidingMethods(Method... methods) {
List<Method> filtered = new ArrayList<>();
for (Method method : methods) {
if (!ImageSingletons.lookup(MethodMetadataDecoder.class).isHidingMethod(method.getModifiers())) {
if (!ImageSingletons.lookup(ReflectionMetadataDecoder.class).isHidingMethod(method.getModifiers())) {
filtered.add(method);
}
}
Expand Down Expand Up @@ -1531,15 +1532,15 @@ private static void setNewInstanceCallerCache(DynamicHub that, Class<?> value) {
}

public Field[] getReachableFields() {
return ImageSingletons.lookup(MethodMetadataDecoder.class).parseFields(this, fieldsEncoding, false, false);
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseFields(this, fieldsEncodingIndex, false, false);
}

public Method[] getReachableMethods() {
return ImageSingletons.lookup(MethodMetadataDecoder.class).parseMethods(this, methodsEncoding, false, false);
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseMethods(this, methodsEncodingIndex, false, false);
}

public Constructor<?>[] getReachableConstructors() {
return ImageSingletons.lookup(MethodMetadataDecoder.class).parseConstructors(this, constructorsEncoding, false, false);
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).parseConstructors(this, constructorsEncodingIndex, false, false);
}
}

Expand Down Expand Up @@ -1640,7 +1641,7 @@ Method getMostSpecific() {
}
}
/* Filter out hiding methods after the retursive lookup is done */
return ImageSingletons.lookup(MethodMetadataDecoder.class).isHidingMethod(m.getModifiers()) ? null : m;
return ImageSingletons.lookup(ReflectionMetadataDecoder.class).isHidingMethod(m.getModifiers()) ? null : m;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@

import com.oracle.svm.core.hub.DynamicHub;

public interface MethodMetadataDecoder {
Field[] parseFields(DynamicHub declaringType, byte[] encoding, boolean publicOnly, boolean reflectOnly);
public interface ReflectionMetadataDecoder {
int NULL_ARRAY = -1;

Method[] parseMethods(DynamicHub declaringType, byte[] encoding, boolean publicOnly, boolean reflectOnly);
Field[] parseFields(DynamicHub declaringType, int index, boolean publicOnly, boolean reflectOnly);

Constructor<?>[] parseConstructors(DynamicHub declaringType, byte[] encoding, boolean publicOnly, boolean reflectOnly);
Method[] parseMethods(DynamicHub declaringType, int index, boolean publicOnly, boolean reflectOnly);

Class<?>[] parseClasses(byte[] encoding);
Constructor<?>[] parseConstructors(DynamicHub declaringType, int index, boolean publicOnly, boolean reflectOnly);

Target_java_lang_reflect_RecordComponent[] parseRecordComponents(DynamicHub declaringType, byte[] encoding);
Class<?>[] parseClasses(int index);

Target_java_lang_reflect_RecordComponent[] parseRecordComponents(DynamicHub declaringType, int index);

Parameter[] parseReflectParameters(Executable executable, byte[] encoding);

Object[] parseEnclosingMethod(byte[] encoding);
Object[] parseEnclosingMethod(int index);

byte[] parseByteArray(int index);

boolean isHidingMethod(int modifiers);

long getMetadataByteLength();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import com.oracle.graal.pointsto.util.TimerCollection;
import org.graalvm.compiler.debug.DebugOptions;
import org.graalvm.compiler.options.OptionValues;
import org.graalvm.compiler.serviceprovider.GraalServices;
Expand All @@ -60,6 +59,7 @@
import com.oracle.graal.pointsto.meta.AnalysisUniverse;
import com.oracle.graal.pointsto.reports.ReportUtils;
import com.oracle.graal.pointsto.util.Timer;
import com.oracle.graal.pointsto.util.TimerCollection;
import com.oracle.svm.core.BuildArtifacts;
import com.oracle.svm.core.BuildArtifacts.ArtifactType;
import com.oracle.svm.core.OS;
Expand All @@ -69,6 +69,7 @@
import com.oracle.svm.core.code.CodeInfoTable;
import com.oracle.svm.core.heap.Heap;
import com.oracle.svm.core.option.HostedOptionValues;
import com.oracle.svm.core.reflect.ReflectionMetadataDecoder;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.hosted.c.codegen.CCompilerInvoker;
import com.oracle.svm.hosted.code.CompileQueue.CompileTask;
Expand Down Expand Up @@ -105,7 +106,6 @@ public class ProgressReporter {
private GCStats lastGCStats = GCStats.getCurrent();
private long numRuntimeCompiledMethods = -1;
private long graphEncodingByteLength = 0;
private long metadataByteLength = 0;
private int numJNIClasses = -1;
private int numJNIFields = -1;
private int numJNIMethods = -1;
Expand Down Expand Up @@ -207,10 +207,6 @@ public void setGraphEncodingByteLength(int value) {
graphEncodingByteLength = value;
}

public void setMetadataByteLength(int value) {
metadataByteLength = value;
}

public void setJNIInfo(int numClasses, int numFields, int numMethods) {
numJNIClasses = numClasses;
numJNIFields = numFields;
Expand Down Expand Up @@ -507,8 +503,9 @@ private Map<String, Long> calculateHeapBreakdown(Collection<ObjectInfo> heapObje
classNameToSize.put(BREAKDOWN_BYTE_ARRAY_PREFIX + linkStrategy.asDocLink("code metadata", "#glossary-code-metadata"), codeInfoSize);
remainingBytes -= codeInfoSize;
}
long metadataByteLength = ImageSingletons.lookup(ReflectionMetadataDecoder.class).getMetadataByteLength();
if (metadataByteLength > 0) {
classNameToSize.put(BREAKDOWN_BYTE_ARRAY_PREFIX + linkStrategy.asDocLink("method metadata", "#glossary-method-metadata"), metadataByteLength);
classNameToSize.put(BREAKDOWN_BYTE_ARRAY_PREFIX + linkStrategy.asDocLink("reflection metadata", "#glossary-method-metadata"), metadataByteLength);
remainingBytes -= metadataByteLength;
}
if (graphEncodingByteLength > 0) {
Expand Down
Loading

0 comments on commit fa190bc

Please sign in to comment.