author | hannesw |
Wed, 21 Mar 2018 16:55:34 +0100 | |
changeset 49275 | c639a6b33c5c |
parent 47351 | fff3970bd14f |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.runtime; |
|
27 |
||
28 |
import java.util.Arrays; |
|
29 |
import java.util.Collection; |
|
30 |
import java.util.Collections; |
|
31 |
import java.util.HashSet; |
|
32 |
import java.util.Map; |
|
33 |
import java.util.Set; |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
34 |
import jdk.nashorn.internal.runtime.options.Options; |
16147 | 35 |
|
36 |
/** |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
37 |
* Immutable hash map implementation for properties. Properties are keyed on strings |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
38 |
* or symbols (ES6). Copying and cloning is avoided by relying on immutability. |
16147 | 39 |
* <p> |
40 |
* When adding an element to a hash table, only the head of a bin list is updated, thus |
|
41 |
* an add only requires the cloning of the bins array and adding an element to the head |
|
42 |
* of the bin list. Similarly for removal, only a portion of a bin list is updated. |
|
43 |
* <p> |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
44 |
* For large tables with hundreds or thousands of elements, even just cloning the bins |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
45 |
* array when adding properties is an expensive operation. For this case, we put new |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
46 |
* elements in a separate list called {@link ElementQueue}. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
47 |
* The list component is merged into the hash table at regular intervals during element |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
48 |
* insertion to keep it from growing too long. Also, when a map with a queue component |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
49 |
* is queried repeatedly, the map will replace itself with a pure hash table version |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
50 |
* of itself to optimize lookup performance. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
51 |
* <p> |
16147 | 52 |
* A separate chronological list is kept for quick generation of keys and values, and, |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
53 |
* for rehashing. For very small maps where the overhead of the hash table would |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
54 |
* outweigh its benefits we deliberately avoid creating a hash structure and use the |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
55 |
* chronological list alone for element storage. |
16147 | 56 |
* <p> |
57 |
* Details: |
|
58 |
* <p> |
|
59 |
* The main goal is to be able to retrieve properties from a map quickly, keying on |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
60 |
* the property name (String or Symbol). A secondary, but important goal, is to keep |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
61 |
* maps immutable, so that a map can be shared by multiple objects in a context. |
16147 | 62 |
* Sharing maps allows objects to be categorized as having similar properties, a |
63 |
* fact that call site guards rely on. In this discussion, immutability allows us |
|
64 |
* to significantly reduce the amount of duplication we have in our maps. |
|
65 |
* <p> |
|
66 |
* The simplest of immutable maps is a basic singly linked list. New properties |
|
67 |
* are simply added to the head of the list. Ancestor maps are not affected by the |
|
68 |
* addition, since they continue to refer to their own head. Searching is done by |
|
69 |
* walking linearly though the elements until a match is found, O(N). |
|
70 |
* <p> |
|
71 |
* A hash map can be thought of as an optimization of a linked list map, where the |
|
72 |
* linked list is broken into fragments based on hashCode(key) . An array is use |
|
73 |
* to quickly reference these fragments, indexing on hashCode(key) mod tableSize |
|
74 |
* (tableSize is typically a power of 2 so that the mod is a fast masking |
|
75 |
* operation.) If the size of the table is sufficient large, then search time |
|
76 |
* approaches O(1). In fact, most bins in a hash table are typically empty or |
|
77 |
* contain a one element list. |
|
78 |
* <p> |
|
79 |
* For immutable hash maps, we can think of the hash map as an array of the shorter |
|
80 |
* linked list maps. If we add an element to the head of one of those lists, it |
|
81 |
* doesn't affect any ancestor maps. Thus adding an element to an immutable hash |
|
82 |
* map only requires cloning the array and inserting an element at the head of one |
|
83 |
* of the bins. |
|
84 |
* <p> |
|
85 |
* Using Java HashMaps we don't have enough control over the entries to allow us to |
|
86 |
* implement this technique, so we are forced to clone the entire hash map. |
|
87 |
* <p> |
|
88 |
* Removing elements is done similarly. We clone the array and then only modify |
|
89 |
* the bin containing the removed element. More often than not, the list contains |
|
90 |
* only one element (or is very short), so this is not very costly. When the list |
|
91 |
* has several items, we need to clone the list portion prior to the removed item. |
|
92 |
* <p> |
|
93 |
* Another requirement of property maps is that we need to be able to gather all |
|
94 |
* properties in chronological (add) order. We have been using LinkedHashMap to |
|
95 |
* provide this. For the implementation of immutable hash map, we use a singly |
|
96 |
* linked list that is linked in reverse chronological order. This means we simply |
|
97 |
* add new entries to the head of the list. If we need to work with the list in |
|
98 |
* forward order, it's simply a matter of allocating an array (size is known) and |
|
99 |
* back filling in reverse order. Removal of elements from the chronological list |
|
100 |
* is trickier. LinkedHashMap uses a doubly linked list to give constant time |
|
101 |
* removal. Immutable hash maps can't do that and maintain immutability. So we |
|
102 |
* manage the chronological list the same way we manage the bins, cloning up to the |
|
103 |
* point of removal. Don't panic. This cost is more than offset by the cost of |
|
104 |
* cloning an entire LinkedHashMap. Plus removal is far more rare than addition. |
|
105 |
* <p> |
|
106 |
* One more optimization. Maps with a small number of entries don't use the hash |
|
107 |
* map at all, the chronological list is used instead. |
|
108 |
* <p> |
|
109 |
* So the benefits from immutable arrays are; fewer objects and less copying. For |
|
110 |
* immutable hash map, when no removal is involved, the number of elements per |
|
111 |
* property is two (bin + chronological elements). For LinkedHashMap it is one |
|
112 |
* (larger element) times the number of maps that refer to the property. For |
|
113 |
* immutable hash map, addition is constant time. For LinkedHashMap it's O(N+C) |
|
114 |
* since we have to clone the older map. |
|
115 |
*/ |
|
33690 | 116 |
public final class PropertyHashMap implements Map <Object, Property> { |
16147 | 117 |
/** Number of initial bins. Power of 2. */ |
16758
4f7379c41907
8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents:
16205
diff
changeset
|
118 |
private static final int INITIAL_BINS = 32; |
16147 | 119 |
|
120 |
/** Threshold before using bins. */ |
|
16758
4f7379c41907
8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents:
16205
diff
changeset
|
121 |
private static final int LIST_THRESHOLD = 8; |
16147 | 122 |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
123 |
/** Threshold before adding new elements to queue instead of directly adding to hash bins. */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
124 |
private static final int QUEUE_THRESHOLD = Options.getIntProperty("nashorn.propmap.queue.threshold", 500); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
125 |
|
16147 | 126 |
/** Initial map. */ |
17513 | 127 |
public static final PropertyHashMap EMPTY_HASHMAP = new PropertyHashMap(); |
16147 | 128 |
|
129 |
/** Number of properties in the map. */ |
|
130 |
private final int size; |
|
131 |
||
132 |
/** Threshold before growing the bins. */ |
|
133 |
private final int threshold; |
|
134 |
||
135 |
/** Reverse list of all properties. */ |
|
136 |
private final Element list; |
|
137 |
||
138 |
/** Hash map bins. */ |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
139 |
private Element[] bins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
140 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
141 |
/** Queue for adding elements to large maps with delayed hashing. */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
142 |
private ElementQueue queue; |
16147 | 143 |
|
144 |
/** All properties as an array (lazy). */ |
|
145 |
private Property[] properties; |
|
146 |
||
147 |
/** |
|
148 |
* Empty map constructor. |
|
149 |
*/ |
|
150 |
private PropertyHashMap() { |
|
151 |
this.size = 0; |
|
152 |
this.threshold = 0; |
|
153 |
this.bins = null; |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
154 |
this.queue = null; |
16147 | 155 |
this.list = null; |
156 |
} |
|
157 |
||
158 |
/** |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
159 |
* Constructor used internally to create new maps |
16147 | 160 |
* |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
161 |
* @param map the new map |
16147 | 162 |
*/ |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
163 |
private PropertyHashMap(final MapBuilder map) { |
16147 | 164 |
this.size = map.size; |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
165 |
if (map.qhead == null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
166 |
this.bins = map.bins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
167 |
this.queue = null; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
168 |
} else { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
169 |
this.bins = null; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
170 |
this.queue = new ElementQueue(map.qhead, map.bins); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
171 |
} |
16147 | 172 |
this.list = map.list; |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
173 |
this.threshold = map.bins != null ? threeQuarters(map.bins.length) : 0; |
16147 | 174 |
} |
175 |
||
176 |
/** |
|
24719 | 177 |
* Clone a property map, replacing a property with a new one in the same place, |
178 |
* which is important for property iterations if a property changes types |
|
179 |
* @param property old property |
|
180 |
* @param newProperty new property |
|
181 |
* @return new property map |
|
182 |
*/ |
|
183 |
public PropertyHashMap immutableReplace(final Property property, final Property newProperty) { |
|
184 |
assert property.getKey().equals(newProperty.getKey()) : "replacing properties with different keys: '" + property.getKey() + "' != '" + newProperty.getKey() + "'"; |
|
185 |
assert findElement(property.getKey()) != null : "replacing property that doesn't exist in map: '" + property.getKey() + "'"; |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
186 |
final MapBuilder builder = newMapBuilder(size); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
187 |
builder.replaceProperty(property.getKey(), newProperty); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
188 |
return new PropertyHashMap(builder); |
24719 | 189 |
} |
190 |
||
191 |
/** |
|
16147 | 192 |
* Clone a {@link PropertyHashMap} and add a {@link Property}. |
193 |
* |
|
194 |
* @param property {@link Property} to add. |
|
195 |
* |
|
196 |
* @return New {@link PropertyHashMap}. |
|
197 |
*/ |
|
198 |
public PropertyHashMap immutableAdd(final Property property) { |
|
199 |
final int newSize = size + 1; |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
200 |
MapBuilder builder = newMapBuilder(newSize); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
201 |
builder.addProperty(property); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
202 |
return new PropertyHashMap(builder); |
16147 | 203 |
} |
204 |
||
205 |
/** |
|
206 |
* Clone a {@link PropertyHashMap} and add an array of properties. |
|
207 |
* |
|
208 |
* @param newProperties Properties to add. |
|
209 |
* |
|
210 |
* @return New {@link PropertyHashMap}. |
|
211 |
*/ |
|
212 |
public PropertyHashMap immutableAdd(final Property... newProperties) { |
|
213 |
final int newSize = size + newProperties.length; |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
214 |
MapBuilder builder = newMapBuilder(newSize); |
16147 | 215 |
for (final Property property : newProperties) { |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
216 |
builder.addProperty(property); |
16147 | 217 |
} |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
218 |
return new PropertyHashMap(builder); |
16147 | 219 |
} |
220 |
||
221 |
/** |
|
222 |
* Clone a {@link PropertyHashMap} and add a collection of properties. |
|
223 |
* |
|
224 |
* @param newProperties Properties to add. |
|
225 |
* |
|
226 |
* @return New {@link PropertyHashMap}. |
|
227 |
*/ |
|
228 |
public PropertyHashMap immutableAdd(final Collection<Property> newProperties) { |
|
229 |
if (newProperties != null) { |
|
230 |
final int newSize = size + newProperties.size(); |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
231 |
MapBuilder builder = newMapBuilder(newSize); |
16147 | 232 |
for (final Property property : newProperties) { |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
233 |
builder.addProperty(property); |
16147 | 234 |
} |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
235 |
return new PropertyHashMap(builder); |
16147 | 236 |
} |
237 |
return this; |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Clone a {@link PropertyHashMap} and remove a {@link Property} based on its key. |
|
242 |
* |
|
243 |
* @param key Key of {@link Property} to remove. |
|
244 |
* |
|
245 |
* @return New {@link PropertyHashMap}. |
|
246 |
*/ |
|
33690 | 247 |
public PropertyHashMap immutableRemove(final Object key) { |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
248 |
MapBuilder builder = newMapBuilder(size); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
249 |
builder.removeProperty(key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
250 |
if (builder.size < size) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
251 |
return builder.size != 0 ? new PropertyHashMap(builder) : EMPTY_HASHMAP; |
16147 | 252 |
} |
253 |
return this; |
|
254 |
} |
|
255 |
||
256 |
/** |
|
257 |
* Find a {@link Property} in the {@link PropertyHashMap}. |
|
258 |
* |
|
259 |
* @param key Key of {@link Property} to find. |
|
260 |
* |
|
261 |
* @return {@link Property} matching key or {@code null} if not found. |
|
262 |
*/ |
|
33690 | 263 |
public Property find(final Object key) { |
16147 | 264 |
final Element element = findElement(key); |
265 |
return element != null ? element.getProperty() : null; |
|
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Return an array of properties in chronological order of adding. |
|
270 |
* |
|
271 |
* @return Array of all properties. |
|
272 |
*/ |
|
16205
93fda2507e35
8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents:
16179
diff
changeset
|
273 |
Property[] getProperties() { |
16147 | 274 |
if (properties == null) { |
275 |
final Property[] array = new Property[size]; |
|
276 |
int i = size; |
|
277 |
for (Element element = list; element != null; element = element.getLink()) { |
|
278 |
array[--i] = element.getProperty(); |
|
279 |
} |
|
280 |
properties = array; |
|
281 |
} |
|
282 |
return properties; |
|
283 |
} |
|
284 |
||
285 |
/** |
|
286 |
* Returns the bin index from the key. |
|
287 |
* |
|
288 |
* @param bins The bins array. |
|
289 |
* @param key {@link Property} key. |
|
290 |
* |
|
291 |
* @return The bin index. |
|
292 |
*/ |
|
33690 | 293 |
private static int binIndex(final Element[] bins, final Object key) { |
24720 | 294 |
return key.hashCode() & bins.length - 1; |
16147 | 295 |
} |
296 |
||
297 |
/** |
|
298 |
* Calculate the number of bins needed to contain n properties. |
|
299 |
* |
|
300 |
* @param n Number of elements. |
|
301 |
* |
|
302 |
* @return Number of bins required. |
|
303 |
*/ |
|
304 |
private static int binsNeeded(final int n) { |
|
16758
4f7379c41907
8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents:
16205
diff
changeset
|
305 |
// 50% padding |
24720 | 306 |
return 1 << 32 - Integer.numberOfLeadingZeros(n + (n >>> 1) | INITIAL_BINS - 1); |
16147 | 307 |
} |
308 |
||
309 |
/** |
|
310 |
* Used to calculate the current capacity of the bins. |
|
311 |
* |
|
312 |
* @param n Number of bin slots. |
|
313 |
* |
|
314 |
* @return 75% of n. |
|
315 |
*/ |
|
316 |
private static int threeQuarters(final int n) { |
|
317 |
return (n >>> 1) + (n >>> 2); |
|
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* Regenerate the bin table after changing the number of bins. |
|
322 |
* |
|
323 |
* @param list // List of all properties. |
|
16758
4f7379c41907
8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents:
16205
diff
changeset
|
324 |
* @param binSize // New size of bins. |
16147 | 325 |
* |
326 |
* @return Populated bins. |
|
327 |
*/ |
|
16758
4f7379c41907
8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents:
16205
diff
changeset
|
328 |
private static Element[] rehash(final Element list, final int binSize) { |
4f7379c41907
8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents:
16205
diff
changeset
|
329 |
final Element[] newBins = new Element[binSize]; |
16147 | 330 |
for (Element element = list; element != null; element = element.getLink()) { |
331 |
final Property property = element.getProperty(); |
|
33690 | 332 |
final Object key = property.getKey(); |
24719 | 333 |
final int binIndex = binIndex(newBins, key); |
334 |
||
16147 | 335 |
newBins[binIndex] = new Element(newBins[binIndex], property); |
336 |
} |
|
337 |
return newBins; |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Locate an element based on key. |
|
342 |
* |
|
343 |
* @param key {@link Element} key. |
|
344 |
* |
|
345 |
* @return {@link Element} matching key or {@code null} if not found. |
|
346 |
*/ |
|
33690 | 347 |
private Element findElement(final Object key) { |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
348 |
if (queue != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
349 |
return queue.find(key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
350 |
} else if (bins != null) { |
16147 | 351 |
final int binIndex = binIndex(bins, key); |
352 |
return findElement(bins[binIndex], key); |
|
353 |
} |
|
354 |
return findElement(list, key); |
|
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* Locate an {@link Element} based on key from a specific list. |
|
359 |
* |
|
360 |
* @param elementList Head of {@link Element} list |
|
361 |
* @param key {@link Element} key. |
|
362 |
* @return {@link Element} matching key or {@code null} if not found. |
|
363 |
*/ |
|
33690 | 364 |
private static Element findElement(final Element elementList, final Object key) { |
16147 | 365 |
final int hashCode = key.hashCode(); |
366 |
for (Element element = elementList; element != null; element = element.getLink()) { |
|
367 |
if (element.match(key, hashCode)) { |
|
368 |
return element; |
|
369 |
} |
|
370 |
} |
|
371 |
return null; |
|
372 |
} |
|
373 |
||
374 |
/** |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
375 |
* Create a {@code MapBuilder} to add new elements to. |
16147 | 376 |
* |
377 |
* @param newSize New size of {@link PropertyHashMap}. |
|
378 |
* |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
379 |
* @return {@link MapBuilder} for the new size. |
16147 | 380 |
*/ |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
381 |
private MapBuilder newMapBuilder(final int newSize) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
382 |
if (bins == null && newSize < LIST_THRESHOLD) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
383 |
return new MapBuilder(bins, list, size, false); |
16147 | 384 |
} else if (newSize > threshold) { |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
385 |
return new MapBuilder(rehash(list, binsNeeded(newSize)), list, size, true); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
386 |
} else if (shouldCloneBins(size, newSize)) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
387 |
return new MapBuilder(cloneBins(), list, size, true); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
388 |
} else if (queue == null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
389 |
return new MapBuilder(bins, list, size, false); |
16147 | 390 |
} else { |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
391 |
return new MapBuilder(queue, list, size, false); |
16147 | 392 |
} |
393 |
} |
|
394 |
||
395 |
/** |
|
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
396 |
* Create a cloned or new bins array and merge the elements in the queue into it if there are any. |
16147 | 397 |
* |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
398 |
* @return the cloned bins array |
16147 | 399 |
*/ |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
400 |
private Element[] cloneBins() { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
401 |
if (queue != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
402 |
return queue.cloneAndMergeBins(); |
16147 | 403 |
} |
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
404 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
405 |
return bins.clone(); |
16147 | 406 |
} |
407 |
||
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
408 |
/** |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
409 |
* Used on insertion to determine whether the bins array should be cloned, or we should keep |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
410 |
* using the ancestor's bins array and put new elements into the queue. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
411 |
* |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
412 |
* @param oldSize the old map size |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
413 |
* @param newSize the new map size |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
414 |
* @return whether to clone the bins array |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
415 |
*/ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
416 |
private boolean shouldCloneBins(final int oldSize, final int newSize) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
417 |
// For maps with less than QUEUE_THRESHOLD elements we clone the bins array on every insertion. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
418 |
// Above that threshold we put new elements into the queue and only merge every 512 elements. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
419 |
return newSize < QUEUE_THRESHOLD || (newSize >>> 9) > (oldSize >>> 9); |
24719 | 420 |
} |
421 |
||
16147 | 422 |
/** |
423 |
* Removes an {@link Element} from a specific list, avoiding duplication. |
|
424 |
* |
|
425 |
* @param list List to remove from. |
|
426 |
* @param key Key of {@link Element} to remove. |
|
427 |
* |
|
428 |
* @return New list with {@link Element} removed. |
|
429 |
*/ |
|
33690 | 430 |
private static Element removeFromList(final Element list, final Object key) { |
16147 | 431 |
if (list == null) { |
432 |
return null; |
|
433 |
} |
|
434 |
final int hashCode = key.hashCode(); |
|
435 |
if (list.match(key, hashCode)) { |
|
436 |
return list.getLink(); |
|
437 |
} |
|
438 |
final Element head = new Element(null, list.getProperty()); |
|
439 |
Element previous = head; |
|
440 |
for (Element element = list.getLink(); element != null; element = element.getLink()) { |
|
441 |
if (element.match(key, hashCode)) { |
|
442 |
previous.setLink(element.getLink()); |
|
443 |
return head; |
|
444 |
} |
|
445 |
final Element next = new Element(null, element.getProperty()); |
|
446 |
previous.setLink(next); |
|
447 |
previous = next; |
|
448 |
} |
|
449 |
return list; |
|
450 |
} |
|
451 |
||
24719 | 452 |
// for element x. if x get link matches, |
33690 | 453 |
private static Element replaceInList(final Element list, final Object key, final Property property) { |
24719 | 454 |
assert list != null; |
455 |
final int hashCode = key.hashCode(); |
|
456 |
||
457 |
if (list.match(key, hashCode)) { |
|
458 |
return new Element(list.getLink(), property); |
|
459 |
} |
|
460 |
||
24734
da070553a8e1
8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents:
24720
diff
changeset
|
461 |
final Element head = new Element(null, list.getProperty()); |
da070553a8e1
8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents:
24720
diff
changeset
|
462 |
Element previous = head; |
24719 | 463 |
for (Element element = list.getLink(); element != null; element = element.getLink()) { |
464 |
if (element.match(key, hashCode)) { |
|
465 |
previous.setLink(new Element(element.getLink(), property)); |
|
24734
da070553a8e1
8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents:
24720
diff
changeset
|
466 |
return head; |
24719 | 467 |
} |
24734
da070553a8e1
8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents:
24720
diff
changeset
|
468 |
final Element next = new Element(null, element.getProperty()); |
da070553a8e1
8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents:
24720
diff
changeset
|
469 |
previous.setLink(next); |
da070553a8e1
8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents:
24720
diff
changeset
|
470 |
previous = next; |
24719 | 471 |
} |
472 |
return list; |
|
473 |
} |
|
474 |
||
475 |
||
16147 | 476 |
/* |
477 |
* Map implementation |
|
478 |
*/ |
|
479 |
||
480 |
@Override |
|
481 |
public int size() { |
|
482 |
return size; |
|
483 |
} |
|
484 |
||
485 |
@Override |
|
486 |
public boolean isEmpty() { |
|
487 |
return size == 0; |
|
488 |
} |
|
489 |
||
490 |
@Override |
|
491 |
public boolean containsKey(final Object key) { |
|
33690 | 492 |
assert key instanceof String || key instanceof Symbol; |
16147 | 493 |
return findElement(key) != null; |
494 |
} |
|
495 |
||
496 |
@Override |
|
497 |
public boolean containsValue(final Object value) { |
|
498 |
if (value instanceof Property) { |
|
499 |
final Property property = (Property) value; |
|
500 |
final Element element = findElement(property.getKey()); |
|
501 |
return element != null && element.getProperty().equals(value); |
|
502 |
} |
|
503 |
return false; |
|
504 |
} |
|
505 |
||
506 |
@Override |
|
507 |
public Property get(final Object key) { |
|
33690 | 508 |
assert key instanceof String || key instanceof Symbol; |
16147 | 509 |
final Element element = findElement(key); |
510 |
return element != null ? element.getProperty() : null; |
|
511 |
} |
|
512 |
||
513 |
@Override |
|
33690 | 514 |
public Property put(final Object key, final Property value) { |
16147 | 515 |
throw new UnsupportedOperationException("Immutable map."); |
516 |
} |
|
517 |
||
518 |
@Override |
|
519 |
public Property remove(final Object key) { |
|
520 |
throw new UnsupportedOperationException("Immutable map."); |
|
521 |
} |
|
522 |
||
523 |
@Override |
|
33690 | 524 |
public void putAll(final Map<? extends Object, ? extends Property> m) { |
16147 | 525 |
throw new UnsupportedOperationException("Immutable map."); |
526 |
} |
|
527 |
||
528 |
@Override |
|
529 |
public void clear() { |
|
530 |
throw new UnsupportedOperationException("Immutable map."); |
|
531 |
} |
|
532 |
||
533 |
@Override |
|
33690 | 534 |
public Set<Object> keySet() { |
535 |
final HashSet<Object> set = new HashSet<>(); |
|
16147 | 536 |
for (Element element = list; element != null; element = element.getLink()) { |
537 |
set.add(element.getKey()); |
|
538 |
} |
|
539 |
return Collections.unmodifiableSet(set); |
|
540 |
} |
|
541 |
||
542 |
@Override |
|
543 |
public Collection<Property> values() { |
|
544 |
return Collections.unmodifiableList(Arrays.asList(getProperties())); |
|
545 |
} |
|
546 |
||
547 |
@Override |
|
33690 | 548 |
public Set<Entry<Object, Property>> entrySet() { |
549 |
final HashSet<Entry<Object, Property>> set = new HashSet<>(); |
|
16147 | 550 |
for (Element element = list; element != null; element = element.getLink()) { |
551 |
set.add(element); |
|
552 |
} |
|
553 |
return Collections.unmodifiableSet(set); |
|
554 |
} |
|
555 |
||
556 |
/** |
|
557 |
* List map element. |
|
558 |
*/ |
|
33690 | 559 |
static final class Element implements Entry<Object, Property> { |
16147 | 560 |
/** Link for list construction. */ |
561 |
private Element link; |
|
562 |
||
563 |
/** Element property. */ |
|
564 |
private final Property property; |
|
565 |
||
566 |
/** Element key. Kept separate for performance.) */ |
|
33690 | 567 |
private final Object key; |
16147 | 568 |
|
569 |
/** Element key hash code. */ |
|
570 |
private final int hashCode; |
|
571 |
||
572 |
/* |
|
573 |
* Constructors |
|
574 |
*/ |
|
575 |
||
576 |
Element(final Element link, final Property property) { |
|
577 |
this.link = link; |
|
578 |
this.property = property; |
|
579 |
this.key = property.getKey(); |
|
580 |
this.hashCode = this.key.hashCode(); |
|
581 |
} |
|
582 |
||
33690 | 583 |
boolean match(final Object otherKey, final int otherHashCode) { |
16147 | 584 |
return this.hashCode == otherHashCode && this.key.equals(otherKey); |
585 |
} |
|
586 |
||
587 |
/* |
|
588 |
* Entry implmentation. |
|
589 |
*/ |
|
590 |
||
591 |
@Override |
|
592 |
public boolean equals(final Object other) { |
|
593 |
assert property != null && other != null; |
|
16179
5b8bcfd712d3
8006517: PropertyHashMap.Element.equals() compares to Property
jlaskey
parents:
16151
diff
changeset
|
594 |
return other instanceof Element && property.equals(((Element)other).property); |
16147 | 595 |
} |
596 |
||
597 |
@Override |
|
33690 | 598 |
public Object getKey() { |
16147 | 599 |
return key; |
600 |
} |
|
601 |
||
602 |
@Override |
|
603 |
public Property getValue() { |
|
604 |
return property; |
|
605 |
} |
|
606 |
||
607 |
@Override |
|
608 |
public int hashCode() { |
|
609 |
return hashCode; |
|
610 |
} |
|
611 |
||
612 |
@Override |
|
613 |
public Property setValue(final Property value) { |
|
614 |
throw new UnsupportedOperationException("Immutable map."); |
|
615 |
} |
|
616 |
||
24719 | 617 |
@Override |
618 |
public String toString() { |
|
619 |
final StringBuffer sb = new StringBuffer(); |
|
620 |
||
621 |
sb.append('['); |
|
622 |
||
623 |
Element elem = this; |
|
624 |
do { |
|
24734
da070553a8e1
8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents:
24720
diff
changeset
|
625 |
sb.append(elem.getValue()); |
24719 | 626 |
elem = elem.link; |
627 |
if (elem != null) { |
|
628 |
sb.append(" -> "); |
|
629 |
} |
|
630 |
} while (elem != null); |
|
631 |
||
632 |
sb.append(']'); |
|
633 |
||
634 |
return sb.toString(); |
|
635 |
} |
|
636 |
||
16147 | 637 |
/* |
638 |
* Accessors |
|
639 |
*/ |
|
640 |
||
641 |
Element getLink() { |
|
642 |
return link; |
|
643 |
} |
|
644 |
||
645 |
void setLink(final Element link) { |
|
646 |
this.link = link; |
|
647 |
} |
|
648 |
||
649 |
Property getProperty() { |
|
650 |
return property; |
|
651 |
} |
|
652 |
} |
|
653 |
||
47351
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
654 |
/** |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
655 |
* A hybrid map/list structure to add elements to the map without the need to clone and rehash the main table. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
656 |
* This is used for large maps to reduce the overhead of adding elements. Instances of this class can replace |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
657 |
* themselves with a pure hash map version of themselves to optimize query performance. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
658 |
*/ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
659 |
private class ElementQueue { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
660 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
661 |
/** List of elements not merged into bins */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
662 |
private final Element qhead; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
663 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
664 |
/** Our own bins array. Differs from original PropertyHashMap bins when queue is merged. */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
665 |
private final Element[] qbins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
666 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
667 |
/** Count searches to trigger merging of queue into bins. */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
668 |
int searchCount = 0; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
669 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
670 |
ElementQueue(final Element qhead, final Element[] qbins) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
671 |
this.qhead = qhead; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
672 |
this.qbins = qbins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
673 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
674 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
675 |
Element find(final Object key) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
676 |
final int binIndex = binIndex(qbins, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
677 |
final Element element = findElement(qbins[binIndex], key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
678 |
if (element != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
679 |
return element; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
680 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
681 |
if (qhead != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
682 |
if (++searchCount > 2) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
683 |
// Merge the queue into the hash bins if this map is queried more than a few times |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
684 |
final Element[] newBins = cloneAndMergeBins(); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
685 |
assert newBins != qbins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
686 |
PropertyHashMap.this.queue = new ElementQueue(null, newBins); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
687 |
return PropertyHashMap.this.queue.find(key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
688 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
689 |
return findElement(qhead, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
690 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
691 |
return null; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
692 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
693 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
694 |
/** |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
695 |
* Create a cloned or new bins array and merge the elements in the queue into it if there are any. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
696 |
* |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
697 |
* @return the cloned bins array |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
698 |
*/ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
699 |
private Element[] cloneAndMergeBins() { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
700 |
if (qhead == null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
701 |
return qbins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
702 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
703 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
704 |
final Element[] newBins = qbins.clone(); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
705 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
706 |
for (Element element = qhead; element != null; element = element.getLink()) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
707 |
final Property property = element.getProperty(); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
708 |
final Object key = property.getKey(); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
709 |
final int binIndex = binIndex(newBins, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
710 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
711 |
newBins[binIndex] = new Element(newBins[binIndex], property); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
712 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
713 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
714 |
return newBins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
715 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
716 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
717 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
718 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
719 |
/** |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
720 |
* A builder class used for adding, replacing, or removing elements. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
721 |
*/ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
722 |
private static class MapBuilder { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
723 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
724 |
/** Bins array - may be shared with map that created us. */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
725 |
private Element[] bins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
726 |
/** Whether our bins are shared */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
727 |
private boolean hasOwnBins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
728 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
729 |
/** Queue of unmerged elements */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
730 |
private Element qhead; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
731 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
732 |
/** Full property list. */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
733 |
private Element list; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
734 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
735 |
/** Number of properties. */ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
736 |
private int size; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
737 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
738 |
MapBuilder(final Element[] bins, final Element list, final int size, final boolean hasOwnBins) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
739 |
this.bins = bins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
740 |
this.hasOwnBins = hasOwnBins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
741 |
this.list = list; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
742 |
this.qhead = null; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
743 |
this.size = size; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
744 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
745 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
746 |
MapBuilder(final ElementQueue queue, final Element list, final int size, final boolean hasOwnBins) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
747 |
this.bins = queue.qbins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
748 |
this.hasOwnBins = hasOwnBins; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
749 |
this.list = list; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
750 |
this.qhead = queue.qhead; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
751 |
this.size = size; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
752 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
753 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
754 |
/** |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
755 |
* Add a {@link Property}. Removes duplicates if necessary. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
756 |
* |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
757 |
* @param property {@link Property} to add. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
758 |
*/ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
759 |
private void addProperty(final Property property) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
760 |
final Object key = property.getKey(); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
761 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
762 |
if (bins != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
763 |
final int binIndex = binIndex(bins, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
764 |
if (findElement(bins[binIndex], key) != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
765 |
ensureOwnBins(); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
766 |
bins[binIndex] = removeExistingElement(bins[binIndex], key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
767 |
} else if (findElement(qhead, key) != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
768 |
qhead = removeExistingElement(qhead, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
769 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
770 |
if (hasOwnBins) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
771 |
bins[binIndex] = new Element(bins[binIndex], property); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
772 |
} else { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
773 |
qhead = new Element(qhead, property); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
774 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
775 |
} else { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
776 |
if (findElement(list, key) != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
777 |
list = removeFromList(list, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
778 |
size--; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
779 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
780 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
781 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
782 |
list = new Element(list, property); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
783 |
size++; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
784 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
785 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
786 |
/** |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
787 |
* Replace an existing {@link Property} with a new one with the same key. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
788 |
* |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
789 |
* @param key the property key |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
790 |
* @param property the property to replace the old one with |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
791 |
*/ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
792 |
private void replaceProperty(final Object key, final Property property) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
793 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
794 |
if (bins != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
795 |
final int binIndex = binIndex(bins, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
796 |
Element bin = bins[binIndex]; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
797 |
if (findElement(bin, key) != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
798 |
ensureOwnBins(); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
799 |
bins[binIndex] = replaceInList(bin, key, property); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
800 |
} else if (qhead != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
801 |
qhead = replaceInList(qhead, key, property); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
802 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
803 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
804 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
805 |
list = replaceInList(list, key, property); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
806 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
807 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
808 |
/** |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
809 |
* Remove a {@link Property} based on its key. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
810 |
* |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
811 |
* @param key Key of {@link Property} to remove. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
812 |
*/ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
813 |
void removeProperty(final Object key) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
814 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
815 |
if (bins != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
816 |
final int binIndex = binIndex(bins, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
817 |
final Element bin = bins[binIndex]; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
818 |
if (findElement(bin, key) != null) { ; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
819 |
if (size >= LIST_THRESHOLD) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
820 |
ensureOwnBins(); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
821 |
bins[binIndex] = removeFromList(bin, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
822 |
} else { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
823 |
// Go back to list-only representation for small maps |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
824 |
bins = null; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
825 |
qhead = null; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
826 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
827 |
} else if (findElement(qhead, key) != null) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
828 |
qhead = removeFromList(qhead, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
829 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
830 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
831 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
832 |
list = removeFromList(list, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
833 |
size--; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
834 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
835 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
836 |
/** |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
837 |
* Removes an element known to exist from an element list and the main {@code list} and decreases {@code size}. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
838 |
* |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
839 |
* @param element the element list |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
840 |
* @param key the key to remove |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
841 |
* @return the new element list |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
842 |
*/ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
843 |
private Element removeExistingElement(Element element, Object key) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
844 |
size--; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
845 |
list = removeFromList(list, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
846 |
return removeFromList(element, key); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
847 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
848 |
|
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
849 |
/** |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
850 |
* Make sure we own the bins we have, cloning them if necessary. |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
851 |
*/ |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
852 |
private void ensureOwnBins() { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
853 |
if (!hasOwnBins) { |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
854 |
bins = bins.clone(); |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
855 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
856 |
hasOwnBins = true; |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
857 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
858 |
} |
fff3970bd14f
8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents:
47216
diff
changeset
|
859 |
|
16147 | 860 |
} |