Skip to content

Commit 9e9e4d5

Browse files
committed
Preserve order of HPy debug handles
1 parent f41aed4 commit 9e9e4d5

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalHPyDebugModuleBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import java.io.IOException;
4646
import java.util.ArrayList;
47+
import java.util.Comparator;
4748
import java.util.List;
4849

4950
import com.oracle.graal.python.PythonLanguage;
@@ -132,7 +133,7 @@ PList doInt(VirtualFrame frame, int generation) {
132133
private static Object[] getOpenDebugHandles(GraalHPyDebugContext debugContext, int generation) {
133134
ArrayList<GraalHPyHandle> openHandles = debugContext.getOpenHandles(generation);
134135
int n = openHandles.size();
135-
openHandles.sort((a, b) -> Integer.compare(debugContext.getGenerationForHandle(b), debugContext.getGenerationForHandle(a)));
136+
openHandles.sort(Comparator.comparingLong(debugContext::getDebugHandleInfo));
136137
Object[] result = new Object[n];
137138
PythonObjectFactory factory = PythonObjectFactory.getUncached();
138139
// do reverse order to match order expected by HPy tests

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/hpy/GraalHPyDebugContext.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
public final class GraalHPyDebugContext extends GraalHPyContext {
4949

5050
private int currentGeneration;
51-
private int[] generationTable = new int[]{0};
51+
private int age;
52+
private long[] debugHandleInfo = new long[]{0};
5253

5354
public GraalHPyDebugContext(GraalHPyContext context) {
5455
super(context.getContext(), context.getLLVMLibrary());
@@ -62,7 +63,7 @@ public GraalHPyDebugContext(GraalHPyContext context) {
6263
/**
6364
* Since the initialization of the context members cannot use {@link #createHandle(Object)}, we
6465
* track the constants of the debug mode separately here. The reason why we can't use
65-
* {@link #createHandle(Object)} is that the {@link #generationTable} will be initialized after
66+
* {@link #createHandle(Object)} is that the {@link #debugHandleInfo} will be initialized after
6667
* the context members and that would cause an NPE.
6768
*/
6869
private void trackConstants() {
@@ -81,8 +82,8 @@ protected String getName() {
8182
@TruffleBoundary
8283
public ArrayList<GraalHPyHandle> getOpenHandles(int generation) {
8384
ArrayList<GraalHPyHandle> openHandles = new ArrayList<>();
84-
for (int i = 0; i < generationTable.length; i++) {
85-
if (generationTable[i] >= generation) {
85+
for (int i = 0; i < debugHandleInfo.length; i++) {
86+
if (getGeneration(debugHandleInfo[i]) >= generation) {
8687
openHandles.add(getObjectForHPyHandle(i));
8788
}
8889
}
@@ -93,21 +94,22 @@ public int getCurrentGeneration() {
9394
return currentGeneration;
9495
}
9596

96-
public int getGenerationForHandle(GraalHPyHandle handle) {
97-
return generationTable[handle.getIdDebug(this)];
97+
public long getDebugHandleInfo(GraalHPyHandle handle) {
98+
return debugHandleInfo[handle.getIdDebug(this)];
9899
}
99100

100101
public int newGeneration() {
102+
age = 0;
101103
return ++currentGeneration;
102104
}
103105

104106
private void trackHandle(GraalHPyHandle handle) {
105107
int id = handle.getIdDebug(this);
106-
if (id >= generationTable.length) {
107-
int newSize = Math.max(16, generationTable.length * 2);
108-
generationTable = PythonUtils.arrayCopyOf(generationTable, newSize);
108+
if (id >= debugHandleInfo.length) {
109+
int newSize = Math.max(16, debugHandleInfo.length * 2);
110+
debugHandleInfo = PythonUtils.arrayCopyOf(debugHandleInfo, newSize);
109111
}
110-
generationTable[id] = currentGeneration;
112+
debugHandleInfo[id] = toBits(currentGeneration, age++);
111113
}
112114

113115
@Override
@@ -120,6 +122,14 @@ public GraalHPyHandle createHandle(Object delegate) {
120122
@Override
121123
public synchronized void releaseHPyHandleForObject(int handle) {
122124
super.releaseHPyHandleForObject(handle);
123-
generationTable[handle] = -1;
125+
debugHandleInfo[handle] = -1;
126+
}
127+
128+
private static int getGeneration(long bits) {
129+
return (int) (bits >>> Integer.SIZE);
130+
}
131+
132+
private static long toBits(int generation, int age) {
133+
return ((long) generation << Integer.SIZE) | age;
124134
}
125135
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/util/PythonUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ public static int[] arrayCopyOf(int[] original, int newLength) {
189189
}
190190
}
191191

192+
/**
193+
* Executes {@link Arrays#copyOf(long[], int)} and puts all exceptions on the slow path.
194+
*/
195+
public static long[] arrayCopyOf(long[] original, int newLength) {
196+
try {
197+
return Arrays.copyOf(original, newLength);
198+
} catch (Throwable t) {
199+
// Break exception edges
200+
CompilerDirectives.transferToInterpreterAndInvalidate();
201+
throw t;
202+
}
203+
}
204+
192205
/**
193206
* Executes {@code String.getChars} and puts all exceptions on the slow path.
194207
*/

0 commit comments

Comments
 (0)