Skip to content

Commit 9a5220f

Browse files
committed
Fix cache overlap for mp_subscript and sq_item
1 parent 6ddb2dd commit 9a5220f

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
import com.oracle.graal.python.builtins.objects.cext.capi.PySequenceMethodsWrapper;
213213
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapper;
214214
import com.oracle.graal.python.builtins.objects.cext.capi.UnicodeObjectNodes.UnicodeAsWideCharNode;
215+
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor;
215216
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions;
216217
import com.oracle.graal.python.builtins.objects.cext.common.CArrayWrappers.CStringWrapper;
217218
import com.oracle.graal.python.builtins.objects.cext.common.CExtCommonNodes.SizeofWCharNode;
@@ -2012,17 +2013,19 @@ private Object getProcsWrapper(Object type) {
20122013
*/
20132014
if (value instanceof PBuiltinFunction function) {
20142015
Object wrappedPtr = ExternalFunctionNodes.tryGetHiddenCallable(function);
2016+
// TODO also check that the signature matches
20152017
if (wrappedPtr != null && name.equalsUncached(function.getName(), TS_ENCODING) &&
2016-
function.getEnclosingType() != null && IsSubtypeNode.getUncached().execute(type, function.getEnclosingType())) {
2018+
function.getEnclosingType() != null && IsSubtypeNode.getUncached().execute(type,
2019+
function.getEnclosingType())) {
20172020
return wrappedPtr;
20182021
}
20192022
}
20202023
CApiContext cApiContext = PythonContext.get(this).getCApiContext();
2021-
return cApiContext.getOrCreateProcWrapper(value, this::createProcsWrapper);
2024+
return cApiContext.getOrCreateProcWrapper(getRetDescriptor(), value, PyGetTypeSlotNode::createProcsWrapper);
20222025
}
20232026

2024-
private Object createProcsWrapper(Object value) {
2025-
switch (getRetDescriptor()) {
2027+
private static Object createProcsWrapper(ArgDescriptor signature, Object value) {
2028+
switch (signature) {
20262029
case unaryfunc:
20272030
case reprfunc:
20282031
case iternextfunc:
@@ -2053,7 +2056,7 @@ private Object createProcsWrapper(Object value) {
20532056
case descrgetfunc:
20542057
return new PyProcsWrapper.DescrGetFunctionWrapper(value);
20552058
}
2056-
throw CompilerDirectives.shouldNotReachHere("descriptor: " + getRetDescriptor());
2059+
throw CompilerDirectives.shouldNotReachHere("descriptor: " + signature);
20572060
}
20582061
}
20592062

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.util.HashSet;
5656
import java.util.Map;
5757
import java.util.Objects;
58+
import java.util.WeakHashMap;
5859
import java.util.concurrent.ConcurrentHashMap;
5960
import java.util.concurrent.atomic.AtomicLong;
6061

@@ -75,6 +76,7 @@
7576
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.ToJavaNodeGen;
7677
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.ToNewRefNodeGen;
7778
import com.oracle.graal.python.builtins.objects.cext.capi.DynamicObjectNativeWrapper.PrimitiveNativeWrapper;
79+
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor;
7880
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions;
7981
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.HandleTester;
8082
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.JavaStringToTruffleString;
@@ -102,7 +104,7 @@
102104
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
103105
import com.oracle.graal.python.runtime.PythonOptions;
104106
import com.oracle.graal.python.runtime.exception.PException;
105-
import com.oracle.graal.python.util.Function;
107+
import com.oracle.graal.python.util.BiFunction;
106108
import com.oracle.graal.python.util.PythonUtils;
107109
import com.oracle.graal.python.util.WeakIdentityHashMap;
108110
import com.oracle.truffle.api.CallTarget;
@@ -201,8 +203,6 @@ public final class CApiContext extends CExtContext {
201203
private boolean loadNativeLibrary = true;
202204
public RootCallTarget signatureContainer;
203205

204-
private final WeakIdentityHashMap<Object, Object> procWrappers = new WeakIdentityHashMap<>();
205-
206206
public static TruffleLogger getLogger(Class<?> clazz) {
207207
return PythonLanguage.getLogger(LOGGER_CAPI_NAME + "." + clazz.getSimpleName());
208208
}
@@ -1071,8 +1071,26 @@ public void retainClosure(Object closure) {
10711071
callableClosures.add(closure);
10721072
}
10731073

1074+
private record ProcCacheItem(ArgDescriptor signature, Object callable) {
1075+
1076+
@Override
1077+
public boolean equals(Object o) {
1078+
if (o instanceof ProcCacheItem other) {
1079+
return signature == other.signature && callable == other.callable;
1080+
}
1081+
return false;
1082+
}
1083+
1084+
@Override
1085+
public int hashCode() {
1086+
return System.identityHashCode(signature) + 31 * System.identityHashCode(callable);
1087+
}
1088+
}
1089+
1090+
private final Map<ProcCacheItem, Object> procWrappers = new WeakHashMap<>();
1091+
10741092
@TruffleBoundary
1075-
public Object getOrCreateProcWrapper(Object callable, Function<Object, Object> factory) {
1076-
return procWrappers.computeIfAbsent(callable, factory);
1093+
public Object getOrCreateProcWrapper(ArgDescriptor signature, Object callable, BiFunction<ArgDescriptor, Object, Object> factory) {
1094+
return procWrappers.computeIfAbsent(new ProcCacheItem(signature, callable), (cacheItem) -> factory.apply(cacheItem.signature, cacheItem.callable));
10771095
}
10781096
}

0 commit comments

Comments
 (0)