Skip to content

Commit 768815e

Browse files
committed
ImportStarNode: use fast get custom locals access
1 parent 2e4db93 commit 768815e

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/GetCurrentFrameRef.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,8 @@ static ConditionProfile[] getFlag() {
115115
static ConditionProfile[] getFlagUncached() {
116116
return DISABLED;
117117
}
118+
119+
public static GetCurrentFrameRef create() {
120+
return GetCurrentFrameRefNodeGen.create();
121+
}
118122
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/ReadLocalsNode.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.oracle.graal.python.builtins.objects.frame.PFrame;
4545
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
4646
import com.oracle.graal.python.builtins.objects.function.PArguments;
47+
import com.oracle.graal.python.builtins.objects.object.PythonObject;
4748
import com.oracle.graal.python.nodes.function.ClassBodyRootNode;
4849
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4950
import com.oracle.truffle.api.dsl.Cached;
@@ -52,6 +53,7 @@
5253
import com.oracle.truffle.api.frame.Frame;
5354
import com.oracle.truffle.api.frame.VirtualFrame;
5455
import com.oracle.truffle.api.nodes.Node;
56+
import com.oracle.truffle.api.profiles.ConditionProfile;
5557

5658
/**
5759
* Read the locals from the passed frame, updating them from the frame if that is needed. This does
@@ -101,4 +103,21 @@ Object frameToUpdate(PFrame frame,
101103
public static ReadLocalsNode create() {
102104
return ReadLocalsNodeGen.create();
103105
}
106+
107+
/**
108+
* Fast access to custom locals if a custom dict was provided by the caller and the python frame does not escape.
109+
*
110+
* @param frame the current frame
111+
* @param havePyFrame profile if we do have a python frame (PFrame)
112+
* @param haveCustomLocals profile if the python frame has a custom set locals dict
113+
* @return the custom locals (if found) or the globals
114+
*/
115+
public static PythonObject fastGetCustomLocalsOrGlobals(VirtualFrame frame, ConditionProfile havePyFrame, ConditionProfile haveCustomLocals) {
116+
PFrame pyFrame = PArguments.getCurrentFrameInfo(frame).getPyFrame();
117+
if (havePyFrame.profile(pyFrame != null) && haveCustomLocals.profile(pyFrame.getLocals(null) != null)) {
118+
return (PythonObject) pyFrame.getLocals(null);
119+
}
120+
// return the globals
121+
return PArguments.getGlobals(frame);
122+
}
104123
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/ImportStarNode.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
*/
2626
package com.oracle.graal.python.nodes.statement;
2727

28+
import static com.oracle.graal.python.nodes.frame.ReadLocalsNode.fastGetCustomLocalsOrGlobals;
29+
2830
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
2931
import com.oracle.graal.python.builtins.objects.dict.PDict;
30-
import com.oracle.graal.python.builtins.objects.frame.PFrame;
3132
import com.oracle.graal.python.builtins.objects.function.PArguments;
3233
import com.oracle.graal.python.builtins.objects.mappingproxy.PMappingproxy;
3334
import com.oracle.graal.python.builtins.objects.module.PythonModule;
@@ -58,6 +59,7 @@
5859

5960
public class ImportStarNode extends AbstractImportNode {
6061
private final ConditionProfile javaImport = ConditionProfile.createBinaryProfile();
62+
private final ConditionProfile havePyFrame = ConditionProfile.createBinaryProfile();
6163
private final ConditionProfile haveCustomLocals = ConditionProfile.createBinaryProfile();
6264

6365
@Child private SetItemNode dictWriteNode;
@@ -106,18 +108,8 @@ public ImportStarNode(String moduleName, int level) {
106108

107109
@Override
108110
public void executeVoid(VirtualFrame frame) {
109-
PythonObject globals = PArguments.getGlobals(frame);
110-
Object importedModule = importModule(frame, moduleName, globals, new String[]{"*"}, level);
111-
PythonObject locals = globals;
112-
113-
Object customLocals = PArguments.getCustomLocals(frame);
114-
if (haveCustomLocals.profile(customLocals != null)) {
115-
assert customLocals instanceof PFrame.Reference;
116-
PFrame pyFrame = ((PFrame.Reference) customLocals).getPyFrame();
117-
if (pyFrame != null) {
118-
locals = (PythonObject) pyFrame.getLocals(factory());
119-
}
120-
}
111+
Object importedModule = importModule(frame, moduleName, PArguments.getGlobals(frame), new String[]{"*"}, level);
112+
PythonObject locals = fastGetCustomLocalsOrGlobals(frame, havePyFrame, haveCustomLocals);
121113

122114
if (javaImport.profile(emulateJython() && getContext().getEnv().isHostObject(importedModule))) {
123115
try {

0 commit comments

Comments
 (0)