Skip to content

Commit 2bfe6d1

Browse files
committed
[GR-21577] IsNode is slow, after adding check if it is executed in Jython emulation mode.
PullRequest: graalpython/837
2 parents 0b9d29e + 496af80 commit 2bfe6d1

File tree

11 files changed

+174
-40
lines changed

11 files changed

+174
-40
lines changed

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
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;
5152
import com.oracle.graal.python.runtime.PythonOptions;
5253
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
5354
import com.oracle.graal.python.runtime.exception.PException;
@@ -173,16 +174,7 @@ protected void finalizeContext(PythonContext context) {
173174

174175
@Override
175176
protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues newOptions) {
176-
// internal sources were marked during context initialization
177-
return (firstOptions.get(PythonOptions.ExposeInternalSources).equals(newOptions.get(PythonOptions.ExposeInternalSources)) &&
178-
// we cache WithThread on the language
179-
firstOptions.get(PythonOptions.WithThread).equals(newOptions.get(PythonOptions.WithThread)) &&
180-
// we cache JythonEmulation on nodes
181-
firstOptions.get(PythonOptions.EmulateJython).equals(newOptions.get(PythonOptions.EmulateJython)) &&
182-
// we cache CatchAllExceptions hard on TryExceptNode
183-
firstOptions.get(PythonOptions.CatchAllExceptions).equals(newOptions.get(PythonOptions.CatchAllExceptions)) &&
184-
// we cache BuiltinsInliningMaxCallerSize on the language
185-
firstOptions.get(PythonOptions.BuiltinsInliningMaxCallerSize).equals(newOptions.get(PythonOptions.BuiltinsInliningMaxCallerSize)));
177+
return PythonEngineOptions.fromOptionValues(firstOptions).equals(PythonEngineOptions.fromOptionValues(newOptions));
186178
}
187179

188180
private boolean areOptionsCompatibleWithPreinitializedContext(OptionValues firstOptions, OptionValues newOptions) {
@@ -497,7 +489,7 @@ public static Source newSource(PythonContext ctxt, TruffleFile src, String name)
497489

498490
private static Source newSource(PythonContext ctxt, SourceBuilder srcBuilder) throws IOException {
499491
boolean coreIsInitialized = ctxt.getCore().isInitialized();
500-
boolean internal = !coreIsInitialized && !PythonOptions.getOption(ctxt, PythonOptions.ExposeInternalSources);
492+
boolean internal = !coreIsInitialized && !ctxt.areInternalSourcesExposed();
501493
if (internal) {
502494
srcBuilder.internal(true);
503495
}

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
@@ -1107,7 +1107,7 @@ boolean isInstance(VirtualFrame frame, Object instance, PTuple clsTuple,
11071107
protected boolean emulateJython() {
11081108
if (emulateJython == null) {
11091109
CompilerDirectives.transferToInterpreterAndInvalidate();
1110-
emulateJython = PythonOptions.getFlag(getContext(), PythonOptions.EmulateJython);
1110+
emulateJython = getContext().isJythonEmulated();
11111111
}
11121112
return emulateJython;
11131113
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ private void ensureCapiWasLoaded() {
270270
Object capi = null;
271271
try {
272272
SourceBuilder capiSrcBuilder = Source.newBuilder(LLVM_LANGUAGE, capiFile);
273-
if (!PythonOptions.getOption(context, PythonOptions.ExposeInternalSources)) {
273+
if (!context.areInternalSourcesExposed()) {
274274
capiSrcBuilder.internal(true);
275275
}
276276
capi = context.getEnv().parseInternal(capiSrcBuilder.build()).call();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void postInitialize(PythonCore core) {
196196
sys.setAttribute("executable", PythonOptions.getOption(context, PythonOptions.Executable));
197197
sys.setAttribute("graal_python_home", context.getLanguage().getHome());
198198
}
199-
sys.setAttribute("graal_python_jython_emulation_enabled", PythonOptions.getOption(context, PythonOptions.EmulateJython));
199+
sys.setAttribute("graal_python_jython_emulation_enabled", context.isJythonEmulated());
200200
sys.setAttribute("graal_python_host_import_enabled", context.getEnv().isHostLookupAllowed());
201201
sys.setAttribute("graal_python_core_home", coreHome);
202202
sys.setAttribute("graal_python_stdlib_home", stdlibHome);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallSpecialMethodNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private <T extends PythonBuiltinBaseNode> boolean callerExceedsMaxSize(T builtin
111111
int n = root instanceof PRootNode ? ((PRootNode) root).getNodeCount() : NodeUtil.countNodes(root);
112112
// nb: option 'BuiltinsInliningMaxCallerSize' is defined as a compatible option, i.e.,
113113
// ASTs will only we shared between contexts that have the same value for this option.
114-
int maxSize = PythonOptions.getOption(lookupContextReference(PythonLanguage.class).get(), PythonOptions.BuiltinsInliningMaxCallerSize);
114+
int maxSize = lookupContextReference(PythonLanguage.class).get().getBuiltinsInliningMaxCallerSize();
115115
if (n >= maxSize || n + NodeUtil.countNodes(builtinNode) >= maxSize) {
116116
maxSizeExceeded = true;
117117
return true;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/IsExpressionNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import com.oracle.graal.python.builtins.objects.ints.PInt;
5252
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
5353
import com.oracle.graal.python.runtime.PythonContext;
54-
import com.oracle.graal.python.runtime.PythonOptions;
5554
import com.oracle.truffle.api.RootCallTarget;
5655
import com.oracle.truffle.api.dsl.Cached;
5756
import com.oracle.truffle.api.dsl.CachedContext;
@@ -218,7 +217,7 @@ boolean doCode(PCode left, PCode right) {
218217
@Specialization
219218
boolean doObjectPNone(Object left, PNone right,
220219
@Cached.Shared("ctxt") @CachedContext(PythonLanguage.class) PythonContext ctxt) {
221-
if (PythonOptions.getFlag(ctxt, PythonOptions.EmulateJython) && ctxt.getEnv().isHostObject(left) && ctxt.getEnv().asHostObject(left) == null &&
220+
if (ctxt.isJythonEmulated() && ctxt.getEnv().isHostObject(left) && ctxt.getEnv().asHostObject(left) == null &&
222221
right == PNone.NONE) {
223222
return true;
224223
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -52,9 +52,7 @@
5252
import com.oracle.graal.python.nodes.BuiltinNames;
5353
import com.oracle.graal.python.nodes.call.CallNode;
5454
import com.oracle.graal.python.nodes.object.GetDictNode;
55-
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.PassCaughtExceptionNode;
5655
import com.oracle.graal.python.runtime.PythonContext;
57-
import com.oracle.graal.python.runtime.PythonOptions;
5856
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5957
import com.oracle.truffle.api.CompilerDirectives;
6058
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -69,7 +67,6 @@ public abstract class AbstractImportNode extends StatementNode {
6967

7068
@Child private CallNode callNode;
7169
@Child private GetDictNode getDictNode;
72-
@Child private PassCaughtExceptionNode passExceptionNode;
7370
@CompilationFinal private Boolean emulateJython;
7471

7572
public AbstractImportNode() {
@@ -156,7 +153,7 @@ Object __import__(VirtualFrame frame, String name, Object globals, String[] from
156153
protected boolean emulateJython() {
157154
if (emulateJython == null) {
158155
CompilerDirectives.transferToInterpreterAndInvalidate();
159-
emulateJython = PythonOptions.getFlag(getContext(), PythonOptions.EmulateJython);
156+
emulateJython = getContext().isJythonEmulated();
160157
}
161158
return emulateJython;
162159
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void executeVoid(VirtualFrame frame) {
6666
if (assertionsEnabled == null) {
6767
CompilerDirectives.transferToInterpreterAndInvalidate();
6868
PythonContext context = getContext();
69-
javaExceptionsFailAssertions = PythonOptions.getOption(context, PythonOptions.CatchAllExceptions);
69+
javaExceptionsFailAssertions = context.isCatchingAllExcetptionsEnabled();
7070
assertionsEnabled = !PythonOptions.getOption(context, PythonOptions.PythonOptimizeFlag);
7171
}
7272
if (assertionsEnabled) {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -25,9 +25,6 @@
2525
*/
2626
package com.oracle.graal.python.nodes.statement;
2727

28-
import static com.oracle.graal.python.runtime.PythonOptions.CatchAllExceptions;
29-
import static com.oracle.graal.python.runtime.PythonOptions.EmulateJython;
30-
3128
import java.util.ArrayList;
3229

3330
import com.oracle.graal.python.PythonLanguage;
@@ -47,7 +44,6 @@
4744
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.RestoreExceptionStateNode;
4845
import com.oracle.graal.python.nodes.util.ExceptionStateNodes.SaveExceptionStateNode;
4946
import com.oracle.graal.python.runtime.PythonContext;
50-
import com.oracle.graal.python.runtime.PythonOptions;
5147
import com.oracle.graal.python.runtime.exception.ExceptionHandledException;
5248
import com.oracle.graal.python.runtime.exception.PException;
5349
import com.oracle.graal.python.runtime.exception.PythonErrorType;
@@ -130,7 +126,7 @@ public void executeVoid(VirtualFrame frame) {
130126
} catch (Exception e) {
131127
if (shouldCatchJavaExceptions == null) {
132128
CompilerDirectives.transferToInterpreterAndInvalidate();
133-
shouldCatchJavaExceptions = PythonOptions.getOption(getContext(), EmulateJython);
129+
shouldCatchJavaExceptions = getContext().isJythonEmulated();
134130
}
135131
if (shouldCatchJavaExceptions && getContext().getEnv().isHostException(e)) {
136132
if (catchException(frame, (TruffleException) e, exceptionState)) {
@@ -139,7 +135,7 @@ public void executeVoid(VirtualFrame frame) {
139135
}
140136
if (shouldCatchAll == null) {
141137
CompilerDirectives.transferToInterpreterAndInvalidate();
142-
shouldCatchAll = PythonOptions.getOption(getContext(), CatchAllExceptions);
138+
shouldCatchAll = getContext().isCatchingAllExcetptionsEnabled();
143139
}
144140
if (shouldCatchAll) {
145141
if (e instanceof ControlFlowException) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonContext.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,19 @@ List<WeakReference<Thread>> getOwners() {
222222
// compat
223223
private final ThreadLocal<ArrayDeque<String>> currentImport = new ThreadLocal<>();
224224

225+
@CompilationFinal private PythonEngineOptions engineOptions;
226+
225227
public PythonContext(PythonLanguage language, TruffleLanguage.Env env, PythonCore core) {
226228
this.language = language;
227229
this.core = core;
228230
this.env = env;
229231
this.resources = new PosixResources();
230232
this.handler = new AsyncHandler(language);
231-
if (env == null) {
232-
this.in = System.in;
233-
this.out = System.out;
234-
this.err = System.err;
235-
} else {
236-
this.resources.setEnv(env);
237-
this.in = env.in();
238-
this.out = env.out();
239-
this.err = env.err();
240-
}
233+
this.engineOptions = PythonEngineOptions.fromOptionValues(env.getOptions());
234+
this.resources.setEnv(env);
235+
this.in = env.in();
236+
this.out = env.out();
237+
this.err = env.err();
241238
}
242239

243240
public ThreadGroup getThreadGroup() {
@@ -293,6 +290,7 @@ public void setEnv(TruffleLanguage.Env newEnv) {
293290
out = env.out();
294291
err = env.err();
295292
resources.setEnv(env);
293+
engineOptions = PythonEngineOptions.fromOptionValues(newEnv.getOptions());
296294
}
297295

298296
/**
@@ -376,6 +374,22 @@ public void patch(Env newEnv) {
376374
core.postInitialize();
377375
}
378376

377+
public boolean isCatchingAllExcetptionsEnabled() {
378+
return engineOptions.isCatchingAllExcetptionsEnabled();
379+
}
380+
381+
public boolean areInternalSourcesExposed() {
382+
return engineOptions.areInternalSourcesExposed();
383+
}
384+
385+
public boolean isJythonEmulated() {
386+
return engineOptions.isJythonEmulated();
387+
}
388+
389+
public int getBuiltinsInliningMaxCallerSize() {
390+
return engineOptions.getBuiltinsInliningMaxCallerSize();
391+
}
392+
379393
/**
380394
* During pre-initialization, we're also loading code from the Python standard library. Since
381395
* some of those modules may be packages, they will have their __path__ attribute set to the

0 commit comments

Comments
 (0)