Skip to content

Commit 043935b

Browse files
committed
Cache most of the CallTargets even in single context mode
1 parent b24e618 commit 043935b

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,34 +1057,40 @@ public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode>
10571057
// be appropriate keys for the cache
10581058
assert RootNode.class.isAssignableFrom(key) || key.getConstructors().length <= 1;
10591059
assert RootNode.class.isAssignableFrom(key) || key.getConstructors().length == 0 || key.getConstructors()[0].getParameterCount() == 0;
1060-
return createCachedCallTargetUnsafe(rootNodeFunction, key);
1060+
return createCachedCallTargetUnsafe(rootNodeFunction, key, true);
10611061
}
10621062

10631063
public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Enum<?> key) {
1064-
return createCachedCallTargetUnsafe(rootNodeFunction, key);
1064+
return createCachedCallTargetUnsafe(rootNodeFunction, key, true);
10651065
}
10661066

10671067
public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Class<? extends Node> nodeClass, String key) {
10681068
// for builtins: name is needed to distinguish builtins that share the same underlying node
10691069
// in general: a String may be parameter of the node wrapped in the root node or the root
10701070
// node itself, there must be finite number of strings that can appear here (i.e., must not
10711071
// be dynamically generated unless their number is bounded).
1072-
return createCachedCallTargetUnsafe(rootNodeFunction, nodeClass, key);
1072+
return createCachedCallTargetUnsafe(rootNodeFunction, true, nodeClass, key);
1073+
}
1074+
1075+
// Variant that should be called at most once per context, because it does not cache the target
1076+
// in single context mode
1077+
public RootCallTarget initBuiltinCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Class<? extends Node> nodeClass, String key) {
1078+
return createCachedCallTargetUnsafe(rootNodeFunction, false, nodeClass, key);
10731079
}
10741080

10751081
public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Class<? extends Node> nodeClass, TruffleString key) {
10761082
// See the String overload
1077-
return createCachedCallTargetUnsafe(rootNodeFunction, nodeClass, key);
1083+
return createCachedCallTargetUnsafe(rootNodeFunction, true, nodeClass, key);
10781084
}
10791085

10801086
public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Class<? extends Node> nodeClass, int key) {
1081-
return createCachedCallTargetUnsafe(rootNodeFunction, nodeClass, key);
1087+
return createCachedCallTargetUnsafe(rootNodeFunction, true, nodeClass, key);
10821088
}
10831089

10841090
public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Class<? extends Node> nodeClass1, Class<?> nodeClass2, String name) {
10851091
// for slot wrappers: the root node may be wrapping a helper wrapper node implementing the
10861092
// wrapper logic and the bare slot node itself
1087-
return createCachedCallTargetUnsafe(rootNodeFunction, nodeClass1, nodeClass2, name);
1093+
return createCachedCallTargetUnsafe(rootNodeFunction, true, nodeClass1, nodeClass2, name);
10881094
}
10891095

10901096
/**
@@ -1096,26 +1102,26 @@ public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode>
10961102
*/
10971103
public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Class<? extends RootNode> klass, Enum<?> signature, TruffleString name,
10981104
boolean doArgumentAndResultConversion) {
1099-
return createCachedCallTargetUnsafe(rootNodeFunction, klass, signature, name, doArgumentAndResultConversion);
1105+
return createCachedCallTargetUnsafe(rootNodeFunction, true, klass, signature, name, doArgumentAndResultConversion);
11001106
}
11011107

11021108
public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Enum<?> signature, TruffleString name,
11031109
boolean doArgumentAndResultConversion) {
1104-
return createCachedCallTargetUnsafe(rootNodeFunction, signature, name, doArgumentAndResultConversion);
1110+
return createCachedCallTargetUnsafe(rootNodeFunction, true, signature, name, doArgumentAndResultConversion);
11051111
}
11061112

11071113
public RootCallTarget createCachedCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Enum<?> signature, TruffleString name) {
1108-
return createCachedCallTargetUnsafe(rootNodeFunction, signature, name);
1114+
return createCachedCallTargetUnsafe(rootNodeFunction, true, signature, name);
11091115
}
11101116

11111117
public RootCallTarget createStructSeqIndexedMemberAccessCachedCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, int memberIndex) {
1112-
return createCachedCallTargetUnsafe(rootNodeFunction, StructSequence.class, memberIndex);
1118+
return createCachedCallTargetUnsafe(rootNodeFunction, true, StructSequence.class, memberIndex);
11131119
}
11141120

11151121
public RootCallTarget createCachedPropAccessCallTarget(Function<PythonLanguage, RootNode> rootNodeFunction, Class<?> nodeClass, String name, int type, int offset) {
11161122
// For the time being, we assume finite/limited number of cext/hpy types members, their
11171123
// types and offsets
1118-
return createCachedCallTargetUnsafe(rootNodeFunction, nodeClass, name, type, offset);
1124+
return createCachedCallTargetUnsafe(rootNodeFunction, true, nodeClass, name, type, offset);
11191125
}
11201126

11211127
/**
@@ -1128,17 +1134,17 @@ public RootCallTarget createCachedPropAccessCallTarget(Function<PythonLanguage,
11281134
* instances. Public methods for adding to the cache must take concrete key type(s) so that all
11291135
* possible cache keys are explicit and documented.
11301136
*/
1131-
private RootCallTarget createCachedCallTargetUnsafe(Function<PythonLanguage, RootNode> rootNodeFunction, Object key) {
1137+
private RootCallTarget createCachedCallTargetUnsafe(Function<PythonLanguage, RootNode> rootNodeFunction, Object key, boolean cacheInSingleContext) {
11321138
CompilerAsserts.neverPartOfCompilation();
1133-
if (!singleContext) {
1139+
if (cacheInSingleContext || !singleContext) {
11341140
return cachedCallTargets.computeIfAbsent(key, k -> PythonUtils.getOrCreateCallTarget(rootNodeFunction.apply(this)));
11351141
} else {
11361142
return PythonUtils.getOrCreateCallTarget(rootNodeFunction.apply(this));
11371143
}
11381144
}
11391145

1140-
private RootCallTarget createCachedCallTargetUnsafe(Function<PythonLanguage, RootNode> rootNodeFunction, Object... cacheKeys) {
1141-
return createCachedCallTargetUnsafe(rootNodeFunction, Arrays.asList(cacheKeys));
1146+
private RootCallTarget createCachedCallTargetUnsafe(Function<PythonLanguage, RootNode> rootNodeFunction, boolean cacheInSingleContext, Object... cacheKeys) {
1147+
return createCachedCallTargetUnsafe(rootNodeFunction, Arrays.asList(cacheKeys), cacheInSingleContext);
11421148
}
11431149

11441150
public void registerBuiltinDescriptorCallTarget(BuiltinMethodDescriptor descriptor, RootCallTarget callTarget) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void initialize(Python3Core core) {
173173
declaresExplicitSelf = true;
174174
}
175175
TruffleString tsName = toTruffleStringUncached(builtin.name());
176-
RootCallTarget callTarget = core.getLanguage().createCachedCallTarget(l -> new BuiltinFunctionRootNode(l, builtin, factory, declaresExplicitSelf), factory.getNodeClass(),
176+
RootCallTarget callTarget = core.getLanguage().initBuiltinCallTarget(l -> new BuiltinFunctionRootNode(l, builtin, factory, declaresExplicitSelf), factory.getNodeClass(),
177177
builtin.name());
178178
Object builtinDoc = builtin.doc().isEmpty() ? PNone.NONE : toTruffleStringUncached(builtin.doc());
179179
int flags = PBuiltinFunction.getFlags(builtin, callTarget);

0 commit comments

Comments
 (0)