Skip to content

Commit 01b95ee

Browse files
committed
Embed Python3Core into PythonContext to avoid indirection
1 parent b030df9 commit 01b95ee

File tree

9 files changed

+68
-99
lines changed

9 files changed

+68
-99
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,7 @@ protected boolean patchContext(PythonContext context, Env newEnv) {
313313

314314
@Override
315315
protected PythonContext createContext(Env env) {
316-
Python3Core newCore = new Python3Core(new PythonParserImpl(env), env.isNativeAccessAllowed());
317-
final PythonContext context = new PythonContext(this, env, newCore);
316+
final PythonContext context = new PythonContext(this, env, new PythonParserImpl(env));
318317
context.initializeHomeAndPrefixPaths(env, getLanguageHome());
319318

320319
Object[] engineOptionsUnroll = this.engineOptionsStorage;
@@ -391,7 +390,7 @@ protected CallTarget parse(ParsingRequest request) {
391390
gil.release(context, wasAcquired);
392391
}
393392
}
394-
if (core.isInitialized()) {
393+
if (core.isCoreInitialized()) {
395394
return PythonUtils.getOrCreateCallTarget(new TopLevelExceptionHandler(this, root, source));
396395
} else {
397396
return PythonUtils.getOrCreateCallTarget(root);
@@ -718,7 +717,7 @@ public static Source newSource(PythonContext ctxt, TruffleFile src, String name)
718717
}
719718

720719
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder) throws IOException {
721-
boolean coreIsInitialized = ctxt.getCore().isInitialized();
720+
boolean coreIsInitialized = ctxt.getCore().isCoreInitialized();
722721
boolean internal = !coreIsInitialized && !ctxt.getLanguage().getEngineOption(PythonOptions.ExposeInternalSources);
723722
if (internal) {
724723
srcBuilder.internal(true);

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

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.util.regex.Matcher;
4545
import java.util.regex.Pattern;
4646

47-
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
4847
import org.graalvm.nativeimage.ImageInfo;
4948

5049
import com.oracle.graal.python.PythonLanguage;
@@ -265,6 +264,7 @@
265264
import com.oracle.graal.python.runtime.exception.PException;
266265
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
267266
import com.oracle.graal.python.runtime.interop.PythonMapScope;
267+
import com.oracle.graal.python.runtime.object.PythonObjectSlowPathFactory;
268268
import com.oracle.graal.python.util.PythonUtils;
269269
import com.oracle.graal.python.util.Supplier;
270270
import com.oracle.truffle.api.CallTarget;
@@ -282,9 +282,10 @@
282282

283283
/**
284284
* The core is intended to the immutable part of the interpreter, including most modules and most
285-
* types.
285+
* types. The core is embedded, using inheritance, into {@link PythonContext} to avoid indirection
286+
* through an extra field in the context.
286287
*/
287-
public final class Python3Core implements ParserErrorCallback {
288+
public abstract class Python3Core implements ParserErrorCallback {
288289
private static final TruffleLogger LOGGER = PythonLanguage.getLogger(Python3Core.class);
289290
private final String[] coreFiles;
290291

@@ -604,7 +605,6 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
604605

605606
private final PythonParser parser;
606607

607-
@CompilationFinal private PythonContext singletonContext;
608608
@CompilationFinal private Object globalScopeObject;
609609

610610
/*
@@ -613,43 +613,45 @@ private static PythonBuiltins[] initializeBuiltins(boolean nativeAccessAllowed)
613613
*/
614614
private volatile boolean initialized;
615615

616-
private PythonObjectSlowPathFactory objectFactory;
616+
private final PythonLanguage language;
617+
@CompilationFinal private PythonObjectSlowPathFactory objectFactory;
617618

618-
public Python3Core(PythonParser parser, boolean isNativeSupportAllowed) {
619+
public Python3Core(PythonLanguage language, PythonParser parser, boolean isNativeSupportAllowed) {
620+
this.language = language;
619621
this.parser = parser;
620622
this.builtins = initializeBuiltins(isNativeSupportAllowed);
621623
this.coreFiles = initializeCoreFiles();
622624
}
623625

624-
public PythonLanguage getLanguage() {
625-
return singletonContext.getLanguage();
626+
@Override
627+
public final PythonContext getContext() {
628+
// Small hack: we know that this is the only implementation of Python3Core
629+
return (PythonContext) this;
626630
}
627631

628-
@Override
629-
public PythonContext getContext() {
630-
return singletonContext;
632+
public final PythonLanguage getLanguage() {
633+
return language;
631634
}
632635

633-
public PythonParser getParser() {
636+
public final PythonParser getParser() {
634637
return parser;
635638
}
636639

637-
public PythonCodeSerializer getSerializer() {
640+
public final PythonCodeSerializer getSerializer() {
638641
return (PythonCodeSerializer) parser;
639642
}
640643

641644
/**
642645
* Checks whether the core is initialized.
643646
*/
644-
public boolean isInitialized() {
647+
public final boolean isCoreInitialized() {
645648
return initialized;
646649
}
647650

648651
/**
649652
* Load the core library and prepare all builtin classes and modules.
650653
*/
651-
public void initialize(PythonContext context) {
652-
singletonContext = context;
654+
public final void initialize(PythonContext context) {
653655
objectFactory = new PythonObjectSlowPathFactory(context.getAllocationReporter());
654656
initializeJavaCore();
655657
initializePython3Core(context.getCoreHomeOrFail());
@@ -678,7 +680,7 @@ private void initializePython3Core(String coreHome) {
678680
* eagerly when the context is initialized on the JVM or a new context is created on SVM, but is
679681
* omitted when the native image is generated.
680682
*/
681-
public void postInitialize() {
683+
public final void postInitialize() {
682684
if (!ImageInfo.inImageBuildtimeCode() || ImageInfo.inImageRuntimeCode()) {
683685
initialized = false;
684686

@@ -696,48 +698,48 @@ public void postInitialize() {
696698
}
697699

698700
@TruffleBoundary
699-
public PythonModule lookupBuiltinModule(String name) {
701+
public final PythonModule lookupBuiltinModule(String name) {
700702
return builtinModules.get(name);
701703
}
702704

703-
public PythonBuiltinClass lookupType(PythonBuiltinClassType type) {
705+
public final PythonBuiltinClass lookupType(PythonBuiltinClassType type) {
704706
assert builtinTypes[type.ordinal()] != null;
705707
return builtinTypes[type.ordinal()];
706708
}
707709

708710
@TruffleBoundary
709-
public String[] builtinModuleNames() {
711+
public final String[] builtinModuleNames() {
710712
return builtinModules.keySet().toArray(PythonUtils.EMPTY_STRING_ARRAY);
711713
}
712714

713-
public PythonModule getBuiltins() {
715+
public final PythonModule getBuiltins() {
714716
return builtinsModule;
715717
}
716718

717-
public PythonModule getSysModule() {
719+
public final PythonModule getSysModule() {
718720
return sysModule;
719721
}
720722

721-
public PDict getSysModules() {
723+
public final PDict getSysModules() {
722724
return sysModules;
723725
}
724726

725-
public PythonModule getImportlib() {
727+
public final PythonModule getImportlib() {
726728
return importlib;
727729
}
728730

729-
public void registerImportlib(PythonModule mod) {
731+
public final void registerImportlib(PythonModule mod) {
730732
if (importlib != null) {
731733
throw new IllegalStateException("importlib cannot be registered more than once");
732734
}
733735
importlib = mod;
734736
}
735737

736-
public PMethod getImportFunc() {
738+
public final PMethod getImportFunc() {
737739
return importFunc;
738740
}
739741

740-
public void registerImportFunc(PMethod func) {
742+
public final void registerImportFunc(PMethod func) {
741743
if (importFunc != null) {
742744
throw new IllegalStateException("__import__ func cannot be registered more than once");
743745
}
@@ -746,14 +748,14 @@ public void registerImportFunc(PMethod func) {
746748

747749
@Override
748750
@TruffleBoundary
749-
public void warn(PythonBuiltinClassType type, String format, Object... args) {
751+
public final void warn(PythonBuiltinClassType type, String format, Object... args) {
750752
WarningsModuleBuiltins.WarnNode.getUncached().warnFormat(null, null, type, 1, format, args);
751753
}
752754

753755
/**
754756
* Returns the stderr object or signals error when stderr is "lost".
755757
*/
756-
public Object getStderr() {
758+
public final Object getStderr() {
757759
try {
758760
return PyObjectLookupAttr.getUncached().execute(null, sysModule, "stderr");
759761
} catch (PException e) {
@@ -933,29 +935,24 @@ private void loadFile(String s, String prefix) {
933935
GenericInvokeNode.getUncached().execute(callTarget, PArguments.withGlobals(mod));
934936
}
935937

936-
public PythonObjectSlowPathFactory factory() {
938+
public final PythonObjectSlowPathFactory factory() {
937939
return objectFactory;
938940
}
939941

940-
public void setContext(PythonContext context) {
941-
assert singletonContext == null;
942-
singletonContext = context;
943-
}
944-
945-
public PInt getTrue() {
942+
public final PInt getTrue() {
946943
return pyTrue;
947944
}
948945

949-
public PInt getFalse() {
946+
public final PInt getFalse() {
950947
return pyFalse;
951948
}
952949

953-
public PFloat getNaN() {
950+
public final PFloat getNaN() {
954951
return pyNaN;
955952
}
956953

957954
@Override
958-
public RuntimeException raiseInvalidSyntax(PythonParser.ErrorType type, Source source, SourceSection section, String message, Object... arguments) {
955+
public final RuntimeException raiseInvalidSyntax(PythonParser.ErrorType type, Source source, SourceSection section, String message, Object... arguments) {
959956
CompilerDirectives.transferToInterpreter();
960957
Node location = new Node() {
961958
@Override
@@ -968,7 +965,7 @@ public SourceSection getSourceSection() {
968965

969966
@Override
970967
@TruffleBoundary
971-
public RuntimeException raiseInvalidSyntax(PythonParser.ErrorType type, Node location, String message, Object... arguments) {
968+
public final RuntimeException raiseInvalidSyntax(PythonParser.ErrorType type, Node location, String message, Object... arguments) {
972969
PBaseException instance;
973970
Object cls;
974971
switch (type) {
@@ -1017,15 +1014,15 @@ public RuntimeException raiseInvalidSyntax(PythonParser.ErrorType type, Node loc
10171014
throw PException.fromObject(instance, location, PythonOptions.isPExceptionWithJavaStacktrace(getLanguage()));
10181015
}
10191016

1020-
public Object getTopScopeObject() {
1017+
public final Object getTopScopeObject() {
10211018
return globalScopeObject;
10221019
}
10231020

1024-
public static final void writeInfo(String message) {
1021+
public static void writeInfo(String message) {
10251022
PythonLanguage.getLogger(Python3Core.class).fine(message);
10261023
}
10271024

1028-
public static final void writeInfo(Supplier<String> messageSupplier) {
1025+
public static void writeInfo(Supplier<String> messageSupplier) {
10291026
PythonLanguage.getLogger(Python3Core.class).fine(messageSupplier);
10301027
}
10311028
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3190,7 +3190,7 @@ Object doMissing(Object klass, PNone none) {
31903190
abstract static class DescriptorNode extends PythonBuiltinNode {
31913191
@TruffleBoundary
31923192
protected final void denyInstantiationAfterInitialization(String name) {
3193-
if (getCore().isInitialized()) {
3193+
if (getCore().isCoreInitialized()) {
31943194
throw PRaiseNode.raiseUncached(this, TypeError, ErrorMessages.CANNOT_CREATE_INSTANCES, name);
31953195
}
31963196
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ PCode compile(String expression, String filename, String mode, int kwFlags, Obje
818818
return PythonUtils.getOrCreateCallTarget((RootNode) getCore().getParser().parse(pm, kwOptimize, getCore(), source, null, null));
819819
}
820820
};
821-
if (getCore().isInitialized()) {
821+
if (getCore().isCoreInitialized()) {
822822
ct = createCode.get();
823823
} else {
824824
ct = getCore().getLanguage().cacheCode(filename, createCode);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private ReferenceQueue<Object> getWeakReferenceQueue() {
200200
ReferenceQueue<Object> queue = (ReferenceQueue<Object>) queueObject;
201201
return queue;
202202
} else {
203-
if (getContext().getCore().isInitialized()) {
203+
if (getContext().getCore().isCoreInitialized()) {
204204
CompilerDirectives.transferToInterpreterAndInvalidate();
205205
throw new IllegalStateException("the weak reference queue was modified!");
206206
} else {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/CodeNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static PCode createCode(PythonContext context, int flags, byte[] codedata
144144
};
145145

146146
PythonObjectFactory factory = context.getCore().factory();
147-
if (context.getCore().isInitialized() || isNotAModule) {
147+
if (context.getCore().isCoreInitialized() || isNotAModule) {
148148
return factory.createCode(createCode, flags, firstlineno, lnotab, filename);
149149
} else {
150150
RootCallTarget ct = (RootCallTarget) language.cacheCode(filename, createCode);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/PythonBuiltinClass.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public PythonBuiltinClass(PythonLanguage lang, PythonBuiltinClassType builtinCla
6969
@Override
7070
public void setAttribute(Object name, Object value) {
7171
CompilerAsserts.neverPartOfCompilation();
72-
if (name instanceof HiddenKey || !PythonContext.get(null).getCore().isInitialized()) {
72+
if (name instanceof HiddenKey || !PythonContext.get(null).getCore().isCoreInitialized()) {
7373
setAttributeUnsafe(name, value);
7474
} else {
7575
throw PRaiseNode.raiseUncached(null, TypeError, ErrorMessages.CANT_SET_ATTRIBUTES_OF_TYPE_S, this);
@@ -90,7 +90,7 @@ public final PythonBuiltinClassType getType() {
9090
@TruffleBoundary
9191
@Override
9292
public void onAttributeUpdate(String key, Object newValue) {
93-
assert !PythonContext.get(null).getCore().isInitialized();
93+
assert !PythonContext.get(null).getCore().isCoreInitialized();
9494
// Ideally, startup code should not create ASTs that rely on assumptions of props of
9595
// builtins. So there should be no assumptions to invalidate yet
9696
assert !getMethodResolutionOrder().invalidateAttributeInMROFinalAssumptions(key);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/control/TopLevelExceptionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public Object execute(VirtualFrame frame) {
154154
@TruffleBoundary
155155
private void checkInitialized() {
156156
Python3Core core = getContext().getCore();
157-
if (core.isInitialized() && PythonLanguage.MIME_TYPE.equals(source.getMimeType())) {
157+
if (core.isCoreInitialized() && PythonLanguage.MIME_TYPE.equals(source.getMimeType())) {
158158
getContext().initializeMainModule(source.getPath());
159159
}
160160
}

0 commit comments

Comments
 (0)