Skip to content

Commit 804dab2

Browse files
committed
[GR-62038] Do not use field offsets to order fields.
PullRequest: graal/20115
2 parents 787a1b5 + 66f6f07 commit 804dab2

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/core/common/Fields.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public class Fields {
6161

6262
private final Class<?>[] declaringClasses;
6363

64+
/**
65+
* @see #getStableOrder()
66+
*/
67+
private final int[] stableOrder;
68+
6469
protected Fields(List<? extends FieldsScanner.FieldInfo> fields) {
6570
Collections.sort(fields);
6671
this.offsets = new long[fields.size()];
@@ -75,6 +80,13 @@ protected Fields(List<? extends FieldsScanner.FieldInfo> fields) {
7580
declaringClasses[index] = f.declaringClass;
7681
index++;
7782
}
83+
this.stableOrder = new int[fields.size()];
84+
List<FieldsScanner.FieldInfo> stableFields = new ArrayList<>(fields);
85+
stableFields.sort(FieldsScanner.FieldInfo.STABLE_COMPARATOR);
86+
for (int i = 0; i < fields.size(); i++) {
87+
FieldsScanner.FieldInfo f = stableFields.get(i);
88+
stableOrder[i] = fields.indexOf(f);
89+
}
7890
}
7991

8092
public Field getField(int i) {
@@ -274,6 +286,21 @@ public long[] getOffsets() {
274286
return offsets;
275287
}
276288

289+
/**
290+
* Gets the indexes of the fields sorted by {@link FieldsScanner.FieldInfo#STABLE_COMPARATOR}.
291+
* Here's an example of how to use this ordering:
292+
*
293+
* <pre>
294+
* for (int idx : fields.getStableOrder()) {
295+
* String fieldName = fields.getName(idx);
296+
* // use fieldName
297+
* }
298+
* </pre>
299+
*/
300+
public int[] getStableOrder() {
301+
return stableOrder;
302+
}
303+
277304
/**
278305
* Gets the name of a field.
279306
*

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/core/common/FieldsScanner.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.lang.reflect.Field;
2828
import java.lang.reflect.Modifier;
2929
import java.util.ArrayList;
30+
import java.util.Comparator;
3031

3132
import jdk.internal.misc.Unsafe;
3233

@@ -60,6 +61,21 @@ public long getOffset(Field field) {
6061
* Describes a field in a class during {@linkplain FieldsScanner scanning}.
6162
*/
6263
public static class FieldInfo implements Comparable<FieldInfo> {
64+
65+
/**
66+
* Sorts fields in ascending order by {@code field name + declaring class name + type name}.
67+
*/
68+
public static final Comparator<FieldInfo> STABLE_COMPARATOR = (o1, o2) -> {
69+
int res = o1.name.compareTo(o2.name);
70+
if (res == 0) {
71+
res = o1.declaringClass.getName().compareTo(o2.declaringClass.getName());
72+
if (res == 0) {
73+
res = o1.type.getName().compareTo(o2.type.getName());
74+
}
75+
}
76+
return res;
77+
};
78+
6379
public final long offset;
6480
public final String name;
6581
public final Class<?> type;

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/GraphDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ protected void readProperties(MethodScope methodScope, Node node) {
14561456
try (DebugCloseable a = ReadPropertiesTimer.start(debug)) {
14571457
NodeSourcePosition position = (NodeSourcePosition) readObject(methodScope);
14581458
Fields fields = node.getNodeClass().getData();
1459-
for (int pos = 0; pos < fields.getCount(); pos++) {
1459+
for (int pos : fields.getStableOrder()) {
14601460
if (fields.getType(pos).isPrimitive()) {
14611461
long primitive = methodScope.reader.getSV();
14621462
fields.setRawPrimitive(node, pos, primitive);

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/GraphEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ private void add(Node node) {
447447

448448
protected void writeProperties(Node node, Fields fields) {
449449
writeObjectId(node.getNodeSourcePosition());
450-
for (int idx = 0; idx < fields.getCount(); idx++) {
450+
for (int idx : fields.getStableOrder()) {
451451
if (fields.getType(idx).isPrimitive()) {
452452
long primitive = fields.getRawPrimitive(node, idx);
453453
writer.putSV(primitive);

0 commit comments

Comments
 (0)