Skip to content

Commit e3594eb

Browse files
committed
[GR-68099] Minor C API performance improvements.
PullRequest: graalpython/3917
2 parents 87086f0 + dbcfae4 commit e3594eb

18 files changed

+160
-129
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@
132132
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.NativePtrToPythonWrapperNode;
133133
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.UpdateStrongRefNode;
134134
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.CoerceNativePointerToLongNode;
135-
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformExceptionToNativeNode;
136-
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodesFactory.TransformExceptionToNativeNodeGen;
135+
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformPExceptionToNativeCachedNode;
137136
import com.oracle.graal.python.builtins.objects.cext.common.CExtToJavaNode;
138137
import com.oracle.graal.python.builtins.objects.cext.common.CExtToNativeNode;
139138
import com.oracle.graal.python.builtins.objects.cext.common.NativePointer;
@@ -792,7 +791,7 @@ static final class CachedExecuteCApiBuiltinNode extends ExecuteCApiBuiltinNode {
792791
@Child private CExtToNativeNode retNode;
793792
@Children private final CExtToJavaNode[] argNodes;
794793
@Child private CApiBuiltinNode builtinNode;
795-
@Child private TransformExceptionToNativeNode transformExceptionToNativeNode;
794+
@Child private TransformPExceptionToNativeCachedNode transformExceptionToNativeNode;
796795

797796
CachedExecuteCApiBuiltinNode(CApiBuiltinExecutable cachedSelf) {
798797
assert cachedSelf.ret.createCheckResultNode() == null : "primitive result check types are only intended for ExternalFunctionInvokeNode";
@@ -828,9 +827,9 @@ Object execute(CApiBuiltinExecutable self, Object[] arguments) {
828827
} catch (PException e) {
829828
if (transformExceptionToNativeNode == null) {
830829
CompilerDirectives.transferToInterpreterAndInvalidate();
831-
transformExceptionToNativeNode = insert(TransformExceptionToNativeNodeGen.create());
830+
transformExceptionToNativeNode = insert(TransformPExceptionToNativeCachedNode.create());
832831
}
833-
transformExceptionToNativeNode.executeCached(e);
832+
transformExceptionToNativeNode.execute(e);
834833
if (cachedSelf.getRetDescriptor().isIntType()) {
835834
return -1;
836835
} else if (cachedSelf.getRetDescriptor().isPyObjectOrPointer()) {

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@
7373
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.CastToNativeLongNode;
7474
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor;
7575
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.ConvertPIntToPrimitiveNode;
76-
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformExceptionToNativeNode;
76+
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformPExceptionToNativeCachedNode;
7777
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodesFactory.ConvertPIntToPrimitiveNodeGen;
78-
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodesFactory.TransformExceptionToNativeNodeGen;
7978
import com.oracle.graal.python.builtins.objects.cext.structs.CStructAccess;
8079
import com.oracle.graal.python.builtins.objects.ints.IntBuiltins;
8180
import com.oracle.graal.python.builtins.objects.ints.IntNodes;
@@ -350,7 +349,7 @@ private static BigInteger convertToBigInteger(long n) {
350349
@CApiBuiltin(ret = Pointer, args = {PyObject}, call = Direct)
351350
public abstract static class PyLong_AsVoidPtr extends CApiUnaryBuiltinNode {
352351
@Child private ConvertPIntToPrimitiveNode asPrimitiveNode;
353-
@Child private TransformExceptionToNativeNode transformExceptionToNativeNode;
352+
@Child private TransformPExceptionToNativeCachedNode transformExceptionToNativeNode;
354353

355354
@Specialization
356355
static long doPointer(int n) {
@@ -374,7 +373,7 @@ long doPointer(PInt n,
374373
try {
375374
throw raiseNode.raise(inliningTarget, OverflowError, ErrorMessages.PYTHON_INT_TOO_LARGE_TO_CONV_TO, "C long");
376375
} catch (PException pe) {
377-
ensureTransformExcNode().executeCached(pe);
376+
ensureTransformExcNode().execute(pe);
378377
return 0;
379378
}
380379
}
@@ -400,15 +399,15 @@ long doGeneric(Object n,
400399
throw raiseNode.raise(inliningTarget, OverflowError, ErrorMessages.PYTHON_INT_TOO_LARGE_TO_CONV_TO, "C long");
401400
}
402401
} catch (PException e) {
403-
ensureTransformExcNode().executeCached(e);
402+
ensureTransformExcNode().execute(e);
404403
return 0;
405404
}
406405
}
407406

408-
private TransformExceptionToNativeNode ensureTransformExcNode() {
407+
private TransformPExceptionToNativeCachedNode ensureTransformExcNode() {
409408
if (transformExceptionToNativeNode == null) {
410409
CompilerDirectives.transferToInterpreterAndInvalidate();
411-
transformExceptionToNativeNode = insert(TransformExceptionToNativeNodeGen.create());
410+
transformExceptionToNativeNode = insert(TransformPExceptionToNativeCachedNode.create());
412411
}
413412
return transformExceptionToNativeNode;
414413
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@
105105
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.PythonToNativeNode;
106106
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.ResolveHandleNode;
107107
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.UpdateStrongRefNode;
108+
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.GetNativeWrapperNode;
108109
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitionsFactory.NativeToPythonNodeGen;
109110
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitionsFactory.PythonToNativeNodeGen;
110-
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.GetNativeWrapperNode;
111111
import com.oracle.graal.python.builtins.objects.cext.common.CArrayWrappers.CArrayWrapper;
112112
import com.oracle.graal.python.builtins.objects.cext.common.CArrayWrappers.CByteArrayWrapper;
113113
import com.oracle.graal.python.builtins.objects.cext.common.CArrayWrappers.CStringWrapper;
114114
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.EnsureExecutableNode;
115115
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.EnsureTruffleStringNode;
116116
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformExceptionFromNativeNode;
117-
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformExceptionToNativeNode;
117+
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformPExceptionToNativeNode;
118118
import com.oracle.graal.python.builtins.objects.cext.common.CExtContext;
119119
import com.oracle.graal.python.builtins.objects.cext.common.GetNextVaArgNode;
120120
import com.oracle.graal.python.builtins.objects.cext.common.NativePointer;
@@ -1041,7 +1041,7 @@ public final int raiseIntWithoutFrame(int errorValue, PythonBuiltinClassType err
10411041
static int doInt(int errorValue, PythonBuiltinClassType errType, TruffleString format, Object[] arguments,
10421042
@Bind Node inliningTarget,
10431043
@Shared("raiseNode") @Cached PRaiseNode raiseNode,
1044-
@Shared("transformExceptionToNativeNode") @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
1044+
@Shared("transformExceptionToNativeNode") @Cached TransformPExceptionToNativeNode transformExceptionToNativeNode) {
10451045
raiseNative(inliningTarget, errType, format, arguments, raiseNode, transformExceptionToNativeNode);
10461046
return errorValue;
10471047
}
@@ -1050,13 +1050,13 @@ static int doInt(int errorValue, PythonBuiltinClassType errType, TruffleString f
10501050
static Object doObject(Object errorValue, PythonBuiltinClassType errType, TruffleString format, Object[] arguments,
10511051
@Bind Node inliningTarget,
10521052
@Shared("raiseNode") @Cached PRaiseNode raiseNode,
1053-
@Shared("transformExceptionToNativeNode") @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) {
1053+
@Shared("transformExceptionToNativeNode") @Cached TransformPExceptionToNativeNode transformExceptionToNativeNode) {
10541054
raiseNative(inliningTarget, errType, format, arguments, raiseNode, transformExceptionToNativeNode);
10551055
return errorValue;
10561056
}
10571057

10581058
private static void raiseNative(Node inliningTarget, PythonBuiltinClassType errType, TruffleString format, Object[] arguments, PRaiseNode raiseNode,
1059-
TransformExceptionToNativeNode transformExceptionToNativeNode) {
1059+
TransformPExceptionToNativeNode transformExceptionToNativeNode) {
10601060
try {
10611061
throw raiseNode.raise(inliningTarget, errType, format, arguments);
10621062
} catch (PException p) {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -49,7 +49,6 @@
4949
import com.oracle.graal.python.util.OverflowException;
5050
import com.oracle.graal.python.util.PythonUtils;
5151
import com.oracle.truffle.api.CompilerDirectives;
52-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5352
import com.oracle.truffle.api.dsl.Cached;
5453
import com.oracle.truffle.api.dsl.Cached.Shared;
5554
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -69,12 +68,10 @@ public final class CPyObjectArrayWrapper extends PythonStructNativeWrapper {
6968

7069
private static final Unsafe UNSAFE = PythonUtils.initUnsafe();
7170

72-
@TruffleBoundary
7371
private static long allocateBoundary(long size) {
7472
return UNSAFE.allocateMemory(size);
7573
}
7674

77-
@TruffleBoundary
7875
private static void freeBoundary(long ptr) {
7976
UNSAFE.freeMemory(ptr);
8077
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapper.PythonStructNativeWrapper;
4747
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.NativeToPythonNode;
4848
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.PythonToNativeNewRefNode;
49-
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformExceptionToNativeNode;
49+
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformPExceptionToNativeNode;
5050
import com.oracle.graal.python.builtins.objects.function.PKeyword;
5151
import com.oracle.graal.python.nodes.argument.keywords.ExpandKeywordStarargsNode;
5252
import com.oracle.graal.python.nodes.argument.positional.ExecutePositionalStarargsNode;
@@ -124,7 +124,7 @@ public Object execute(Object[] arguments,
124124
@Exclusive @Cached CallNode callNode,
125125
@Exclusive @Cached ExecutePositionalStarargsNode posStarargsNode,
126126
@Exclusive @Cached ExpandKeywordStarargsNode expandKwargsNode,
127-
@Exclusive @Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
127+
@Exclusive @Cached TransformPExceptionToNativeNode transformExceptionToNativeNode,
128128
@Exclusive @Cached GilNode gil) throws ArityException {
129129
boolean mustRelease = gil.acquire();
130130
try {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,17 @@ void toNative(
255255
@Bind Node inliningTarget,
256256
@Cached CApiTransitions.FirstToNativeNode firstToNativeNode) {
257257
if (!isNative()) {
258+
boolean immortal;
258259
if (isBool()) {
259260
assert (PythonContext.get(inliningTarget).getCApiContext().getCachedBooleanPrimitiveNativeWrapper(value != 0) == this);
260-
setNativePointer(firstToNativeNode.execute(inliningTarget, this, true /* immortal */));
261-
return;
261+
immortal = true;
262+
} else {
263+
// small int values are cached and will be immortal
264+
immortal = isIntLike() && CApiGuards.isSmallLong(value);
265+
// if this wrapper wraps a small int value, this wrapper is one of the cached
266+
// primitive native wrappers
267+
assert !immortal || (PythonContext.get(inliningTarget).getCApiContext().getCachedPrimitiveNativeWrapper(value) == this);
262268
}
263-
// small int values are cached and will be immortal
264-
boolean immortal = isIntLike() && CApiGuards.isSmallLong(value);
265-
// if this wrapper wraps a small int value, this wrapper is one of the cached primitive
266-
// native wrappers
267-
assert !immortal || (PythonContext.get(inliningTarget).getCApiContext().getCachedPrimitiveNativeWrapper(value) == this);
268269
setNativePointer(firstToNativeNode.execute(inliningTarget, this, immortal));
269270
}
270271
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTiming;
4848
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.NativeToPythonNode;
4949
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.PythonToNativeNewRefNode;
50-
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformExceptionToNativeNode;
50+
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.TransformPExceptionToNativeNode;
5151
import com.oracle.graal.python.builtins.objects.cext.common.CExtContext;
5252
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
5353
import com.oracle.graal.python.builtins.objects.function.PKeyword;
@@ -216,7 +216,7 @@ Object execute(Object[] arguments,
216216
@Cached CreateArgumentsNode createArgsNode,
217217
@Cached CallDispatchers.CallTargetCachedInvokeNode invokeNode,
218218
@Cached NativeToPythonNode toJavaNode,
219-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
219+
@Cached TransformPExceptionToNativeNode transformExceptionToNativeNode,
220220
@Exclusive @Cached GilNode gil) throws ArityException {
221221
boolean mustRelease = gil.acquire();
222222
CApiTiming.enter();
@@ -273,7 +273,7 @@ Object execute(Object[] arguments,
273273
@Cached CallDispatchers.CallTargetCachedInvokeNode invokeNode,
274274
@Cached CreateArgumentsNode createArgsNode,
275275
@Cached NativeToPythonNode toJavaNode,
276-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
276+
@Cached TransformPExceptionToNativeNode transformExceptionToNativeNode,
277277
@Exclusive @Cached GilNode gil) throws ArityException {
278278
boolean mustRelease = gil.acquire();
279279
CApiTiming.enter();
@@ -328,7 +328,7 @@ Object execute(Object[] arguments,
328328
@Cached CreateArgumentsNode createArgsNode,
329329
@Cached CallDispatchers.CallTargetCachedInvokeNode invokeNode,
330330
@Cached NativeToPythonNode toJavaNode,
331-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
331+
@Cached TransformPExceptionToNativeNode transformExceptionToNativeNode,
332332
@Exclusive @Cached GilNode gil) throws ArityException {
333333
boolean mustRelease = gil.acquire();
334334
CApiTiming.enter();
@@ -385,7 +385,7 @@ Object execute(Object[] arguments,
385385
@Cached CallDispatchers.CallTargetCachedInvokeNode invokeNode,
386386
@Cached ExpandKeywordStarargsNode expandKwargsNode,
387387
@Cached NativeToPythonNode toJavaNode,
388-
@Cached TransformExceptionToNativeNode transformExceptionToNativeNode,
388+
@Cached TransformPExceptionToNativeNode transformExceptionToNativeNode,
389389
@Exclusive @Cached GilNode gil) throws ArityException {
390390
boolean mustRelease = gil.acquire();
391391
CApiTiming.enter();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ private static Object allocate(PMemoryView object) {
159159
return mem;
160160
}
161161

162-
@Override
163162
public Object getReplacement(InteropLibrary lib) {
164163
if (replacement == null) {
165164
Object pointerObject = allocate((PMemoryView) getDelegate());

0 commit comments

Comments
 (0)