Skip to content

Commit 09b4e69

Browse files
committed
mark our internal sources as cached and ensure they can be re-used across contexts in the same engine
1 parent b62ab3b commit 09b4e69

File tree

7 files changed

+64
-61
lines changed

7 files changed

+64
-61
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/PythonTests.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.nio.file.Paths;
4949

5050
import org.graalvm.polyglot.Context;
51+
import org.graalvm.polyglot.Engine;
5152
import org.graalvm.polyglot.PolyglotException;
5253
import org.junit.BeforeClass;
5354

@@ -81,8 +82,23 @@ public class PythonTests {
8182
final static ByteArrayOutputStream outArray = new ByteArrayOutputStream();
8283
final static PrintStream errStream = new PrintStream(errArray);
8384
final static PrintStream outStream = new PrintStream(outArray);
85+
public static Engine engine = Engine.newBuilder().out(PythonTests.outStream).err(PythonTests.errStream).build();
8486
public static Context context = null;
8587

88+
public static void resetContext(String[] args) {
89+
if (PythonTests.context != null) {
90+
PythonTests.context.close();
91+
}
92+
org.graalvm.polyglot.Context.Builder ctxBuilder = Context.newBuilder();
93+
ctxBuilder.engine(engine);
94+
ctxBuilder.allowAllAccess(true);
95+
PythonTests.outArray.reset();
96+
PythonTests.errArray.reset();
97+
ctxBuilder.arguments("python", args);
98+
PythonTests.context = ctxBuilder.build();
99+
PythonTests.context.initialize("python");
100+
}
101+
86102
public static void assertBenchNoError(Path scriptName, String[] args) {
87103
final ByteArrayOutputStream byteArrayErr = new ByteArrayOutputStream();
88104
final ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
@@ -298,21 +314,6 @@ public static File getTestFile(Path filename) {
298314
throw new RuntimeException("Unable to locate " + path);
299315
}
300316

301-
public static void resetContext(String[] args) {
302-
if (PythonTests.context != null) {
303-
PythonTests.context.close();
304-
}
305-
org.graalvm.polyglot.Context.Builder ctxBuilder = Context.newBuilder();
306-
ctxBuilder.allowAllAccess(true);
307-
PythonTests.outArray.reset();
308-
PythonTests.errArray.reset();
309-
ctxBuilder.out(PythonTests.outStream);
310-
ctxBuilder.err(PythonTests.errStream);
311-
ctxBuilder.arguments("python", args);
312-
PythonTests.context = ctxBuilder.build();
313-
PythonTests.context.initialize("python");
314-
}
315-
316317
public static void runScript(String[] args, File path, OutputStream out, OutputStream err) {
317318
try {
318319
PythonTests.getContext(args).eval(org.graalvm.polyglot.Source.newBuilder("python", path).build());

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,15 @@ protected CallTarget parse(ParsingRequest request) throws Exception {
207207
PythonContext context = this.getContextReference().get();
208208
PythonCore pythonCore = context.getCore();
209209
Source source = request.getSource();
210-
context.initializeMainModule(source.getPath());
210+
if (pythonCore.isInitialized()) {
211+
context.initializeMainModule(source.getPath());
211212

212-
// if we are running the interpreter, module 'site' is automatically imported
213-
if (source.isInteractive()) {
214-
CompilerAsserts.neverPartOfCompilation();
215-
// no frame required
216-
new ImportNode("site").execute(null);
213+
// if we are running the interpreter, module 'site' is automatically imported
214+
if (source.isInteractive()) {
215+
CompilerAsserts.neverPartOfCompilation();
216+
// no frame required
217+
new ImportNode("site").execute(null);
218+
}
217219
}
218220
RootNode root;
219221
try {
@@ -223,7 +225,11 @@ protected CallTarget parse(ParsingRequest request) throws Exception {
223225
Truffle.getRuntime().createCallTarget(new TopLevelExceptionHandler(this, e)).call();
224226
throw e;
225227
}
226-
return Truffle.getRuntime().createCallTarget(new TopLevelExceptionHandler(this, root));
228+
if (pythonCore.isInitialized()) {
229+
return Truffle.getRuntime().createCallTarget(new TopLevelExceptionHandler(this, root));
230+
} else {
231+
return Truffle.getRuntime().createCallTarget(root);
232+
}
227233
}
228234

229235
@Override
@@ -373,10 +379,15 @@ public static Source newSource(PythonContext ctxt, URL url, String name) throws
373379
private static <E1 extends Exception, E2 extends Exception, E3 extends Exception> Source newSource(PythonContext ctxt, Builder<E1, E2, E3> srcBuilder,
374380
String name) throws E1 {
375381
Builder<E1, RuntimeException, RuntimeException> newBuilder = srcBuilder.name(name).mimeType(MIME_TYPE);
376-
boolean internal = !ctxt.getCore().isInitialized() && !PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources);
382+
boolean coreIsInitialized = ctxt.getCore().isInitialized();
383+
boolean internal = !coreIsInitialized && !PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources);
377384
if (internal) {
378385
srcBuilder.internal();
379386
}
387+
if (!coreIsInitialized) {
388+
srcBuilder.cached(true);
389+
}
390+
srcBuilder.cached(true);
380391
return newBuilder.build();
381392
}
382393

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,16 @@
128128
import com.oracle.graal.python.runtime.PythonContext;
129129
import com.oracle.graal.python.runtime.PythonCore;
130130
import com.oracle.graal.python.runtime.PythonParser;
131-
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
132131
import com.oracle.graal.python.runtime.exception.PException;
133132
import com.oracle.graal.python.runtime.exception.PythonErrorType;
134133
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
135134
import com.oracle.truffle.api.CallTarget;
136135
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
137136
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
138-
import com.oracle.truffle.api.Truffle;
139137
import com.oracle.truffle.api.TruffleFile;
140138
import com.oracle.truffle.api.TruffleLanguage.Env;
141139
import com.oracle.truffle.api.interop.TruffleObject;
142140
import com.oracle.truffle.api.nodes.Node;
143-
import com.oracle.truffle.api.nodes.RootNode;
144141
import com.oracle.truffle.api.source.Source;
145142

146143
/**
@@ -606,13 +603,13 @@ private Source getSource(String basename, String prefix) {
606603
}
607604

608605
private void loadFile(String s, String prefix) {
609-
RootNode parsedModule = (RootNode) getParser().parse(ParserMode.File, this, getSource(s, prefix), null);
606+
Source source = getSource(s, prefix);
607+
CallTarget callTarget = getContext().getEnv().parse(source);
610608
PythonModule mod = lookupBuiltinModule(s);
611609
if (mod == null) {
612610
// use an anonymous module for the side-effects
613611
mod = factory().createPythonModule("__anonymous__");
614612
}
615-
CallTarget callTarget = Truffle.getRuntime().createCallTarget(parsedModule);
616613
callTarget.call(PArguments.withGlobals(mod));
617614
}
618615

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import com.oracle.graal.python.builtins.objects.function.PythonCallable;
8989
import com.oracle.graal.python.builtins.objects.ints.PInt;
9090
import com.oracle.graal.python.builtins.objects.list.PList;
91+
import com.oracle.graal.python.builtins.objects.module.PythonModule;
9192
import com.oracle.graal.python.builtins.objects.object.PythonObject;
9293
import com.oracle.graal.python.builtins.objects.set.PFrozenSet;
9394
import com.oracle.graal.python.builtins.objects.str.PString;
@@ -117,6 +118,7 @@
117118
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
118119
import com.oracle.graal.python.nodes.expression.TernaryArithmetic;
119120
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
121+
import com.oracle.graal.python.nodes.function.FunctionRootNode;
120122
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
121123
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
122124
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
@@ -1282,21 +1284,6 @@ private Object iterateGeneric(Object iterator, Object start, ConditionProfile er
12821284
}
12831285
}
12841286

1285-
@Builtin(name = "__load_builtins__", fixedNumOfArguments = 1)
1286-
@GenerateNodeFactory
1287-
public abstract static class LoadBuiltinsNode extends PythonBuiltinNode {
1288-
@Specialization
1289-
@TruffleBoundary
1290-
public Object doIt(String name) {
1291-
PythonModule mod = getCore().isInitialized() ? getContext().getBuiltins() : getCore().lookupBuiltinModule("builtins");
1292-
Source src = getCore().getCoreSource(name);
1293-
RootNode parsedModule = (RootNode) getCore().getParser().parse(ParserMode.File, getCore(), src, null);
1294-
CallTarget callTarget = Truffle.getRuntime().createCallTarget(parsedModule);
1295-
callTarget.call(PArguments.withGlobals(mod));
1296-
return PNone.NONE;
1297-
}
1298-
}
1299-
13001287
@Builtin(name = "__builtin__", fixedNumOfArguments = 1)
13011288
@GenerateNodeFactory
13021289
public abstract static class BuiltinNode extends PythonUnaryBuiltinNode {
@@ -1334,16 +1321,20 @@ public Object doIt(PFunction func) {
13341321
System.arraycopy(arity.getParameterIds(), 0, parameterIds, 1, parameterIds.length - 1);
13351322
Arity arityWithSelf = new Arity(name, arity.getMinNumOfArgs() + 1, arity.getMaxNumOfArgs() + 1, arity.takesKeywordArg(), arity.takesVarArgs(), parameterIds,
13361323
arity.getKeywordNames());
1337-
func.getFunctionRootNode().accept(new NodeVisitor() {
1338-
public boolean visit(Node node) {
1339-
if (node instanceof ReadVarArgsNode) {
1340-
node.replace(ReadVarArgsNode.create(((ReadVarArgsNode) node).getIndex() + 1, ((ReadVarArgsNode) node).isBuiltin()));
1341-
} else if (node instanceof ReadIndexedArgumentNode) {
1342-
node.replace(ReadIndexedArgumentNode.create(((ReadIndexedArgumentNode) node).getIndex() + 1));
1324+
FunctionRootNode functionRootNode = (FunctionRootNode) func.getFunctionRootNode();
1325+
if (!functionRootNode.isRewritten()) {
1326+
functionRootNode.setRewritten();
1327+
func.getFunctionRootNode().accept(new NodeVisitor() {
1328+
public boolean visit(Node node) {
1329+
if (node instanceof ReadVarArgsNode) {
1330+
node.replace(ReadVarArgsNode.create(((ReadVarArgsNode) node).getIndex() + 1, ((ReadVarArgsNode) node).isBuiltin()));
1331+
} else if (node instanceof ReadIndexedArgumentNode) {
1332+
node.replace(ReadIndexedArgumentNode.create(((ReadIndexedArgumentNode) node).getIndex() + 1));
1333+
}
1334+
return true;
13431335
}
1344-
return true;
1345-
}
1346-
});
1336+
});
1337+
}
13471338
builtinFunc = factory().createFunction(name, func.getEnclosingClassName(), arityWithSelf, Truffle.getRuntime().createCallTarget(func.getFunctionRootNode()),
13481339
func.getFrameDescriptor(), func.getGlobals(), func.getClosure());
13491340
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@
6969
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
7070
import com.oracle.graal.python.runtime.PythonContext;
7171
import com.oracle.graal.python.runtime.PythonCore;
72-
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
7372
import com.oracle.graal.python.runtime.exception.PException;
7473
import com.oracle.graal.python.runtime.exception.PythonErrorType;
7574
import com.oracle.truffle.api.CallTarget;
7675
import com.oracle.truffle.api.CompilerDirectives;
7776
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
78-
import com.oracle.truffle.api.Truffle;
7977
import com.oracle.truffle.api.TruffleFile;
8078
import com.oracle.truffle.api.TruffleLanguage.Env;
8179
import com.oracle.truffle.api.dsl.Cached;
@@ -92,7 +90,6 @@
9290
import com.oracle.truffle.api.interop.UnsupportedMessageException;
9391
import com.oracle.truffle.api.interop.UnsupportedTypeException;
9492
import com.oracle.truffle.api.nodes.Node;
95-
import com.oracle.truffle.api.nodes.RootNode;
9693
import com.oracle.truffle.api.source.Source;
9794

9895
@CoreFunctions(defineModule = "_imp")
@@ -355,11 +352,8 @@ public Object run(String path, PythonModule mod) {
355352
file = env.getTruffleFile(path);
356353
}
357354
Source src = PythonLanguage.newSource(ctxt, file, fileName);
358-
RootNode parsedModule = (RootNode) getCore().getParser().parse(ParserMode.File, getCore(), src, null);
359-
if (parsedModule != null) {
360-
CallTarget callTarget = Truffle.getRuntime().createCallTarget(parsedModule);
361-
callTarget.call(PArguments.withGlobals(mod));
362-
}
355+
CallTarget callTarget = env.parse(src);
356+
callTarget.call(PArguments.withGlobals(mod));
363357
} catch (PException e) {
364358
throw e;
365359
} catch (IOException | SecurityException e) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private PythonClass getDataClass(Object descr) {
344344
}
345345
}
346346

347-
@Builtin(name = SpecialMethodNames.__GETATTR__, fixedNumOfArguments = 3)
347+
@Builtin(name = SpecialMethodNames.__GETATTR__, fixedNumOfArguments = 2)
348348
@GenerateNodeFactory
349349
public abstract static class GetattrNode extends PythonBinaryBuiltinNode {
350350
@Specialization

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class FunctionRootNode extends PClosureFunctionRootNode implements CellSu
5454
private final SourceSection sourceSection;
5555
private final boolean isGenerator;
5656
private final ValueProfile generatorFrameProfile;
57+
private boolean isRewritten = false;
5758

5859
@Child private PNode body;
5960
private PNode uninitializedBody;
@@ -151,4 +152,12 @@ public String toString() {
151152
public SourceSection getSourceSection() {
152153
return sourceSection;
153154
}
155+
156+
public boolean isRewritten() {
157+
return isRewritten;
158+
}
159+
160+
public void setRewritten() {
161+
this.isRewritten = true;
162+
}
154163
}

0 commit comments

Comments
 (0)