Skip to content

Commit 84b401d

Browse files
committed
Avoid repeated PythonLanguage lookup in PythonObjectSlowPathFactory
1 parent 407d8e3 commit 84b401d

File tree

4 files changed

+81
-8
lines changed

4 files changed

+81
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ public final boolean isCoreInitialized() {
798798
* Load the core library and prepare all builtin classes and modules.
799799
*/
800800
public final void initialize(PythonContext context) {
801-
objectFactory = new PythonObjectSlowPathFactory(context.getAllocationReporter());
801+
objectFactory = new PythonObjectSlowPathFactory(context.getAllocationReporter(), context.getLanguage());
802802
initializeJavaCore();
803803
initializePython3Core(context.getCoreHomeOrFail());
804804
assert SpecialMethodSlot.checkSlotOverrides(this);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -281,12 +281,12 @@ protected AllocationReporter getAllocationReporter() {
281281
return PythonContext.get(this).getAllocationReporter();
282282
}
283283

284-
public final PythonLanguage getLanguage() {
284+
public PythonLanguage getLanguage() {
285285
return PythonLanguage.get(this);
286286
}
287287

288288
public final Shape getShape(PythonBuiltinClassType cls) {
289-
return cls.getInstanceShape(PythonLanguage.get(this));
289+
return cls.getInstanceShape(getLanguage());
290290
}
291291

292292
public final Shape getShape(Object cls) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectSlowPathFactory.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022, 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
@@ -42,7 +42,7 @@
4242

4343
import java.util.Objects;
4444

45-
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory.GetInstanceShapeNodeGen;
45+
import com.oracle.graal.python.PythonLanguage;
4646
import com.oracle.graal.python.runtime.PythonContext;
4747
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4848
import com.oracle.truffle.api.instrumentation.AllocationReporter;
@@ -63,9 +63,18 @@
6363
public final class PythonObjectSlowPathFactory extends PythonObjectFactory {
6464

6565
private final AllocationReporter reporter;
66+
private final PythonLanguage language;
67+
private final SlowPathGetInstanceShapeNode getInstanceShapeNode;
6668

67-
public PythonObjectSlowPathFactory(AllocationReporter reporter) {
69+
public PythonObjectSlowPathFactory(AllocationReporter reporter, PythonLanguage language) {
6870
this.reporter = Objects.requireNonNull(reporter);
71+
this.language = language;
72+
this.getInstanceShapeNode = new SlowPathGetInstanceShapeNode(language);
73+
}
74+
75+
@Override
76+
public PythonLanguage getLanguage() {
77+
return language;
6978
}
7079

7180
@TruffleBoundary
@@ -78,7 +87,7 @@ protected AllocationReporter executeTrace(Object arg0Value, long arg1Value) {
7887
@TruffleBoundary
7988
@Override
8089
protected Shape executeGetShape(Object arg0Value, boolean arg1Value) {
81-
return PythonObjectFactory.getShape(arg0Value, arg1Value, (GetInstanceShapeNodeGen.getUncached()));
90+
return PythonObjectFactory.getShape(arg0Value, arg1Value, getInstanceShapeNode);
8291
}
8392

8493
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2022, 2022, 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.object;
42+
43+
import com.oracle.graal.python.PythonLanguage;
44+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
45+
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
46+
import com.oracle.graal.python.builtins.objects.type.TypeNodesFactory;
47+
import com.oracle.truffle.api.object.Shape;
48+
49+
final class SlowPathGetInstanceShapeNode extends TypeNodes.GetInstanceShape {
50+
private final PythonLanguage language;
51+
52+
SlowPathGetInstanceShapeNode(PythonLanguage language) {
53+
this.language = language;
54+
}
55+
56+
@Override
57+
public Shape execute(Object clazz) {
58+
if (clazz instanceof PythonBuiltinClassType) {
59+
// fast-path for PBCT which avoids the language lookup
60+
return ((PythonBuiltinClassType) clazz).getInstanceShape(language);
61+
}
62+
return TypeNodesFactory.GetInstanceShapeNodeGen.getUncached().execute(clazz);
63+
}
64+
}

0 commit comments

Comments
 (0)