38
38
39
39
/**
40
40
* A thread-safe map implementation that optimizes its run time representation for space efficiency.
41
- *
42
- * At image build time the map is implemented as a {@link ConcurrentHashMap} wrapped into an
43
- * {@link EconomicMap}. The ImageHeapMapFeature intercepts each build time map and transforms it
44
- * into an actual memory-efficient {@link EconomicMap} backed by a flat object array during
45
- * analysis. The later image building stages see only the {@link EconomicMap}.
46
- *
41
+ * <p>
42
+ * At image build time the map is implemented as a {@link ConcurrentHashMap} or
43
+ * {@link ConcurrentIdentityHashMap} wrapped into an {@link EconomicMapWrap}. The
44
+ * ImageHeapCollectionFeature intercepts each build time map and transforms it into an actual
45
+ * memory-efficient {@link EconomicMap} backed by a flat object array during analysis. The later
46
+ * image building stages see only the {@link EconomicMap}.
47
+ * <p>
48
+ * Additionally, in layered builds, at run time this map can be part of a
49
+ * {@link LayeredImageHeapMap} structure, i.e., conceptually a linked list of {@link EconomicMap}s
50
+ * that spans across multiple layers.
51
+ * <p>
47
52
* This map implementation allows thread-safe collection of data at image build time and storing it
48
53
* into a space efficient data structure at run time.
49
54
*/
@@ -54,27 +59,43 @@ private ImageHeapMap() {
54
59
}
55
60
56
61
/**
57
- * Create a hosted representation of an {@link ImageHeapMap}: a {@link ConcurrentHashMap}
58
- * wrapped into an {@link EconomicMap}. At run time this will be an actual memory-efficient
59
- * {@link EconomicMap} backed by a flat object array.
62
+ * In non-layered builds this creates a regular {@link ImageHeapMap} (see above).
63
+ * <p>
64
+ * In layered builds this creates a map that expands across layers. During the current layer
65
+ * build this is a {@link ConcurrentHashMap} wrapped by an {@link EconomicMapWrap}. At run time
66
+ * it is part of a {@link LayeredImageHeapMap} structure, i.e., conceptually a linked list of
67
+ * {@link EconomicMap}s that spans across multiple layers.
60
68
*/
61
69
@ Platforms (Platform .HOSTED_ONLY .class ) //
62
70
public static <K , V > EconomicMap <K , V > create (String key ) {
63
71
return create (Equivalence .DEFAULT , key );
64
72
}
65
73
66
- @ Platforms (Platform .HOSTED_ONLY .class ) //
67
- public static <K , V > EconomicMap <K , V > createNonLayeredMap () {
68
- return createNonLayeredMap (Equivalence .DEFAULT );
69
- }
70
-
74
+ /**
75
+ * Same as {@link #create(String)}, but if the {@code strategy} is {@link Equivalence#IDENTITY}
76
+ * the hosted map is a {@link ConcurrentIdentityHashMap}.
77
+ */
71
78
@ Platforms (Platform .HOSTED_ONLY .class ) //
72
79
public static <K , V > EconomicMap <K , V > create (Equivalence strategy , String key ) {
73
80
assert key != null : "The key should not be null if the map needs to be automatically layered" ;
74
81
VMError .guarantee (!BuildPhaseProvider .isAnalysisFinished (), "Trying to create an ImageHeapMap after analysis." );
75
82
return HostedImageHeapMap .create (strategy , key , true );
76
83
}
77
84
85
+ /**
86
+ * Create an {@link ImageHeapMap}. At build time it is a {@link ConcurrentHashMap} wrapped into
87
+ * an {@link EconomicMapWrap}. At run time this will be an actual memory-efficient
88
+ * {@link EconomicMap} backed by a flat object array.
89
+ */
90
+ @ Platforms (Platform .HOSTED_ONLY .class ) //
91
+ public static <K , V > EconomicMap <K , V > createNonLayeredMap () {
92
+ return createNonLayeredMap (Equivalence .DEFAULT );
93
+ }
94
+
95
+ /**
96
+ * Same as {@link #createNonLayeredMap()}, but if the {@code strategy} is
97
+ * {@link Equivalence#IDENTITY} the hosted map is a {@link ConcurrentIdentityHashMap}.
98
+ */
78
99
@ Platforms (Platform .HOSTED_ONLY .class ) //
79
100
public static <K , V > EconomicMap <K , V > createNonLayeredMap (Equivalence strategy ) {
80
101
VMError .guarantee (!BuildPhaseProvider .isAnalysisFinished (), "Trying to create an ImageHeapMap after analysis." );
@@ -115,12 +136,10 @@ public EconomicMap<Object, Object> getRuntimeMap() {
115
136
return runtimeMap ;
116
137
}
117
138
118
- public static <K , V > HostedImageHeapMap <K , V > create (Equivalence strategy , String key , boolean needLayeredMap ) {
139
+ public static <K , V > HostedImageHeapMap <K , V > create (Equivalence strategy , String key , boolean isLayeredMap ) {
119
140
Map <K , V > hostedMap = (strategy == Equivalence .IDENTITY ) ? new ConcurrentIdentityHashMap <>() : new ConcurrentHashMap <>();
120
141
EconomicMap <Object , Object > currentLayerMap = EconomicMap .create (strategy );
121
- if (!needLayeredMap || !ImageLayerBuildingSupport .buildingImageLayer ()) {
122
- return new HostedImageHeapMap <>(hostedMap , currentLayerMap , currentLayerMap );
123
- } else {
142
+ if (ImageLayerBuildingSupport .buildingImageLayer () && isLayeredMap ) {
124
143
LayeredImageHeapMap <Object , Object > runtimeMap = new LayeredImageHeapMap <>(strategy , key );
125
144
var previousMap = LayeredImageHeapMapStore .currentLayer ().getImageHeapMapStore ().put (key , currentLayerMap );
126
145
if (previousMap != null ) {
@@ -132,6 +151,8 @@ public static <K, V> HostedImageHeapMap<K, V> create(Equivalence strategy, Strin
132
151
singleton .registerPreviousLayerHostedImageHeapMap (hostedImageHeapMap );
133
152
}
134
153
return hostedImageHeapMap ;
154
+ } else {
155
+ return new HostedImageHeapMap <>(hostedMap , currentLayerMap , currentLayerMap );
135
156
}
136
157
}
137
158
}
0 commit comments