Skip to content

Commit df53965

Browse files
author
Christian Wimmer
committed
Use EconomicMap in MethodFlowsGraph
1 parent bbf2831 commit df53965

File tree

16 files changed

+302
-123
lines changed

16 files changed

+302
-123
lines changed

compiler/src/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/GraphDecoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.ArrayList;
3333
import java.util.Arrays;
3434
import java.util.BitSet;
35-
import java.util.Collection;
3635
import java.util.Deque;
3736
import java.util.Iterator;
3837
import java.util.List;
@@ -489,7 +488,7 @@ public final void decode(EncodedGraph encodedGraph) {
489488
}
490489

491490
@SuppressWarnings("try")
492-
public final void decode(EncodedGraph encodedGraph, Collection<EncodedNodeReference> nodeReferences) {
491+
public final void decode(EncodedGraph encodedGraph, Iterable<EncodedNodeReference> nodeReferences) {
493492
try (DebugContext.Scope scope = debug.scope("GraphDecoder", graph)) {
494493
MethodScope methodScope = new MethodScope(null, graph, encodedGraph, LoopExplosionKind.NONE);
495494
LoopScope loopScope = createInitialLoopScope(methodScope, null);

compiler/src/org.graalvm.compiler.nodes/src/org/graalvm/compiler/nodes/GraphEncoder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
package org.graalvm.compiler.nodes;
2626

2727
import java.util.ArrayDeque;
28-
import java.util.Collection;
2928
import java.util.Deque;
3029
import java.util.Iterator;
3130
import java.util.Objects;
@@ -175,7 +174,7 @@ public static EncodedGraph encodeSingleGraph(StructuredGraph graph, Architecture
175174
/**
176175
* Utility method that does everything necessary to encode a single graph.
177176
*/
178-
public static EncodedGraph encodeSingleGraph(StructuredGraph graph, Architecture architecture, Collection<EncodedNodeReference> nodeReferences) {
177+
public static EncodedGraph encodeSingleGraph(StructuredGraph graph, Architecture architecture, Iterable<EncodedNodeReference> nodeReferences) {
179178
GraphEncoder encoder = new GraphEncoder(architecture);
180179
encoder.prepare(graph);
181180
encoder.finishPrepare();
@@ -242,7 +241,7 @@ public int encode(StructuredGraph graph) {
242241
return encode(graph, null);
243242
}
244243

245-
protected int encode(StructuredGraph graph, Collection<EncodedNodeReference> nodeReferences) {
244+
protected int encode(StructuredGraph graph, Iterable<EncodedNodeReference> nodeReferences) {
246245
assert objectsArray != null && nodeClassesArray != null : "finishPrepare() must be called before encode()";
247246

248247
NodeOrder nodeOrder = new NodeOrder(graph);

sdk/src/org.graalvm.collections/snapshot.sigtest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ meth public static <%0 extends java.lang.Object, %1 extends java.lang.Object> or
3434
meth public static <%0 extends java.lang.Object, %1 extends java.lang.Object> org.graalvm.collections.EconomicMap<{%%0},{%%1}> create(org.graalvm.collections.Equivalence,int)
3535
meth public static <%0 extends java.lang.Object, %1 extends java.lang.Object> org.graalvm.collections.EconomicMap<{%%0},{%%1}> create(org.graalvm.collections.Equivalence,org.graalvm.collections.UnmodifiableEconomicMap<{%%0},{%%1}>)
3636
meth public static <%0 extends java.lang.Object, %1 extends java.lang.Object> org.graalvm.collections.EconomicMap<{%%0},{%%1}> create(org.graalvm.collections.UnmodifiableEconomicMap<{%%0},{%%1}>)
37+
meth public static <%0 extends java.lang.Object, %1 extends java.lang.Object> org.graalvm.collections.EconomicMap<{%%0},{%%1}> emptyMap()
3738
meth public static <%0 extends java.lang.Object, %1 extends java.lang.Object> org.graalvm.collections.EconomicMap<{%%0},{%%1}> wrapMap(java.util.Map<{%%0},{%%1}>)
3839
meth public static <%0 extends java.lang.Object, %1 extends java.lang.Object> org.graalvm.collections.MapCursor<{%%0},{%%1}> emptyCursor()
3940
meth public void putAll(org.graalvm.collections.EconomicMap<{org.graalvm.collections.EconomicMap%0},{org.graalvm.collections.EconomicMap%1}>)

sdk/src/org.graalvm.collections/src/org/graalvm/collections/EconomicMap.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -236,31 +236,18 @@ static <K, V> EconomicMap<K, V> wrapMap(Map<K, V> map) {
236236
*
237237
* @since 22.0
238238
*/
239+
@SuppressWarnings("unchecked")
239240
static <K, V> MapCursor<K, V> emptyCursor() {
240-
return new MapCursor<>() {
241-
@Override
242-
public void remove() {
243-
}
244-
245-
@Override
246-
public boolean advance() {
247-
return false;
248-
}
249-
250-
@Override
251-
public K getKey() {
252-
return null;
253-
}
254-
255-
@Override
256-
public V getValue() {
257-
return null;
258-
}
241+
return (MapCursor<K, V>) EmptyMap.EMPTY_CURSOR;
242+
}
259243

260-
@Override
261-
public V setValue(V newValue) {
262-
return null;
263-
}
264-
};
244+
/**
245+
* Return an empty, unmodifiable {@link EconomicMap}.
246+
*
247+
* @since 22.2
248+
*/
249+
@SuppressWarnings("unchecked")
250+
static <K, V> EconomicMap<K, V> emptyMap() {
251+
return (EconomicMap<K, V>) EmptyMap.EMPTY_MAP;
265252
}
266253
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (c) 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 org.graalvm.collections;
42+
43+
import java.util.Iterator;
44+
import java.util.NoSuchElementException;
45+
import java.util.function.BiFunction;
46+
47+
/**
48+
* Singleton instances for empty maps and the corresponding iterators and cursors.
49+
*/
50+
class EmptyMap {
51+
52+
static final MapCursor<Object, Object> EMPTY_CURSOR = new MapCursor<>() {
53+
@Override
54+
public void remove() {
55+
throw new NoSuchElementException("Empty cursor does not have elements");
56+
}
57+
58+
@Override
59+
public boolean advance() {
60+
return false;
61+
}
62+
63+
@Override
64+
public Object getKey() {
65+
throw new NoSuchElementException("Empty cursor does not have elements");
66+
}
67+
68+
@Override
69+
public Object getValue() {
70+
throw new NoSuchElementException("Empty cursor does not have elements");
71+
}
72+
73+
@Override
74+
public Object setValue(Object newValue) {
75+
throw new NoSuchElementException("Empty cursor does not have elements");
76+
}
77+
};
78+
79+
static final Iterator<Object> EMPTY_ITERATOR = new Iterator<>() {
80+
@Override
81+
public boolean hasNext() {
82+
return false;
83+
}
84+
85+
@Override
86+
public Object next() {
87+
throw new NoSuchElementException("Empty iterator does not have elements");
88+
}
89+
};
90+
91+
static final Iterable<Object> EMPTY_ITERABLE = new Iterable<>() {
92+
@Override
93+
public Iterator<Object> iterator() {
94+
return EMPTY_ITERATOR;
95+
}
96+
};
97+
98+
static final EconomicMap<Object, Object> EMPTY_MAP = new EconomicMap<>() {
99+
@Override
100+
public Object put(Object key, Object value) {
101+
throw new IllegalArgumentException("Cannot modify the always-empty map");
102+
}
103+
104+
@Override
105+
public void clear() {
106+
throw new IllegalArgumentException("Cannot modify the always-empty map");
107+
}
108+
109+
@Override
110+
public Object removeKey(Object key) {
111+
throw new IllegalArgumentException("Cannot modify the always-empty map");
112+
}
113+
114+
@Override
115+
public Object get(Object key) {
116+
return null;
117+
}
118+
119+
@Override
120+
public boolean containsKey(Object key) {
121+
return false;
122+
}
123+
124+
@Override
125+
public int size() {
126+
return 0;
127+
}
128+
129+
@Override
130+
public boolean isEmpty() {
131+
return true;
132+
}
133+
134+
@Override
135+
public Iterable<Object> getValues() {
136+
return EMPTY_ITERABLE;
137+
}
138+
139+
@Override
140+
public Iterable<Object> getKeys() {
141+
return EMPTY_ITERABLE;
142+
}
143+
144+
@Override
145+
public MapCursor<Object, Object> getEntries() {
146+
return EMPTY_CURSOR;
147+
}
148+
149+
@Override
150+
public void replaceAll(BiFunction<? super Object, ? super Object, ?> function) {
151+
throw new IllegalArgumentException("Cannot modify the always-empty map");
152+
}
153+
};
154+
}

0 commit comments

Comments
 (0)