Skip to content

Commit c86c0aa

Browse files
timfelfangerer
authored andcommitted
create the call target for code lazily
1 parent 9ffa404 commit c86c0aa

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
import com.oracle.truffle.api.nodes.RootNode;
172172
import com.oracle.truffle.api.nodes.UnexpectedResultException;
173173
import com.oracle.truffle.api.profiles.ConditionProfile;
174+
import com.oracle.truffle.api.profiles.ValueProfile;
174175
import com.oracle.truffle.api.source.Source;
175176

176177
@CoreFunctions(defineModule = "builtins")
@@ -488,33 +489,37 @@ public Object eval(VirtualFrame frame, String expression, @SuppressWarnings("unu
488489
}
489490

490491
@Specialization
491-
public Object eval(VirtualFrame frame, PCode code, @SuppressWarnings("unused") PNone globals, @SuppressWarnings("unused") PNone locals) {
492+
public Object eval(VirtualFrame frame, PCode code, @SuppressWarnings("unused") PNone globals, @SuppressWarnings("unused") PNone locals,
493+
@Cached("createIdentityProfile()") ValueProfile constantCt) {
492494
Frame callerFrame = readCallerFrameNode.executeWith(frame);
493495
PythonObject callerGlobals = PArguments.getGlobals(callerFrame);
494496
PCell[] callerClosure = PArguments.getClosure(callerFrame);
495-
return evalExpression(code, callerGlobals, callerGlobals, callerClosure);
497+
return evalExpression(constantCt.profile(code.getRootCallTarget()), callerGlobals, callerGlobals, callerClosure);
496498
}
497499

498500
@Specialization
499-
public Object eval(VirtualFrame frame, PCode code, PythonObject globals, @SuppressWarnings("unused") PNone locals) {
501+
public Object eval(VirtualFrame frame, PCode code, PythonObject globals, @SuppressWarnings("unused") PNone locals,
502+
@Cached("createIdentityProfile()") ValueProfile constantCt) {
500503
Frame callerFrame = readCallerFrameNode.executeWith(frame);
501504
PCell[] callerClosure = PArguments.getClosure(callerFrame);
502-
return evalExpression(code, globals, globals, callerClosure);
505+
return evalExpression(constantCt.profile(code.getRootCallTarget()), globals, globals, callerClosure);
503506
}
504507

505508
@Specialization
506-
public Object eval(VirtualFrame frame, PCode code, PythonObject globals, PythonObject locals) {
509+
public Object eval(VirtualFrame frame, PCode code, PythonObject globals, PythonObject locals,
510+
@Cached("createIdentityProfile()") ValueProfile constantCt) {
507511
Frame callerFrame = readCallerFrameNode.executeWith(frame);
508512
PCell[] callerClosure = PArguments.getClosure(callerFrame);
509-
return evalExpression(code, globals, locals, callerClosure);
513+
return evalExpression(constantCt.profile(code.getRootCallTarget()), globals, locals, callerClosure);
510514
}
511515

512516
@Specialization
513-
public Object eval(VirtualFrame frame, PCode code, @SuppressWarnings("unused") PNone globals, PythonObject locals) {
517+
public Object eval(VirtualFrame frame, PCode code, @SuppressWarnings("unused") PNone globals, PythonObject locals,
518+
@Cached("createIdentityProfile()") ValueProfile constantCt) {
514519
Frame callerFrame = readCallerFrameNode.executeWith(frame);
515520
PythonObject callerGlobals = PArguments.getGlobals(callerFrame);
516521
PCell[] callerClosure = PArguments.getClosure(callerFrame);
517-
return evalExpression(code, callerGlobals, locals, callerClosure);
522+
return evalExpression(constantCt.profile(code.getRootCallTarget()), callerGlobals, locals, callerClosure);
518523
}
519524

520525
@Specialization
@@ -551,8 +556,8 @@ private byte[] getByteArray(PIBytesLike pByteArray) {
551556
}
552557

553558
@TruffleBoundary
554-
private static Object evalExpression(PCode code, PythonObject globals, PythonObject locals, PCell[] closure) {
555-
return evalNode(code.getRootCallTarget(), globals, locals, closure);
559+
private static Object evalExpression(RootCallTarget ct, PythonObject globals, PythonObject locals, PCell[] closure) {
560+
return evalNode(ct, globals, locals, closure);
556561
}
557562

558563
@TruffleBoundary

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
5959
import com.oracle.graal.python.runtime.PythonCore;
6060
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
61+
import com.oracle.truffle.api.CompilerDirectives;
6162
import com.oracle.truffle.api.RootCallTarget;
6263
import com.oracle.truffle.api.Truffle;
6364
import com.oracle.truffle.api.nodes.Node;
@@ -70,7 +71,8 @@ public final class PCode extends PythonBuiltinObject {
7071
private final long FLAG_POS_VAR_ARGS = 2;
7172
private final long FLAG_POS_VAR_KW_ARGS = 3;
7273

73-
private final RootCallTarget callTarget;
74+
private RootCallTarget callTarget = null;
75+
private final RootNode rootNode;
7476
private final PythonCore core;
7577

7678
// number of arguments (not including keyword only arguments, * or ** args)
@@ -114,7 +116,7 @@ public final class PCode extends PythonBuiltinObject {
114116
public PCode(LazyPythonClass cls, RootNode rootNode, PythonCore core) {
115117
super(cls);
116118
assert rootNode != null;
117-
this.callTarget = Truffle.getRuntime().createCallTarget(rootNode);
119+
this.rootNode = rootNode;
118120
this.core = core;
119121
}
120122

@@ -125,7 +127,7 @@ public PCode(LazyPythonClass cls, int argcount, int kwonlyargcount,
125127
String filename, String name, int firstlineno,
126128
byte[] lnotab) {
127129
super(cls);
128-
this.callTarget = null;
130+
this.rootNode = null;
129131
this.core = null;
130132

131133
this.argcount = argcount;
@@ -294,7 +296,7 @@ private void extractArgStats() {
294296
}
295297

296298
public RootNode getRootNode() {
297-
return callTarget == null ? null : callTarget.getRootNode();
299+
return rootNode;
298300
}
299301

300302
private boolean hasRootNode() {
@@ -434,6 +436,10 @@ public Arity getArity() {
434436
}
435437

436438
public RootCallTarget getRootCallTarget() {
439+
if (rootNode != null && callTarget == null) {
440+
CompilerDirectives.transferToInterpreter();
441+
callTarget = Truffle.getRuntime().createCallTarget(rootNode);
442+
}
437443
return callTarget;
438444
}
439445
}

0 commit comments

Comments
 (0)