Skip to content

Commit 1999fb2

Browse files
fangererqunaibit
authored andcommitted
Don't steal native references when replicating
1 parent 733a616 commit 1999fb2

File tree

1 file changed

+1
-42
lines changed

1 file changed

+1
-42
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextBuiltins.java

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,22 +1465,6 @@ protected void trace(PythonContext context, Object ptr, Reference ref, TruffleSt
14651465
* means that it ensures the existence of a {@link PythonAbstractNativeObject} for each native
14661466
* object) and then stored in a Java object array which is then attached to the primary object.
14671467
* </p>
1468-
* <p>
1469-
* Further, the native references are <emph>stolen</emph>. This is important because otherwise
1470-
* we would still keep potential reference cycles in native and the Java GC cannot collect them.
1471-
* As a consequence, the refcount of a native object may temporarily be lower than
1472-
* {@link PythonAbstractObjectNativeWrapper#MANAGED_REFCNT MANAGED_REFCNT}. This is best
1473-
* explained by an example: Assume there is a native object {@code p0} that has a field
1474-
* {@code PyObject *obj}. If native object {@code p1} is assigned to {@code p0->obj}, an incref
1475-
* needs to be done. Now, if the Python GC runs, we will replicate the reference to Java and do
1476-
* a decref. Since the corresponding {@link PythonAbstractNativeObject} then exist, the refcount
1477-
* will be at least {@link PythonAbstractObjectNativeWrapper#MANAGED_REFCNT MANAGED_REFCNT}.
1478-
* Now, another object {@code p2} is assigned to {@code p0->obj} which means the previous
1479-
* {@code p1} will be decref'd. The refcount is at this point
1480-
* {@link PythonAbstractObjectNativeWrapper#MANAGED_REFCNT MANAGED_REFCNT - 1} although there is
1481-
* a managed reference to {@code p1}. This will be fixed in the next Python GC run as soon as we
1482-
* see the update.
1483-
* </p>
14841468
*/
14851469
@CApiBuiltin(ret = Void, args = {Pointer, Pointer, Int}, call = Ignored)
14861470
abstract static class PyTruffleObject_ReplicateNativeReferences extends CApiTernaryBuiltinNode {
@@ -1533,38 +1517,13 @@ static Object doGeneric(Object pointer, Object listHead, int n,
15331517
oldReferents = nativeSequenceStorage.getReplicatedNativeReferences();
15341518
nativeSequenceStorage.setReplicatedNativeReferences(referents);
15351519
}
1536-
// 1. Collect referents (traverse native list and resolve pointers)
1520+
// Collect referents (traverse native list and resolve pointers)
15371521
Object cur = listHead;
15381522
for (int i = 0; i < n; i++) {
15391523
referents[i] = readObjectNode.read(cur, GraalPyGC_CycleNode__item);
15401524
cur = readPointerNode.read(cur, GraalPyGC_CycleNode__next);
15411525
}
15421526

1543-
/*
1544-
* 2. Compare old and new referents. We optimize for the case where the arrays are
1545-
* equal. In this case, we don't need to do anything. If the arrays differ, we will
1546-
* give the stolen reference back (by doing an incref) and we will steal the
1547-
* reference which is up to be replicated.
1548-
*/
1549-
if (!arrayEquals(oldReferents, referents)) {
1550-
int oldLen = oldReferents != null ? oldReferents.length : 0;
1551-
int maxLen = Math.max(oldLen, referents.length);
1552-
for (int i = 0; i < maxLen; i++) {
1553-
Object oldReferent = i < oldLen ? oldReferents[i] : null;
1554-
Object referent = i < referents.length ? referents[i] : null;
1555-
assert oldReferent != null || referent != null;
1556-
if (oldReferent != referent) {
1557-
if (oldReferent instanceof PythonAbstractNativeObject nativeObject) {
1558-
long lItemPointer = coerceNativePointerToLongNode.execute(inliningTarget, nativeObject.getPtr());
1559-
CApiTransitions.addNativeRefCount(lItemPointer, 1);
1560-
}
1561-
if (referent instanceof PythonAbstractNativeObject nativeObject) {
1562-
long lItemPointer = coerceNativePointerToLongNode.execute(inliningTarget, nativeObject.getPtr());
1563-
CApiTransitions.subNativeRefCount(lItemPointer, 1);
1564-
}
1565-
}
1566-
}
1567-
}
15681527
/*
15691528
* As described above: Ensure that the 'old' replicated references are strong until
15701529
* this point. Otherwise, weakly referenced managed objects could die.

0 commit comments

Comments
 (0)