Skip to content

Commit 0fcbd43

Browse files
committed
WIP: make ast shareable, ripping out shared core and only keeping context references in the AST
1 parent fb566c7 commit 0fcbd43

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+617
-986
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public static File getBenchFile(Path filename) {
225225

226226
public static PythonContext getContext() {
227227
PythonTests.ensureContext();
228-
return PythonLanguage.getContext();
228+
return PythonLanguage.getContextRef().get();
229229
}
230230

231231
static Context getContext(String[] args) {
@@ -263,7 +263,7 @@ private static String getFileContent(File file) {
263263

264264
public static RootNode getParseResult(com.oracle.truffle.api.source.Source source, PrintStream out, PrintStream err) {
265265
PythonTests.ensureContext();
266-
PythonContext ctx = PythonLanguage.getContext();
266+
PythonContext ctx = PythonLanguage.getContextRef().get();
267267
ctx.setOut(out);
268268
ctx.setErr(err);
269269
return (RootNode) ctx.getCore().getParser().parse(ParserMode.File, ctx.getCore(), source, null);

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

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public final class PythonLanguage extends TruffleLanguage<PythonContext> {
8989
public static final String EXTENSION = ".py";
9090

9191
@CompilationFinal private boolean nativeBuildTime = TruffleOptions.AOT;
92-
@CompilationFinal private PythonCore sharedCore;
9392
private final NodeFactory nodeFactory;
9493

9594
public PythonLanguage() {
@@ -109,47 +108,17 @@ protected void finalizeContext(PythonContext context) {
109108
@Override
110109
protected boolean patchContext(PythonContext context, Env newEnv) {
111110
nativeBuildTime = false; // now we're running
112-
ensureSysExecutable(context);
113111
ensureHomeInOptions(newEnv);
114-
if (!optionsAllowPreInitializedContext(context, newEnv)) {
115-
// Incompatible options - cannot use pre-initialized context
116-
return false;
117-
}
118-
context.setEnv(newEnv);
119-
context.setOut(newEnv.out());
120-
context.setErr(newEnv.err());
121-
context.initialize();
122-
context.getCore().postInitialize();
112+
PythonCore.writeInfo(newEnv, "Using preinitialized context.");
113+
context.patch(newEnv);
123114
return true;
124115
}
125116

126-
private static void ensureSysExecutable(PythonContext context) {
127-
PythonModule sys = context.getCore().lookupBuiltinModule("sys");
128-
sys.setAttribute("executable", Compiler.command(new Object[]{"com.oracle.svm.core.posix.GetExecutableName"}));
129-
}
130-
131-
private static boolean optionsAllowPreInitializedContext(PythonContext context, Env newEnv) {
132-
// Verify that the option for using a shared core is the same as
133-
// at image building time
134-
final boolean useSharedCore = newEnv.getOptions().get(PythonOptions.SharedCore);
135-
boolean canUsePreinitializedContext = context.getCore().hasSingletonContext() == !useSharedCore;
136-
if (canUsePreinitializedContext) {
137-
PythonCore.writeInfo(newEnv, "Using preinitialized context.");
138-
} else {
139-
PythonCore.writeInfo(newEnv, "Not using preinitialized context.");
140-
}
141-
return canUsePreinitializedContext;
142-
}
143-
144117
@Override
145118
protected PythonContext createContext(Env env) {
146119
ensureHomeInOptions(env);
147-
if (env.getOptions().get(PythonOptions.SharedCore) && sharedCore != null) {
148-
return new PythonContext(this, env, sharedCore);
149-
} else {
150-
Python3Core newCore = new Python3Core(this, new PythonParserImpl());
151-
return new PythonContext(this, env, newCore);
152-
}
120+
Python3Core newCore = new Python3Core(new PythonParserImpl());
121+
return new PythonContext(this, env, newCore);
153122
}
154123

155124
private void ensureHomeInOptions(Env env) {
@@ -230,31 +199,13 @@ protected OptionDescriptors getOptionDescriptors() {
230199

231200
@Override
232201
protected void initializeContext(PythonContext context) throws Exception {
233-
Python3Core core = (Python3Core) getCore();
234-
boolean fullInit = !PythonOptions.getOption(context, PythonOptions.LazyInit);
235-
if (context.getOptions().get(PythonOptions.SharedCore)) {
236-
if (sharedCore == null) {
237-
sharedCore = context.getCore();
238-
core.bootstrap();
239-
} else {
240-
fullInit = false;
241-
}
242-
} else {
243-
core.bootstrap();
244-
}
245202
context.initialize();
246-
if (fullInit) {
247-
core.initialize();
248-
}
249203
}
250204

251205
@Override
252206
protected CallTarget parse(ParsingRequest request) throws Exception {
253207
PythonContext context = this.getContextReference().get();
254208
PythonCore pythonCore = context.getCore();
255-
if (!pythonCore.isInitialized()) {
256-
pythonCore.initialize();
257-
}
258209
Source source = request.getSource();
259210
context.initializeMainModule(source.getPath());
260211

@@ -346,8 +297,16 @@ protected Object findMetaObject(PythonContext context, Object value) {
346297
return null;
347298
}
348299

349-
public static PythonContext getContext() {
350-
return getCurrentContext(PythonLanguage.class);
300+
public static PythonLanguage getCurrent() {
301+
return getCurrentLanguage(PythonLanguage.class);
302+
}
303+
304+
public static ContextReference<PythonContext> getContextRef() {
305+
return getCurrentLanguage(PythonLanguage.class).getContextReference();
306+
}
307+
308+
public static PythonCore getCore() {
309+
return getCurrentContext(PythonLanguage.class).getCore();
351310
}
352311

353312
@Override
@@ -395,11 +354,6 @@ protected String toString(PythonContext context, Object value) {
395354
return res.toString();
396355
}
397356

398-
public static PythonCore getCore() {
399-
PythonCore core = getCurrentLanguage(PythonLanguage.class).sharedCore;
400-
return core != null ? core : getContext().getCore();
401-
}
402-
403357
public static TruffleLogger getLogger() {
404358
return TruffleLogger.getLogger(ID, PythonLanguage.class);
405359
}
@@ -419,7 +373,7 @@ public static Source newSource(PythonContext ctxt, URL url, String name) throws
419373
private static <E1 extends Exception, E2 extends Exception, E3 extends Exception> Source newSource(PythonContext ctxt, Builder<E1, E2, E3> srcBuilder,
420374
String name) throws E1 {
421375
Builder<E1, RuntimeException, RuntimeException> newBuilder = srcBuilder.name(name).mimeType(MIME_TYPE);
422-
boolean internal = !ctxt.getCore().isInitialized() && !PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources) && !PythonOptions.getOption(ctxt, PythonOptions.LazyInit);
376+
boolean internal = !ctxt.getCore().isInitialized() && !PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources);
423377
if (internal) {
424378
srcBuilder.internal();
425379
}

0 commit comments

Comments
 (0)