Skip to content

Commit 6025003

Browse files
committed
don't generate GeneratorFunctionRootNode on fast path
1 parent 04435cf commit 6025003

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PGeneratorFunction.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,17 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.function;
2727

28-
import com.oracle.graal.python.PythonLanguage;
2928
import com.oracle.graal.python.builtins.objects.cell.PCell;
3029
import com.oracle.graal.python.builtins.objects.object.PythonObject;
3130
import com.oracle.graal.python.builtins.objects.type.PythonClass;
32-
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
33-
import com.oracle.graal.python.parser.ExecutionCellSlots;
3431
import com.oracle.truffle.api.RootCallTarget;
35-
import com.oracle.truffle.api.Truffle;
3632
import com.oracle.truffle.api.frame.FrameDescriptor;
3733

3834
public final class PGeneratorFunction extends PFunction {
3935

40-
public static PGeneratorFunction create(PythonClass clazz, PythonLanguage language, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget,
41-
FrameDescriptor frameDescriptor, PythonObject globals, PCell[] closure, ExecutionCellSlots executionCellSlots,
42-
int numOfActiveFlags, int numOfGeneratorBlockNode, int numOfGeneratorForNode) {
43-
44-
GeneratorFunctionRootNode generatorFunctionRootNode = new GeneratorFunctionRootNode(language, callTarget, name,
45-
frameDescriptor, closure, executionCellSlots, numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode);
46-
47-
return new PGeneratorFunction(clazz, name, enclosingClassName, arity, Truffle.getRuntime().createCallTarget(generatorFunctionRootNode), frameDescriptor, globals, closure);
36+
public static PGeneratorFunction create(PythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget,
37+
FrameDescriptor frameDescriptor, PythonObject globals, PCell[] closure) {
38+
return new PGeneratorFunction(clazz, name, enclosingClassName, arity, callTarget, frameDescriptor, globals, closure);
4839
}
4940

5041
public PGeneratorFunction(PythonClass clazz, String name, String enclosingClassName, Arity arity, RootCallTarget callTarget,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/GeneratorFunctionDefinitionNode.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,23 @@
3232
import com.oracle.graal.python.nodes.EmptyNode;
3333
import com.oracle.graal.python.nodes.control.BlockNode;
3434
import com.oracle.graal.python.nodes.expression.ExpressionNode;
35+
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
3536
import com.oracle.graal.python.nodes.statement.StatementNode;
3637
import com.oracle.graal.python.parser.DefinitionCellSlots;
3738
import com.oracle.graal.python.parser.ExecutionCellSlots;
3839
import com.oracle.graal.python.runtime.PythonCore;
3940
import com.oracle.truffle.api.CompilerDirectives;
4041
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4142
import com.oracle.truffle.api.RootCallTarget;
43+
import com.oracle.truffle.api.Truffle;
4244
import com.oracle.truffle.api.frame.FrameDescriptor;
4345
import com.oracle.truffle.api.frame.VirtualFrame;
4446

4547
public class GeneratorFunctionDefinitionNode extends FunctionDefinitionNode {
46-
4748
protected final int numOfActiveFlags;
4849
protected final int numOfGeneratorBlockNode;
4950
protected final int numOfGeneratorForNode;
51+
@CompilationFinal private RootCallTarget generatorCallTarget;
5052

5153
public GeneratorFunctionDefinitionNode(String name, String enclosingClassName, ExpressionNode doc, PythonCore core, Arity arity, StatementNode defaults, RootCallTarget callTarget,
5254
FrameDescriptor frameDescriptor, DefinitionCellSlots definitionCellSlots, ExecutionCellSlots executionCellSlots,
@@ -76,8 +78,18 @@ public PGeneratorFunction execute(VirtualFrame frame) {
7678
defaults.executeVoid(frame);
7779

7880
PCell[] closure = getClosureFromLocals(frame);
79-
return withDocString(frame, factory().createGeneratorFunction(functionName, enclosingClassName, arity, callTarget, frameDescriptor, PArguments.getGlobals(frame), closure, executionCellSlots,
80-
numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode));
81+
return withDocString(frame,
82+
factory().createGeneratorFunction(functionName, enclosingClassName, arity, getGeneratorCallTarget(closure), frameDescriptor, PArguments.getGlobals(frame), closure));
83+
}
84+
85+
protected RootCallTarget getGeneratorCallTarget(PCell[] closure) {
86+
if (generatorCallTarget == null) {
87+
CompilerDirectives.transferToInterpreterAndInvalidate();
88+
GeneratorFunctionRootNode generatorFunctionRootNode = new GeneratorFunctionRootNode(getContext().getLanguage(), callTarget, functionName,
89+
frameDescriptor, closure, executionCellSlots, numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode);
90+
generatorCallTarget = Truffle.getRuntime().createCallTarget(generatorFunctionRootNode);
91+
}
92+
return generatorCallTarget;
8193
}
8294

8395
/**
@@ -101,8 +113,7 @@ public PGeneratorFunction execute(VirtualFrame frame) {
101113
CompilerDirectives.transferToInterpreterAndInvalidate();
102114
PCell[] closure = getClosureFromLocals(frame);
103115
cached = withDocString(frame,
104-
factory().createGeneratorFunction(functionName, enclosingClassName, arity, callTarget, frameDescriptor, PArguments.getGlobals(frame), closure, executionCellSlots,
105-
numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode));
116+
factory().createGeneratorFunction(functionName, enclosingClassName, arity, getGeneratorCallTarget(closure), frameDescriptor, PArguments.getGlobals(frame), closure));
106117
}
107118
return cached;
108119
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,10 +485,9 @@ public PGenerator createGenerator(String name, RootCallTarget callTarget,
485485
}
486486

487487
public PGeneratorFunction createGeneratorFunction(String name, String enclosingClassName, Arity arity, RootCallTarget callTarget,
488-
FrameDescriptor frameDescriptor, PythonObject globals, PCell[] closure, ExecutionCellSlots cellSlots,
489-
int numOfActiveFlags, int numOfGeneratorBlockNode, int numOfGeneratorForNode) {
490-
return trace(PGeneratorFunction.create(lookupClass(PythonBuiltinClassType.PGeneratorFunction), getCore().getLanguage(), name, enclosingClassName, arity, callTarget,
491-
frameDescriptor, globals, closure, cellSlots, numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode));
488+
FrameDescriptor frameDescriptor, PythonObject globals, PCell[] closure) {
489+
return trace(PGeneratorFunction.create(lookupClass(PythonBuiltinClassType.PGeneratorFunction), name, enclosingClassName, arity, callTarget,
490+
frameDescriptor, globals, closure));
492491
}
493492

494493
public PMappingproxy createMappingproxy(PythonObject object) {

0 commit comments

Comments
 (0)