Skip to content

Commit a11ed69

Browse files
committed
[GR-21577] IsNode is slow, after adding check if it is executed in Jython emulation mode.
1 parent c0f25ce commit a11ed69

File tree

4 files changed

+115
-4
lines changed

4 files changed

+115
-4
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.oracle.graal.python.nodes.object.GetClassNode;
6565
import com.oracle.graal.python.parser.PythonParserImpl;
6666
import com.oracle.graal.python.runtime.PythonContext;
67+
import com.oracle.graal.python.runtime.PythonContextOptions;
6768
import com.oracle.graal.python.runtime.PythonCore;
6869
import com.oracle.graal.python.runtime.PythonOptions;
6970
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
@@ -217,7 +218,7 @@ protected PythonContext createContext(Env env) {
217218
assert this.isWithThread == null || this.isWithThread == PythonOptions.isWithThread(env) : "conflicting thread options in the same language!";
218219
this.isWithThread = PythonOptions.isWithThread(env);
219220
Python3Core newCore = new Python3Core(new PythonParserImpl(env));
220-
final PythonContext context = new PythonContext(this, env, newCore);
221+
final PythonContext context = new PythonContext(this, env, newCore, PythonContextOptions.fromOptionValues(env.getOptions()));
221222
context.initializeHomeAndPrefixPaths(env, getLanguageHome());
222223
return context;
223224
}

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/runtime/PythonContext.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,15 @@ List<WeakReference<Thread>> getOwners() {
223223
// compat
224224
private final ThreadLocal<ArrayDeque<String>> currentImport = new ThreadLocal<>();
225225

226-
public PythonContext(PythonLanguage language, TruffleLanguage.Env env, PythonCore core) {
226+
private final PythonContextOptions options;
227+
228+
public PythonContext(PythonLanguage language, TruffleLanguage.Env env, PythonCore core, PythonContextOptions options) {
227229
this.language = language;
228230
this.core = core;
229231
this.env = env;
230232
this.resources = new PosixResources();
231233
this.handler = new AsyncHandler(language);
234+
this.options = options;
232235
if (env == null) {
233236
this.in = System.in;
234237
this.out = System.out;
@@ -377,6 +380,10 @@ public void patch(Env newEnv) {
377380
core.postInitialize();
378381
}
379382

383+
public boolean isJythonEmulated() {
384+
return options.isJythonEmulated();
385+
}
386+
380387
/**
381388
* During pre-initialization, we're also loading code from the Python standard library. Since
382389
* some of those modules may be packages, they will have their __path__ attribute set to the
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.runtime;
42+
43+
import com.oracle.truffle.api.CompilerAsserts;
44+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
45+
import org.graalvm.options.OptionKey;
46+
import org.graalvm.options.OptionValues;
47+
48+
public class PythonContextOptions {
49+
@CompilationFinal private OptionValues optionValues;
50+
51+
@CompilationFinal private boolean emulatedJython;
52+
53+
PythonContextOptions(OptionValues optionValues) {
54+
this.optionValues = optionValues;
55+
setOptionValues(optionValues);
56+
}
57+
58+
public static PythonContextOptions fromOptionValues(OptionValues optionValues) {
59+
return new PythonContextOptions(optionValues);
60+
}
61+
62+
private boolean readBooleanOption(OptionKey<Boolean> key) {
63+
return key.getValue(optionValues);
64+
}
65+
66+
public void setOptionValues(OptionValues newOptions) {
67+
CompilerAsserts.neverPartOfCompilation();
68+
optionValues = newOptions;
69+
cacheOptions();
70+
}
71+
72+
private void cacheOptions() {
73+
this.emulatedJython = readBooleanOption(PythonOptions.EmulateJython);
74+
}
75+
76+
public boolean isJythonEmulated() {
77+
return this.emulatedJython;
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
int hash = 7;
83+
hash = 53 * hash + (this.emulatedJython ? 1 : 0);
84+
return hash;
85+
}
86+
87+
@Override
88+
public boolean equals(Object obj) {
89+
if (this == obj) {
90+
return true;
91+
}
92+
if (obj == null) {
93+
return false;
94+
}
95+
if (getClass() != obj.getClass()) {
96+
return false;
97+
}
98+
final PythonContextOptions other = (PythonContextOptions) obj;
99+
if (this.emulatedJython != other.emulatedJython) {
100+
return false;
101+
}
102+
return true;
103+
}
104+
}

0 commit comments

Comments
 (0)