Skip to content

Commit 470dfe5

Browse files
committed
use a PythonNativeWrapperLibrary to treat non-null native pointers and delegates as constants
1 parent 24d19ec commit 470dfe5

23 files changed

+382
-159
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
9999
import com.oracle.graal.python.builtins.objects.cext.PythonNativeVoidPtr;
100100
import com.oracle.graal.python.builtins.objects.cext.PythonNativeWrapper;
101+
import com.oracle.graal.python.builtins.objects.cext.PythonNativeWrapperLibrary;
101102
import com.oracle.graal.python.builtins.objects.cext.UnicodeObjectNodes.UnicodeAsWideCharNode;
102103
import com.oracle.graal.python.builtins.objects.code.PCode;
103104
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
@@ -642,10 +643,11 @@ Object slots(Object module, LazyPythonClass pythonClass,
642643
abstract static class CheckFunctionResultNode extends PNodeWithContext {
643644
public abstract Object execute(String name, Object result);
644645

645-
@Specialization
646+
@Specialization(limit = "1")
646647
Object doNativeWrapper(String name, DynamicObjectNativeWrapper.PythonObjectNativeWrapper result,
648+
@CachedLibrary(value = "result") PythonNativeWrapperLibrary lib,
647649
@Cached("create()") CheckFunctionResultNode recursive) {
648-
return recursive.execute(name, result.getDelegate());
650+
return recursive.execute(name, lib.getDelegate(result));
649651
}
650652

651653
@Specialization(guards = "!isPythonObjectNativeWrapper(result)")
@@ -1246,15 +1248,17 @@ private static byte[] subRangeIfNeeded(byte[] ary, long n) {
12461248
}
12471249
}
12481250

1249-
@Specialization
1250-
byte[] doCArrayWrapper(CByteArrayWrapper o, long n) {
1251-
return subRangeIfNeeded(o.getByteArray(), n);
1251+
@Specialization(limit = "1")
1252+
byte[] doCArrayWrapper(CByteArrayWrapper o, long n,
1253+
@CachedLibrary("o") PythonNativeWrapperLibrary lib) {
1254+
return subRangeIfNeeded(o.getByteArray(lib), n);
12521255
}
12531256

1254-
@Specialization
1257+
@Specialization(limit = "1")
12551258
byte[] doSequenceArrayWrapper(VirtualFrame frame, PySequenceArrayWrapper obj, long n,
1259+
@CachedLibrary(value = "obj") PythonNativeWrapperLibrary lib,
12561260
@Cached("create()") BytesNodes.ToBytesNode toBytesNode) {
1257-
return subRangeIfNeeded(toBytesNode.execute(frame, obj.getDelegate()), n);
1261+
return subRangeIfNeeded(toBytesNode.execute(frame, lib.getDelegate(obj)), n);
12581262
}
12591263

12601264
@Specialization(limit = "5")
@@ -1384,9 +1388,10 @@ abstract static class PyTruffle_GetTpFlags extends NativeBuiltin {
13841388
@Child private GetTypeFlagsNode getTypeFlagsNode;
13851389
@Child private GetClassNode getClassNode;
13861390

1387-
@Specialization
1388-
long doPythonObject(PythonNativeWrapper nativeWrapper) {
1389-
PythonAbstractClass pclass = getGetClassNode().execute(nativeWrapper.getDelegate());
1391+
@Specialization(limit = "1")
1392+
long doPythonObject(PythonNativeWrapper nativeWrapper,
1393+
@CachedLibrary("nativeWrapper") PythonNativeWrapperLibrary lib) {
1394+
PythonAbstractClass pclass = getGetClassNode().execute(lib.getDelegate(nativeWrapper));
13901395
return getGetTypeFlagsNode().execute(pclass);
13911396
}
13921397

@@ -1417,9 +1422,10 @@ private GetTypeFlagsNode getGetTypeFlagsNode() {
14171422
@GenerateNodeFactory
14181423
abstract static class PyTruffle_Set_SulongType extends NativeBuiltin {
14191424

1420-
@Specialization
1421-
Object doPythonObject(PythonClassNativeWrapper klass, Object ptr) {
1422-
((PythonManagedClass) klass.getPythonObject()).setSulongType(ptr);
1425+
@Specialization(limit = "1")
1426+
Object doPythonObject(PythonClassNativeWrapper klass, Object ptr,
1427+
@CachedLibrary("klass") PythonNativeWrapperLibrary lib) {
1428+
((PythonManagedClass) lib.getDelegate(klass)).setSulongType(ptr);
14231429
return ptr;
14241430
}
14251431
}

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ public boolean isPointer(
7676

7777
@ExportMessage
7878
public long asPointer(
79+
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
7980
@CachedLibrary(limit = "1") InteropLibrary interopLibrary) throws UnsupportedMessageException {
80-
Object nativePointer = this.getNativePointer();
81+
Object nativePointer = lib.getNativePointer(this);
8182
if (nativePointer instanceof Long) {
8283
return (long) nativePointer;
8384
}
@@ -86,11 +87,12 @@ public long asPointer(
8687

8788
@ExportMessage
8889
public void toNative(
90+
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
8991
@Exclusive @Cached CExtNodes.AsCharPointerNode asCharPointerNode,
9092
@Exclusive @Cached InvalidateNativeObjectsAllManagedNode invalidateNode) {
9193
invalidateNode.execute();
92-
if (!isNative()) {
93-
setNativePointer(asCharPointerNode.execute(getDelegate()));
94+
if (!lib.isNative(this)) {
95+
setNativePointer(asCharPointerNode.execute(lib.getDelegate(this)));
9496
}
9597
}
9698
}
@@ -109,13 +111,10 @@ public CStringWrapper(String delegate) {
109111
super(delegate);
110112
}
111113

112-
public String getString() {
113-
return (String) getDelegate();
114-
}
115-
116114
@ExportMessage
117-
final long getArraySize() {
118-
return this.getString().length();
115+
final long getArraySize(
116+
@CachedLibrary("this") PythonNativeWrapperLibrary lib) {
117+
return ((String) lib.getDelegate(this)).length();
119118
}
120119

121120
@ExportMessage
@@ -125,10 +124,11 @@ final boolean hasArrayElements() {
125124
}
126125

127126
@ExportMessage
128-
final Object readArrayElement(long index) throws InvalidArrayIndexException {
127+
final Object readArrayElement(long index,
128+
@CachedLibrary("this") PythonNativeWrapperLibrary lib) throws InvalidArrayIndexException {
129129
try {
130130
int idx = PInt.intValueExact(index);
131-
String s = getString();
131+
String s = (String) lib.getDelegate(this);
132132
if (idx >= 0 && idx < s.length()) {
133133
return s.charAt(idx);
134134
} else if (idx == s.length()) {
@@ -141,8 +141,9 @@ final Object readArrayElement(long index) throws InvalidArrayIndexException {
141141
}
142142

143143
@ExportMessage
144-
final boolean isArrayElementReadable(long identifier) {
145-
return 0 <= identifier && identifier < getArraySize();
144+
final boolean isArrayElementReadable(long identifier,
145+
@CachedLibrary("this") PythonNativeWrapperLibrary lib) {
146+
return 0 <= identifier && identifier < getArraySize(lib);
146147
}
147148

148149
@ExportMessage
@@ -153,8 +154,9 @@ protected boolean hasNativeType() {
153154

154155
@ExportMessage
155156
protected Object getNativeType(
157+
@CachedLibrary("this") PythonNativeWrapperLibrary lib,
156158
@Exclusive @Cached PCallCapiFunction callByteArrayTypeIdNode) {
157-
return callByteArrayTypeIdNode.call(FUN_GET_BYTE_ARRAY_TYPE_ID, getString().length());
159+
return callByteArrayTypeIdNode.call(FUN_GET_BYTE_ARRAY_TYPE_ID, ((String) lib.getDelegate(this)).length());
158160
}
159161
}
160162

@@ -170,13 +172,13 @@ public CByteArrayWrapper(byte[] delegate) {
170172
super(delegate);
171173
}
172174

173-
public byte[] getByteArray() {
174-
return (byte[]) getDelegate();
175+
public final byte[] getByteArray(PythonNativeWrapperLibrary lib) {
176+
return ((byte[]) lib.getDelegate(this));
175177
}
176178

177179
@ExportMessage
178-
final long getArraySize() {
179-
return this.getByteArray().length;
180+
final long getArraySize(@CachedLibrary("this") PythonNativeWrapperLibrary lib) {
181+
return getByteArray(lib).length;
180182
}
181183

182184
@ExportMessage
@@ -186,10 +188,11 @@ final boolean hasArrayElements() {
186188
}
187189

188190
@ExportMessage
189-
Object readArrayElement(long index) throws InvalidArrayIndexException {
191+
Object readArrayElement(long index,
192+
@CachedLibrary("this") PythonNativeWrapperLibrary lib) throws InvalidArrayIndexException {
190193
try {
191194
int idx = PInt.intValueExact(index);
192-
byte[] arr = getByteArray();
195+
byte[] arr = getByteArray(lib);
193196
if (idx >= 0 && idx < arr.length) {
194197
return arr[idx];
195198
} else if (idx == arr.length) {
@@ -202,8 +205,9 @@ Object readArrayElement(long index) throws InvalidArrayIndexException {
202205
}
203206

204207
@ExportMessage
205-
final boolean isArrayElementReadable(long identifier) {
206-
return 0 <= identifier && identifier < getArraySize();
208+
final boolean isArrayElementReadable(long identifier,
209+
@CachedLibrary("this") PythonNativeWrapperLibrary lib) {
210+
return 0 <= identifier && identifier < getArraySize(lib);
207211
}
208212

209213
@ExportMessage

0 commit comments

Comments
 (0)