Skip to content

Commit 5186d9f

Browse files
committed
Do not materialize value of primitive wrappers on 'TO_NATIVE'.
1 parent b7e2c5e commit 5186d9f

File tree

2 files changed

+9
-95
lines changed

2 files changed

+9
-95
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ abstract static class ToNativeNode extends Node {
148148

149149
Object access(PyUnicodeWrapper obj) {
150150
if (!obj.isNative()) {
151-
obj.setNativePointer(toPyObjectNode.execute(obj.getDelegate()));
151+
obj.setNativePointer(toPyObjectNode.execute(obj));
152152
}
153153
return obj;
154154
}

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

Lines changed: 8 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,10 @@
102102
import com.oracle.graal.python.nodes.object.GetLazyClassNode;
103103
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
104104
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
105-
import com.oracle.graal.python.runtime.PythonContext;
106105
import com.oracle.graal.python.runtime.PythonCore;
107106
import com.oracle.graal.python.runtime.exception.PException;
108107
import com.oracle.graal.python.runtime.interop.PythonMessageResolution;
109108
import com.oracle.graal.python.runtime.sequence.PSequence;
110-
import com.oracle.truffle.api.Assumption;
111109
import com.oracle.truffle.api.CompilerDirectives;
112110
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
113111
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -852,32 +850,23 @@ public Object access(Object object) {
852850
@Resolve(message = "TO_NATIVE")
853851
abstract static class ToNativeNode extends Node {
854852
@Child private ToPyObjectNode toPyObjectNode;
855-
@Child private MaterializeDelegateNode materializeNode;
856853
@Child private PIsPointerNode pIsPointerNode = PIsPointerNode.create();
857854

858855
Object access(PythonClassInitNativeWrapper obj) {
859856
if (!pIsPointerNode.execute(obj)) {
860-
obj.setNativePointer(getToPyObjectNode().getHandleForObject(getMaterializeDelegateNode().execute(obj)));
857+
obj.setNativePointer(getToPyObjectNode().execute(obj));
861858
}
862859
return obj;
863860
}
864861

865862
Object access(PythonNativeWrapper obj) {
866863
assert !(obj instanceof PythonClassInitNativeWrapper);
867864
if (!pIsPointerNode.execute(obj)) {
868-
obj.setNativePointer(getToPyObjectNode().execute(getMaterializeDelegateNode().execute(obj)));
865+
obj.setNativePointer(getToPyObjectNode().execute(obj));
869866
}
870867
return obj;
871868
}
872869

873-
private MaterializeDelegateNode getMaterializeDelegateNode() {
874-
if (materializeNode == null) {
875-
CompilerDirectives.transferToInterpreterAndInvalidate();
876-
materializeNode = insert(MaterializeDelegateNode.create());
877-
}
878-
return materializeNode;
879-
}
880-
881870
private ToPyObjectNode getToPyObjectNode() {
882871
if (toPyObjectNode == null) {
883872
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -933,7 +922,7 @@ abstract static class PAsPointerNode extends PNodeWithContext {
933922

934923
public abstract long execute(PythonNativeWrapper o);
935924

936-
@Specialization(assumptions = "getSingleNativeContextAssumption()", guards = "!obj.isNative()")
925+
@Specialization(guards = "!obj.isNative()")
937926
long doBoolNotNative(BoolNativeWrapper obj,
938927
@Cached("create()") MaterializeDelegateNode materializeNode) {
939928
// special case for True and False singletons
@@ -942,25 +931,17 @@ long doBoolNotNative(BoolNativeWrapper obj,
942931
return doFast(obj);
943932
}
944933

945-
@Specialization(assumptions = "getSingleNativeContextAssumption()", guards = "obj.isNative()")
934+
@Specialization(guards = "obj.isNative()")
946935
long doBoolNative(BoolNativeWrapper obj) {
947936
return doFast(obj);
948937
}
949938

950-
@Specialization(assumptions = "getSingleNativeContextAssumption()", guards = "!isBoolNativeWrapper(obj)")
939+
@Specialization(guards = "!isBoolNativeWrapper(obj)")
951940
long doFast(PythonNativeWrapper obj) {
952941
// the native pointer object must either be a TruffleObject or a primitive
953942
return ensureLong(obj.getNativePointer());
954943
}
955944

956-
@Specialization(replaces = {"doFast", "doBoolNotNative", "doBoolNative"})
957-
long doGenericSlow(PythonNativeWrapper obj,
958-
@Cached("create()") MaterializeDelegateNode materializeNode,
959-
@Cached("create()") ToPyObjectNode toPyObjectNode) {
960-
Object materialized = materializeNode.execute(obj);
961-
return ensureLong(toPyObjectNode.execute(materialized));
962-
}
963-
964945
private long ensureLong(Object nativePointer) {
965946
if (nativePointer instanceof TruffleObject) {
966947
if (asPointerNode == null) {
@@ -981,70 +962,11 @@ protected static boolean isBoolNativeWrapper(Object obj) {
981962
return obj instanceof BoolNativeWrapper;
982963
}
983964

984-
protected Assumption getSingleNativeContextAssumption() {
985-
return PythonContext.getSingleNativeContextAssumption();
986-
}
987-
988965
public static PAsPointerNode create() {
989966
return PAsPointerNodeGen.create();
990967
}
991968
}
992969

993-
abstract static class PToNativeNode extends PNodeWithContext {
994-
@Child private ToPyObjectNode toPyObjectNode;
995-
@Child private MaterializeDelegateNode materializeNode;
996-
@Child private PIsPointerNode pIsPointerNode = PIsPointerNode.create();
997-
998-
public abstract PythonNativeWrapper execute(PythonNativeWrapper obj);
999-
1000-
@Specialization
1001-
PythonNativeWrapper doInitClass(PythonClassInitNativeWrapper obj) {
1002-
if (!pIsPointerNode.execute(obj)) {
1003-
obj.setNativePointer(getToPyObjectNode().getHandleForObject(getMaterializeDelegateNode().execute(obj)));
1004-
}
1005-
return obj;
1006-
}
1007-
1008-
@Specialization
1009-
PythonNativeWrapper doBool(BoolNativeWrapper obj) {
1010-
if (!pIsPointerNode.execute(obj)) {
1011-
PInt materialized = (PInt) getMaterializeDelegateNode().execute(obj);
1012-
obj.setNativePointer(getToPyObjectNode().execute(materialized));
1013-
if (!materialized.getNativeWrapper().isNative()) {
1014-
assert materialized.getNativeWrapper() != obj;
1015-
materialized.getNativeWrapper().setNativePointer(obj.getNativePointer());
1016-
}
1017-
assert obj.getNativePointer() == materialized.getNativeWrapper().getNativePointer();
1018-
}
1019-
return obj;
1020-
}
1021-
1022-
@Fallback
1023-
PythonNativeWrapper doGeneric(PythonNativeWrapper obj) {
1024-
assert !(obj instanceof PythonClassInitNativeWrapper);
1025-
if (!pIsPointerNode.execute(obj)) {
1026-
obj.setNativePointer(getToPyObjectNode().execute(getMaterializeDelegateNode().execute(obj)));
1027-
}
1028-
return obj;
1029-
}
1030-
1031-
private MaterializeDelegateNode getMaterializeDelegateNode() {
1032-
if (materializeNode == null) {
1033-
CompilerDirectives.transferToInterpreterAndInvalidate();
1034-
materializeNode = insert(MaterializeDelegateNode.create());
1035-
}
1036-
return materializeNode;
1037-
}
1038-
1039-
private ToPyObjectNode getToPyObjectNode() {
1040-
if (toPyObjectNode == null) {
1041-
CompilerDirectives.transferToInterpreterAndInvalidate();
1042-
toPyObjectNode = insert(ToPyObjectNode.create());
1043-
}
1044-
return toPyObjectNode;
1045-
}
1046-
}
1047-
1048970
abstract static class ToPyObjectNode extends CExtBaseNode {
1049971
@CompilationFinal private TruffleObject PyObjectHandle_FromJavaObject;
1050972
@CompilationFinal private TruffleObject PyObjectHandle_FromJavaType;
@@ -1053,15 +975,15 @@ abstract static class ToPyObjectNode extends CExtBaseNode {
1053975
@Child private PCallNativeNode callNativeBinary;
1054976
@Child private CExtNodes.ToSulongNode toSulongNode;
1055977

1056-
public abstract Object execute(PythonNativeWrapper wrapper, Object value);
978+
public abstract Object execute(PythonNativeWrapper wrapper);
1057979

1058980
@Specialization(guards = "isManagedPythonClass(wrapper)")
1059-
Object doClass(PythonClassNativeWrapper wrapper, PythonClass object) {
981+
Object doClass(PythonClassNativeWrapper wrapper) {
1060982
return callUnaryIntoCapi(getPyObjectHandle_ForJavaType(), wrapper);
1061983
}
1062984

1063985
@Fallback
1064-
Object doObject(PythonNativeWrapper wrapper, Object object) {
986+
Object doObject(PythonNativeWrapper wrapper) {
1065987
return callUnaryIntoCapi(getPyObjectHandle_ForJavaObject(), wrapper);
1066988
}
1067989

@@ -1094,14 +1016,6 @@ private Object callUnaryIntoCapi(TruffleObject fun, Object arg) {
10941016
return callNativeUnary.execute(fun, new Object[]{arg});
10951017
}
10961018

1097-
private CExtNodes.ToSulongNode getToSulongNode() {
1098-
if (toSulongNode == null) {
1099-
CompilerDirectives.transferToInterpreterAndInvalidate();
1100-
toSulongNode = insert(CExtNodes.ToSulongNode.create());
1101-
}
1102-
return toSulongNode;
1103-
}
1104-
11051019
public static ToPyObjectNode create() {
11061020
return ToPyObjectNodeGen.create();
11071021
}

0 commit comments

Comments
 (0)