Skip to content

Commit b4eb977

Browse files
committed
refactor PythonOptions to allow folding on the fast path
1 parent 2c0cc8b commit b4eb977

27 files changed

+231
-341
lines changed

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import com.oracle.graal.python.parser.PythonParserImpl;
4949
import com.oracle.graal.python.runtime.PythonContext;
5050
import com.oracle.graal.python.runtime.PythonCore;
51-
import com.oracle.graal.python.runtime.PythonEngineOptions;
5251
import com.oracle.graal.python.runtime.PythonOptions;
5352
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
5453
import com.oracle.graal.python.runtime.exception.PException;
@@ -176,19 +175,12 @@ protected void finalizeContext(PythonContext context) {
176175

177176
@Override
178177
protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues newOptions) {
179-
return PythonEngineOptions.fromOptionValues(firstOptions).equals(PythonEngineOptions.fromOptionValues(newOptions));
180-
}
181-
182-
private boolean areOptionsCompatibleWithPreinitializedContext(OptionValues firstOptions, OptionValues newOptions) {
183-
return (areOptionsCompatible(firstOptions, newOptions) &&
184-
// disabling TRegex has an effect on the _sre Python functions that are
185-
// dynamically created
186-
firstOptions.get(PythonOptions.WithTRegex).equals(newOptions.get(PythonOptions.WithTRegex)));
178+
return PythonOptions.areOptionsCompatible(firstOptions, newOptions);
187179
}
188180

189181
@Override
190182
protected boolean patchContext(PythonContext context, Env newEnv) {
191-
if (!areOptionsCompatibleWithPreinitializedContext(context.getEnv().getOptions(), newEnv.getOptions())) {
183+
if (!areOptionsCompatible(context.getEnv().getOptions(), newEnv.getOptions())) {
192184
PythonCore.writeInfo("Cannot use preinitialized context.");
193185
return false;
194186
}
@@ -210,7 +202,7 @@ protected PythonContext createContext(Env env) {
210202

211203
@Override
212204
protected OptionDescriptors getOptionDescriptors() {
213-
return PythonOptions.createDescriptors();
205+
return PythonOptions.DESCRIPTORS;
214206
}
215207

216208
@Override
@@ -238,7 +230,7 @@ protected CallTarget parse(ParsingRequest request) {
238230
private RootNode doParse(PythonContext context, Source source) {
239231
ParserMode mode = null;
240232
if (source.isInteractive()) {
241-
if (PythonOptions.getOption(context, PythonOptions.TerminalIsInteractive)) {
233+
if (context.getOption(PythonOptions.TerminalIsInteractive)) {
242234
// if we run through our own launcher, the sys.__displayhook__ would provide the
243235
// printing
244236
mode = ParserMode.Statement;
@@ -491,7 +483,7 @@ public static Source newSource(PythonContext ctxt, TruffleFile src, String name)
491483

492484
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder) throws IOException {
493485
boolean coreIsInitialized = ctxt.getCore().isInitialized();
494-
boolean internal = !coreIsInitialized && !ctxt.areInternalSourcesExposed();
486+
boolean internal = !coreIsInitialized && !ctxt.getOption(PythonOptions.ExposeInternalSources);
495487
if (internal) {
496488
srcBuilder.internal(true);
497489
}

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ protected List<com.oracle.truffle.api.dsl.NodeFactory<? extends PythonBuiltinBas
219219
public void postInitialize(PythonCore core) {
220220
super.postInitialize(core);
221221
PythonModule builtinsModule = core.lookupBuiltinModule(BuiltinNames.BUILTINS);
222-
builtinsModule.setAttribute(__DEBUG__, !PythonOptions.getOption(core.getContext(), PythonOptions.PythonOptimizeFlag));
222+
builtinsModule.setAttribute(__DEBUG__, !core.getContext().getOption(PythonOptions.PythonOptimizeFlag));
223223
}
224224

225225
// abs(x)
@@ -808,7 +808,7 @@ public static GetAttrNode create() {
808808
public abstract Object executeWithArgs(VirtualFrame frame, Object primary, String name, Object defaultValue);
809809

810810
@SuppressWarnings("unused")
811-
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", guards = {"stringEquals(cachedName, name, stringProfile)", "isNoValue(defaultValue)"})
811+
@Specialization(limit = "getAttributeAccessInlineCacheMaxDepth()", guards = {"stringEquals(cachedName, name, stringProfile)", "isNoValue(defaultValue)"})
812812
public Object getAttrDefault(VirtualFrame frame, Object primary, String name, PNone defaultValue,
813813
@Cached("createBinaryProfile()") ConditionProfile stringProfile,
814814
@Cached("name") String cachedName,
@@ -817,7 +817,7 @@ public Object getAttrDefault(VirtualFrame frame, Object primary, String name, PN
817817
}
818818

819819
@SuppressWarnings("unused")
820-
@Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", guards = {"stringEquals(cachedName, name, stringProfile)", "!isNoValue(defaultValue)"})
820+
@Specialization(limit = "getAttributeAccessInlineCacheMaxDepth()", guards = {"stringEquals(cachedName, name, stringProfile)", "!isNoValue(defaultValue)"})
821821
Object getAttr(VirtualFrame frame, Object primary, String name, Object defaultValue,
822822
@Cached("createBinaryProfile()") ConditionProfile stringProfile,
823823
@Cached("name") String cachedName,
@@ -1106,18 +1106,11 @@ boolean isInstance(VirtualFrame frame, Object instance, PTuple clsTuple,
11061106
return false;
11071107
}
11081108

1109-
protected boolean emulateJython() {
1110-
if (emulateJython == null) {
1111-
CompilerDirectives.transferToInterpreterAndInvalidate();
1112-
emulateJython = getContext().isJythonEmulated();
1113-
}
1114-
return emulateJython;
1115-
}
1116-
11171109
@Fallback
11181110
boolean isInstance(VirtualFrame frame, Object instance, Object cls) {
1119-
TruffleLanguage.Env env = getContext().getEnv();
1120-
if (emulateJython() && env.isHostObject(cls)) {
1111+
PythonContext context = getContext();
1112+
TruffleLanguage.Env env = context.getEnv();
1113+
if (context.getOption(PythonOptions.EmulateJython) && env.isHostObject(cls)) {
11211114
Object hostCls = env.asHostObject(cls);
11221115
Object hostInstance = env.isHostObject(instance) ? env.asHostObject(instance) : instance;
11231116
return hostCls instanceof Class && ((Class<?>) hostCls).isAssignableFrom(hostInstance.getClass());

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ private void ensureCapiWasLoaded() {
273273
Object capi = null;
274274
try {
275275
SourceBuilder capiSrcBuilder = Source.newBuilder(LLVM_LANGUAGE, capiFile);
276-
if (!context.areInternalSourcesExposed()) {
276+
if (!context.getOption(PythonOptions.ExposeInternalSources)) {
277277
capiSrcBuilder.internal(true);
278278
}
279279
capi = context.getEnv().parseInternal(capiSrcBuilder.build()).call();
@@ -541,7 +541,7 @@ public abstract static class HasCachedCode extends PythonUnaryBuiltinNode {
541541
public boolean run(String modulename,
542542
@CachedContext(PythonLanguage.class) PythonContext ctxt,
543543
@CachedLanguage PythonLanguage lang) {
544-
boolean b = PythonOptions.getFlag(ctxt, PythonOptions.WithCachedSources) && lang.hasCachedCode(modulename);
544+
boolean b = ctxt.getOption(PythonOptions.WithCachedSources) && lang.hasCachedCode(modulename);
545545
if (b) {
546546
LOGGER.log(Level.FINEST, () -> "Cached code re-used for " + modulename);
547547
}
@@ -559,7 +559,7 @@ public Object run(String modulename,
559559
@CachedContext(PythonLanguage.class) PythonContext ctxt,
560560
@CachedLanguage PythonLanguage lang) {
561561
String[] modulePath = null;
562-
if (PythonOptions.getFlag(ctxt, PythonOptions.WithCachedSources)) {
562+
if (ctxt.getOption(PythonOptions.WithCachedSources)) {
563563
modulePath = lang.cachedCodeModulePath(modulename);
564564
}
565565
if (modulePath != null) {

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@
121121
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
122122
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
123123
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
124-
import com.oracle.graal.python.nodes.util.CoerceToIntegerNode;
125124
import com.oracle.graal.python.nodes.util.CastToJavaIntNode;
126125
import com.oracle.graal.python.nodes.util.CastToPathNode;
127126
import com.oracle.graal.python.nodes.util.ChannelNodes.ReadFromChannelNode;
127+
import com.oracle.graal.python.nodes.util.CoerceToIntegerNode;
128128
import com.oracle.graal.python.nodes.util.CoerceToJavaLongNode;
129129
import com.oracle.graal.python.runtime.PosixResources;
130130
import com.oracle.graal.python.runtime.PythonContext;
@@ -220,7 +220,7 @@ public class PosixModuleBuiltins extends PythonBuiltins {
220220
};
221221

222222
private static boolean terminalIsInteractive(PythonContext context) {
223-
return PythonOptions.getFlag(context, PythonOptions.TerminalIsInteractive);
223+
return context.getOption(PythonOptions.TerminalIsInteractive);
224224
}
225225

226226
@Override
@@ -283,7 +283,7 @@ public void postInitialize(PythonCore core) {
283283
// On Mac, the CPython launcher uses this env variable to specify the real Python
284284
// executable. It will be honored by packages like "site". So, if it is set, we
285285
// overwrite it with our executable to ensure that subprocesses will use us.
286-
value = PythonOptions.getOption(core.getContext(), PythonOptions.Executable);
286+
value = core.getContext().getOption(PythonOptions.Executable);
287287
} else {
288288
value = entry.getValue();
289289
}
@@ -1803,15 +1803,15 @@ PTuple getTerminalSize(VirtualFrame frame, @SuppressWarnings("unused") PNone fd)
18031803
if (getErrorProfile().profile(getContext().getResources().getFileChannel(0) == null)) {
18041804
throw raiseOSError(frame, OSErrorEnum.EBADF);
18051805
}
1806-
return factory().createTuple(new Object[]{PythonOptions.getTerminalWidth(), PythonOptions.getTerminalHeight()});
1806+
return factory().createTuple(new Object[]{getTerminalWidth(), getTerminalHeight()});
18071807
}
18081808

18091809
@Specialization
18101810
PTuple getTerminalSize(VirtualFrame frame, int fd) {
18111811
if (getErrorProfile().profile(getContext().getResources().getFileChannel(fd) == null)) {
18121812
throw raiseOSError(frame, OSErrorEnum.EBADF);
18131813
}
1814-
return factory().createTuple(new Object[]{PythonOptions.getTerminalWidth(), PythonOptions.getTerminalHeight()});
1814+
return factory().createTuple(new Object[]{getTerminalWidth(), getTerminalHeight()});
18151815
}
18161816

18171817
@Specialization
@@ -1822,7 +1822,7 @@ PTuple getTerminalSize(VirtualFrame frame, long fd) {
18221822
if (getErrorProfile().profile(getContext().getResources().getFileChannel((int) fd) == null)) {
18231823
throw raiseOSError(frame, OSErrorEnum.EBADF);
18241824
}
1825-
return factory().createTuple(new Object[]{PythonOptions.getTerminalWidth(), PythonOptions.getTerminalHeight()});
1825+
return factory().createTuple(new Object[]{getTerminalWidth(), getTerminalHeight()});
18261826
}
18271827

18281828
@Specialization
@@ -1836,7 +1836,15 @@ PTuple getTerminalSize(VirtualFrame frame, PInt fd) {
18361836
} catch (ArithmeticException e) {
18371837
throw raise(PythonErrorType.OverflowError, "Python int too large to convert to C long");
18381838
}
1839-
return factory().createTuple(new Object[]{PythonOptions.getTerminalWidth(), PythonOptions.getTerminalHeight()});
1839+
return factory().createTuple(new Object[]{getTerminalWidth(), getTerminalHeight()});
1840+
}
1841+
1842+
private int getTerminalWidth() {
1843+
return getContext().getOption(PythonOptions.TerminalWidth);
1844+
}
1845+
1846+
private int getTerminalHeight() {
1847+
return getContext().getOption(PythonOptions.TerminalHeight);
18401848
}
18411849

18421850
@Fallback

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ private synchronized int forkExec(PList args, @SuppressWarnings("unused") PList
142142
// that actually gives you a whole cmdline, which we need to split up for process
143143
// builder
144144
if (!argStrings.isEmpty()) {
145-
if (argStrings.get(0).equals(PythonOptions.getOption(context, PythonOptions.Executable))) {
146-
String[] executableList = PythonOptions.getExecutableList();
145+
if (argStrings.get(0).equals(context.getOption(PythonOptions.Executable))) {
146+
String[] executableList = PythonOptions.getExecutableList(context);
147147
argStrings.remove(0);
148148
for (int i = executableList.length - 1; i >= 0; i--) {
149149
argStrings.add(0, executableList[i]);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
6565
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
6666
import com.oracle.graal.python.runtime.PythonContext;
67+
import com.oracle.graal.python.runtime.PythonOptions;
6768
import com.oracle.graal.python.runtime.exception.PythonErrorType;
6869
import com.oracle.truffle.api.CompilerAsserts;
6970
import com.oracle.truffle.api.CompilerDirectives;
@@ -95,13 +96,17 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9596
@Builtin(name = "_build_regex_engine", minNumOfPositionalArgs = 1)
9697
@GenerateNodeFactory
9798
abstract static class BuildRegexEngine extends PythonUnaryBuiltinNode {
98-
@Specialization(guards = "!getFlag(context, WithTRegex)")
99+
protected static boolean withTRegex(PythonContext context) {
100+
return context.getOption(PythonOptions.WithTRegex);
101+
}
102+
103+
@Specialization(guards = "!withTRegex(context)")
99104
Object useSRE(@SuppressWarnings("unused") String code,
100105
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context) {
101106
return PNone.NONE;
102107
}
103108

104-
@Specialization(guards = "getFlag(context, WithTRegex)")
109+
@Specialization(guards = "withTRegex(context)")
105110
@TruffleBoundary
106111
Object run(String code,
107112
@SuppressWarnings("unused") @CachedContext(PythonLanguage.class) PythonContext context) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ abstract static class SelectNode extends PythonBuiltinNode {
6868
@Specialization
6969
@TruffleBoundary
7070
PTuple select(Object rlist, Object wlist, Object xlist, @SuppressWarnings("unused") Object timeout) {
71-
if (PythonOptions.getFlag(getContext(), PythonOptions.VerboseFlag)) {
71+
if (getContext().getOption(PythonOptions.VerboseFlag)) {
7272
new PrintStream(getContext().getEnv().err()).println("select() will always return immediately, we only support blocking I/O for now");
7373
}
7474
return factory().createTuple(new Object[]{rlist, wlist, xlist});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242

4343
import java.util.List;
4444

45-
import org.graalvm.collections.EconomicMap;
46-
4745
import com.oracle.graal.python.builtins.Builtin;
4846
import com.oracle.graal.python.builtins.CoreFunctions;
4947
import com.oracle.graal.python.builtins.PythonBuiltins;
@@ -56,6 +54,8 @@
5654
import com.oracle.truffle.api.dsl.NodeFactory;
5755
import com.oracle.truffle.api.dsl.Specialization;
5856

57+
import org.graalvm.collections.EconomicMap;
58+
5959
/**
6060
* this builtin module is used to fill in truffle land config options into the sysconfig python
6161
* module
@@ -67,7 +67,7 @@ public class SysConfigModuleBuiltins extends PythonBuiltins {
6767
@Override
6868
public void initialize(PythonCore core) {
6969
super.initialize(core);
70-
STATIC_CONFIG_OPTIONS.put("WITH_THREAD", PythonOptions.isWithThread(core.getContext().getEnv()) ? 1 : 0);
70+
STATIC_CONFIG_OPTIONS.put("WITH_THREAD", core.getContext().getOption(PythonOptions.WithThread) ? 1 : 0);
7171
}
7272

7373
@Override

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,40 +193,40 @@ public void postInitialize(PythonCore core) {
193193
String capiHome = context.getCAPIHome();
194194

195195
if (!ImageInfo.inImageBuildtimeCode()) {
196-
sys.setAttribute("executable", PythonOptions.getOption(context, PythonOptions.Executable));
197-
sys.setAttribute("_base_executable", PythonOptions.getOption(context, PythonOptions.Executable));
196+
sys.setAttribute("executable", context.getOption(PythonOptions.Executable));
197+
sys.setAttribute("_base_executable", context.getOption(PythonOptions.Executable));
198198
sys.setAttribute("graal_python_home", context.getLanguage().getHome());
199199
}
200-
sys.setAttribute("graal_python_jython_emulation_enabled", context.isJythonEmulated());
200+
sys.setAttribute("graal_python_jython_emulation_enabled", context.getOption(PythonOptions.EmulateJython));
201201
sys.setAttribute("graal_python_host_import_enabled", context.getEnv().isHostLookupAllowed());
202202
sys.setAttribute("graal_python_core_home", coreHome);
203203
sys.setAttribute("graal_python_stdlib_home", stdlibHome);
204204
sys.setAttribute("graal_python_capi_home", capiHome);
205205
sys.setAttribute("__flags__", core.factory().createTuple(new Object[]{
206206
false, // bytes_warning
207-
!PythonOptions.getFlag(context, PythonOptions.PythonOptimizeFlag), // debug
207+
!context.getOption(PythonOptions.PythonOptimizeFlag), // debug
208208
true, // dont_write_bytecode
209209
false, // hash_randomization
210-
PythonOptions.getFlag(context, PythonOptions.IgnoreEnvironmentFlag), // ignore_environment
211-
PythonOptions.getFlag(context, PythonOptions.InspectFlag), // inspect
212-
PythonOptions.getFlag(context, PythonOptions.TerminalIsInteractive), // interactive
213-
PythonOptions.getFlag(context, PythonOptions.IsolateFlag), // isolated
214-
PythonOptions.getFlag(context, PythonOptions.NoSiteFlag), // no_site
215-
PythonOptions.getFlag(context, PythonOptions.NoUserSiteFlag), // no_user_site
216-
PythonOptions.getFlag(context, PythonOptions.PythonOptimizeFlag), // optimize
217-
PythonOptions.getFlag(context, PythonOptions.QuietFlag), // quiet
218-
PythonOptions.getFlag(context, PythonOptions.VerboseFlag), // verbose
210+
context.getOption(PythonOptions.IgnoreEnvironmentFlag), // ignore_environment
211+
context.getOption(PythonOptions.InspectFlag), // inspect
212+
context.getOption(PythonOptions.TerminalIsInteractive), // interactive
213+
context.getOption(PythonOptions.IsolateFlag), // isolated
214+
context.getOption(PythonOptions.NoSiteFlag), // no_site
215+
context.getOption(PythonOptions.NoUserSiteFlag), // no_user_site
216+
context.getOption(PythonOptions.PythonOptimizeFlag), // optimize
217+
context.getOption(PythonOptions.QuietFlag), // quiet
218+
context.getOption(PythonOptions.VerboseFlag), // verbose
219219
false, // dev_mode
220220
0, // utf8_mode
221221
}));
222222

223223
Env env = context.getEnv();
224-
String option = PythonOptions.getOption(context, PythonOptions.PythonPath);
224+
String option = context.getOption(PythonOptions.PythonPath);
225225

226226
LanguageInfo llvmInfo = env.getInternalLanguages().get(LLVM_LANGUAGE);
227227
Toolchain toolchain = env.lookup(llvmInfo, Toolchain.class);
228228

229-
boolean isIsolated = PythonOptions.getOption(context, PythonOptions.IsolateFlag);
229+
boolean isIsolated = context.getOption(PythonOptions.IsolateFlag);
230230
boolean capiSeparate = !capiHome.equals(coreHome);
231231

232232
Object[] path;

0 commit comments

Comments
 (0)