Skip to content

Commit 3acb7a3

Browse files
committed
reduce code duplication for getting the locals dict off a truffle frame
1 parent dad2afe commit 3acb7a3

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
9696
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
9797
import com.oracle.graal.python.builtins.objects.dict.PDict;
98-
import com.oracle.graal.python.builtins.objects.frame.PFrame;
9998
import com.oracle.graal.python.builtins.objects.frame.FrameBuiltins.GetLocalsNode;
10099
import com.oracle.graal.python.builtins.objects.function.Arity;
101100
import com.oracle.graal.python.builtins.objects.function.PArguments;
@@ -506,12 +505,7 @@ private static void inheritGlobals(Frame callerFrame, Object[] args) {
506505
}
507506

508507
private void inheritLocals(Frame callerFrame, Object[] args, GetLocalsNode getLocalsNode) {
509-
PFrame pFrame = PArguments.getPFrame(callerFrame);
510-
if (pFrame == null) {
511-
pFrame = factory().createPFrame(callerFrame);
512-
PArguments.setPFrame(callerFrame, pFrame);
513-
}
514-
Object callerLocals = getLocalsNode.execute(pFrame);
508+
Object callerLocals = getLocalsNode.execute(callerFrame);
515509
setCustomLocals(args, callerLocals);
516510
}
517511

@@ -1653,21 +1647,11 @@ public Object globals(VirtualFrame frame) {
16531647
abstract static class LocalsNode extends PythonBuiltinNode {
16541648
@Child ReadCallerFrameNode readCallerFrameNode = ReadCallerFrameNode.create();
16551649
@Child GetLocalsNode getLocalsNode = GetLocalsNode.create();
1656-
private final ConditionProfile hasFrame = ConditionProfile.createBinaryProfile();
1657-
private final ConditionProfile hasPFrame = ConditionProfile.createBinaryProfile();
16581650

16591651
@Specialization
16601652
public Object locals(VirtualFrame frame) {
16611653
Frame callerFrame = readCallerFrameNode.executeWith(frame);
1662-
PFrame pFrame = PArguments.getPFrame(callerFrame);
1663-
if (hasPFrame.profile(pFrame == null)) {
1664-
pFrame = factory().createPFrame(callerFrame);
1665-
PArguments.setPFrame(callerFrame, pFrame);
1666-
} else if (hasFrame.profile(!pFrame.hasFrame())) {
1667-
pFrame = factory().createPFrame(callerFrame, pFrame.getLocalsDict());
1668-
PArguments.setPFrame(callerFrame, pFrame);
1669-
}
1670-
return getLocalsNode.execute(pFrame);
1654+
return getLocalsNode.execute(callerFrame);
16711655
}
16721656
}
16731657
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/frame/FrameBuiltins.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@
4444
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
4545
import com.oracle.graal.python.nodes.subscript.SetItemNode;
4646
import com.oracle.truffle.api.CompilerDirectives;
47+
import com.oracle.truffle.api.dsl.Cached;
4748
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4849
import com.oracle.truffle.api.dsl.NodeFactory;
4950
import com.oracle.truffle.api.dsl.Specialization;
5051
import com.oracle.truffle.api.frame.Frame;
5152
import com.oracle.truffle.api.nodes.Node;
5253
import com.oracle.truffle.api.nodes.RootNode;
54+
import com.oracle.truffle.api.profiles.BranchProfile;
55+
import com.oracle.truffle.api.profiles.ConditionProfile;
5356

5457
@CoreFunctions(extendClasses = PythonBuiltinClassType.PFrame)
5558
public final class FrameBuiltins extends PythonBuiltins {
@@ -157,6 +160,22 @@ Object get(PFrame self) {
157160
return locals;
158161
}
159162

163+
@Specialization
164+
Object getFromFrame(Frame owner,
165+
@Cached("createBinaryProfile()") ConditionProfile noPFrame,
166+
@Cached("create()") BranchProfile noFrameOnPFrame) {
167+
PFrame pFrame = PArguments.getPFrame(owner);
168+
if (noPFrame.profile(pFrame == null)) {
169+
pFrame = factory().createPFrame(owner);
170+
PArguments.setPFrame(owner, pFrame);
171+
} else if (!pFrame.hasFrame()) {
172+
noFrameOnPFrame.enter();
173+
pFrame = factory().createPFrame(owner, pFrame.getLocalsDict());
174+
PArguments.setPFrame(owner, pFrame);
175+
}
176+
return get(pFrame);
177+
}
178+
160179
public static GetLocalsNode create() {
161180
return GetLocalsNodeFactory.create();
162181
}

0 commit comments

Comments
 (0)