Skip to content

Commit 6af95f0

Browse files
committed
PythonContext: add support for caching per context global ids for builtin empty immutable types (i.e., tuple(), frozenset(), etc)
1 parent ac5bae4 commit 6af95f0

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import static com.oracle.graal.python.nodes.BuiltinNames.SETATTR;
5454
import static com.oracle.graal.python.nodes.BuiltinNames.SUM;
5555
import static com.oracle.graal.python.nodes.BuiltinNames.__BREAKPOINT__;
56+
import static com.oracle.graal.python.nodes.HiddenAttributes.ID_KEY;
5657
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
5758
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INSTANCECHECK__;
5859
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
@@ -144,7 +145,6 @@
144145
import com.oracle.truffle.api.nodes.ExplodeLoop;
145146
import com.oracle.truffle.api.nodes.RootNode;
146147
import com.oracle.truffle.api.nodes.UnexpectedResultException;
147-
import com.oracle.truffle.api.object.HiddenKey;
148148
import com.oracle.truffle.api.profiles.ConditionProfile;
149149
import com.oracle.truffle.api.source.Source;
150150

@@ -637,7 +637,6 @@ public abstract static class IdNode extends PythonBuiltinNode {
637637
* The next available global id. We reserve space for all integers to be their own id +
638638
* offset.
639639
*/
640-
private static HiddenKey idKey = new HiddenKey("object_id");
641640

642641
@Child private ReadAttributeFromObjectNode readId = null;
643642
@Child private WriteAttributeToObjectNode writeId = null;
@@ -705,10 +704,10 @@ private Object getId(PythonObject obj) {
705704
readId = insert(ReadAttributeFromObjectNode.create());
706705
writeId = insert(WriteAttributeToObjectNode.create());
707706
}
708-
Object id = readId.execute(obj, idKey);
707+
Object id = readId.execute(obj, ID_KEY);
709708
if (id == NO_VALUE) {
710709
id = getContext().getNextGlobalId();
711-
writeId.execute(obj, idKey, id);
710+
writeId.execute(obj, ID_KEY, id);
712711
}
713712
return id;
714713
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates.
3+
*
4+
* The Universal Permissive License (UPL), Version 1.0
5+
*
6+
* Subject to the condition set forth below, permission is hereby granted to any
7+
* person obtaining a copy of this software, associated documentation and/or data
8+
* (collectively the "Software"), free of charge and under any and all copyright
9+
* rights in the Software, and any and all patent rights owned or freely
10+
* licensable by each licensor hereunder covering either (i) the unmodified
11+
* Software as contributed to or provided by such licensor, or (ii) the Larger
12+
* Works (as defined below), to deal in both
13+
*
14+
* (a) the Software, and
15+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
* one is included with the Software (each a "Larger Work" to which the
17+
* Software is contributed by such licensors),
18+
*
19+
* without restriction, including without limitation the rights to copy, create
20+
* derivative works of, display, perform, and distribute the Software and make,
21+
* use, sell, offer for sale, import, export, have made, and have sold the
22+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
* either these or other terms.
24+
*
25+
* This license is subject to the following condition:
26+
*
27+
* The above copyright notice and either this complete permission notice or at a
28+
* minimum a reference to the UPL must be included in all copies or substantial
29+
* portions of the Software.
30+
*
31+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
* SOFTWARE.
38+
*/
39+
package com.oracle.graal.python.nodes;
40+
41+
import com.oracle.truffle.api.object.HiddenKey;
42+
43+
public abstract class HiddenAttributes {
44+
public static final HiddenKey ID_KEY = new HiddenKey("object_id");
45+
}

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ public class PythonContext {
5656
private PythonModule mainModule;
5757
private final PythonCore core;
5858
private final HashMap<Object, CallTarget> atExitHooks = new HashMap<>();
59-
private final AtomicLong globalId = new AtomicLong(Integer.MAX_VALUE * 2 + 4L);
59+
private final AtomicLong globalId = new AtomicLong(Integer.MAX_VALUE * 2L + 4L);
60+
61+
enum PythonBuiltinImmutableType {
62+
PFrozenSet,
63+
PTuple,
64+
PBytes,
65+
PString
66+
}
67+
68+
private final long[] emptyImmutableObjectsIdCache = new long[PythonBuiltinImmutableType.values().length];
6069

6170
@CompilationFinal private TruffleLanguage.Env env;
6271

@@ -91,6 +100,18 @@ public PythonContext(PythonLanguage language, TruffleLanguage.Env env, PythonCor
91100
}
92101
}
93102

103+
public long getEmptyImmutableObjectGlobalId(PythonBuiltinImmutableType immutableType) {
104+
int idx = immutableType.ordinal();
105+
if (emptyImmutableObjectsIdCache[idx] == 0) {
106+
synchronized (emptyImmutableObjectsIdCache) {
107+
if (emptyImmutableObjectsIdCache[idx] == 0) {
108+
emptyImmutableObjectsIdCache[idx] = getNextGlobalId();
109+
}
110+
}
111+
}
112+
return emptyImmutableObjectsIdCache[idx];
113+
}
114+
94115
public long getNextGlobalId() {
95116
return globalId.incrementAndGet();
96117
}

0 commit comments

Comments
 (0)