author | lagergren |
Fri, 26 Sep 2014 18:47:20 +0200 | |
changeset 26886 | 18c744ab4df2 |
parent 26768 | 751b0f427090 |
child 27098 | 2875b30458d3 |
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.arrays; |
|
27 |
||
24720 | 28 |
import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall; |
24719 | 29 |
import java.lang.invoke.MethodHandle; |
30 |
import java.lang.invoke.MethodHandles; |
|
16147 | 31 |
import java.util.Arrays; |
24719 | 32 |
import jdk.nashorn.internal.codegen.types.Type; |
16147 | 33 |
import jdk.nashorn.internal.runtime.JSType; |
34 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
35 |
||
36 |
/** |
|
37 |
* Implementation of {@link ArrayData} as soon as an int has been |
|
38 |
* written to the array. This is the default data for new arrays |
|
39 |
*/ |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
40 |
final class IntArrayData extends ContinuousArrayData implements IntElements { |
16147 | 41 |
/** |
42 |
* The wrapped array |
|
43 |
*/ |
|
44 |
private int[] array; |
|
45 |
||
46 |
IntArrayData() { |
|
47 |
this(new int[ArrayData.CHUNK_SIZE], 0); |
|
48 |
} |
|
49 |
||
50 |
IntArrayData(final int length) { |
|
51 |
super(length); |
|
52 |
this.array = new int[ArrayData.nextSize(length)]; |
|
53 |
} |
|
54 |
||
55 |
/** |
|
56 |
* Constructor |
|
57 |
* @param array an int array |
|
58 |
* @param length a length, not necessarily array.length |
|
59 |
*/ |
|
60 |
IntArrayData(final int array[], final int length) { |
|
61 |
super(length); |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18879
diff
changeset
|
62 |
assert array.length >= length; |
16147 | 63 |
this.array = array; |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18879
diff
changeset
|
64 |
} |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18879
diff
changeset
|
65 |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
66 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
67 |
public Class<?> getElementType() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
68 |
return int.class; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
69 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
70 |
|
24741
4232289c3235
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents:
24720
diff
changeset
|
71 |
private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), IntArrayData.class, "getElem", int.class, int.class).methodHandle(); |
4232289c3235
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents:
24720
diff
changeset
|
72 |
private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), IntArrayData.class, "setElem", void.class, int.class, int.class).methodHandle(); |
24719 | 73 |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
74 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
75 |
public Object[] asObjectArray() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
76 |
return toObjectArray(); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
77 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
78 |
|
24719 | 79 |
@SuppressWarnings("unused") |
80 |
private int getElem(final int index) { |
|
81 |
if (has(index)) { |
|
82 |
return array[index]; |
|
83 |
} |
|
84 |
throw new ClassCastException(); |
|
85 |
} |
|
86 |
||
87 |
@SuppressWarnings("unused") |
|
88 |
private void setElem(final int index, final int elem) { |
|
24720 | 89 |
if (hasRoomFor(index)) { |
90 |
array[index] = elem; |
|
91 |
return; |
|
92 |
} |
|
93 |
throw new ClassCastException(); |
|
24719 | 94 |
} |
95 |
||
96 |
@Override |
|
97 |
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) { |
|
98 |
return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint); |
|
99 |
} |
|
100 |
||
101 |
@Override |
|
102 |
public MethodHandle getElementSetter(final Class<?> elementType) { |
|
103 |
return elementType == int.class ? getContinuousElementSetter(SET_ELEM, elementType) : null; |
|
104 |
} |
|
105 |
||
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18879
diff
changeset
|
106 |
@Override |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18879
diff
changeset
|
107 |
public ArrayData copy() { |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
108 |
return new IntArrayData(array.clone(), (int)length); |
16147 | 109 |
} |
110 |
||
111 |
@Override |
|
112 |
public Object asArrayOfType(final Class<?> componentType) { |
|
24719 | 113 |
if (componentType == int.class) { |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
114 |
return array.length == length ? array.clone() : Arrays.copyOf(array, (int)length); |
16147 | 115 |
} |
116 |
return super.asArrayOfType(componentType); |
|
117 |
} |
|
118 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
119 |
private Object[] toObjectArray() { |
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
120 |
assert length <= array.length : "length exceeds internal array size"; |
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
121 |
final Object[] oarray = new Object[array.length]; |
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
122 |
|
16147 | 123 |
for (int index = 0; index < length; index++) { |
124 |
oarray[index] = Integer.valueOf(array[index]); |
|
125 |
} |
|
126 |
||
127 |
return oarray; |
|
128 |
} |
|
129 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
130 |
private double[] toDoubleArray() { |
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
131 |
assert length <= array.length : "length exceeds internal array size"; |
16147 | 132 |
final double[] darray = new double[array.length]; |
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
133 |
|
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
134 |
for (int index = 0; index < length; index++) { |
16147 | 135 |
darray[index] = array[index]; |
136 |
} |
|
137 |
||
138 |
return darray; |
|
139 |
} |
|
140 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
141 |
private long[] toLongArray() { |
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
142 |
assert length <= array.length : "length exceeds internal array size"; |
16147 | 143 |
final long[] larray = new long[array.length]; |
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
144 |
|
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
145 |
for (int index = 0; index < length; index++) { |
16147 | 146 |
larray[index] = array[index]; |
147 |
} |
|
148 |
||
149 |
return larray; |
|
150 |
} |
|
151 |
||
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
152 |
private LongArrayData convertToLong() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
153 |
return new LongArrayData(toLongArray(), (int)length); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
154 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
155 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
156 |
private NumberArrayData convertToDouble() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
157 |
return new NumberArrayData(toDoubleArray(), (int)length); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
158 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
159 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
160 |
private ObjectArrayData convertToObject() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
161 |
return new ObjectArrayData(toObjectArray(), (int)length); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
162 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
163 |
|
16147 | 164 |
@Override |
165 |
public ArrayData convert(final Class<?> type) { |
|
166 |
if (type == Integer.class) { |
|
167 |
return this; |
|
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
168 |
} |
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
169 |
if (type == Long.class) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
170 |
return convertToLong(); |
16147 | 171 |
} else if (type == Double.class) { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
172 |
return convertToDouble(); |
16147 | 173 |
} else { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
174 |
assert type == null || (!Number.class.isAssignableFrom(type) && !type.isPrimitive()); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
175 |
return convertToObject(); |
16147 | 176 |
} |
177 |
} |
|
178 |
||
179 |
@Override |
|
180 |
public void shiftLeft(final int by) { |
|
181 |
System.arraycopy(array, by, array, 0, array.length - by); |
|
182 |
} |
|
183 |
||
184 |
@Override |
|
185 |
public ArrayData shiftRight(final int by) { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
186 |
final ArrayData newData = ensure(by + length - 1); |
16147 | 187 |
if (newData != this) { |
188 |
newData.shiftRight(by); |
|
189 |
return newData; |
|
190 |
} |
|
191 |
System.arraycopy(array, 0, array, by, array.length - by); |
|
192 |
||
193 |
return this; |
|
194 |
} |
|
195 |
||
196 |
@Override |
|
197 |
public ArrayData ensure(final long safeIndex) { |
|
24720 | 198 |
if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) { |
16147 | 199 |
return new SparseArrayData(this, safeIndex + 1); |
200 |
} |
|
24720 | 201 |
final int alen = array.length; |
202 |
if (safeIndex >= alen) { |
|
203 |
final int newLength = ArrayData.nextSize((int)safeIndex); |
|
16147 | 204 |
array = Arrays.copyOf(array, newLength); |
205 |
} |
|
206 |
setLength(safeIndex + 1); |
|
207 |
return this; |
|
208 |
} |
|
209 |
||
210 |
@Override |
|
211 |
public ArrayData shrink(final long newLength) { |
|
212 |
Arrays.fill(array, (int) newLength, array.length, 0); |
|
213 |
||
214 |
return this; |
|
215 |
} |
|
216 |
||
217 |
@Override |
|
218 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
|
24719 | 219 |
if (JSType.isRepresentableAsInt(value)) { |
220 |
return set(index, JSType.toInt32(value), strict); |
|
18879
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
221 |
} else if (value == ScriptRuntime.UNDEFINED) { |
382986b34105
8020283: Don't use exceptions for widening of ArrayData
hannesw
parents:
16151
diff
changeset
|
222 |
return new UndefinedArrayFilter(this).set(index, value, strict); |
16147 | 223 |
} |
224 |
||
225 |
final ArrayData newData = convert(value == null ? Object.class : value.getClass()); |
|
226 |
return newData.set(index, value, strict); |
|
227 |
} |
|
228 |
||
229 |
@Override |
|
230 |
public ArrayData set(final int index, final int value, final boolean strict) { |
|
231 |
array[index] = value; |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
232 |
setLength(Math.max(index + 1, length)); |
16147 | 233 |
|
234 |
return this; |
|
235 |
} |
|
236 |
||
237 |
@Override |
|
238 |
public ArrayData set(final int index, final long value, final boolean strict) { |
|
24719 | 239 |
if (JSType.isRepresentableAsInt(value)) { |
240 |
array[index] = JSType.toInt32(value); |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
241 |
setLength(Math.max(index + 1, length)); |
16147 | 242 |
return this; |
243 |
} |
|
244 |
||
245 |
return convert(Long.class).set(index, value, strict); |
|
246 |
} |
|
247 |
||
248 |
@Override |
|
249 |
public ArrayData set(final int index, final double value, final boolean strict) { |
|
250 |
if (JSType.isRepresentableAsInt(value)) { |
|
251 |
array[index] = (int)(long)value; |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
252 |
setLength(Math.max(index + 1, length)); |
16147 | 253 |
return this; |
254 |
} |
|
255 |
||
256 |
return convert(Double.class).set(index, value, strict); |
|
257 |
} |
|
258 |
||
259 |
@Override |
|
24719 | 260 |
public Type getOptimisticType() { |
261 |
return Type.INT; |
|
262 |
} |
|
263 |
||
264 |
@Override |
|
16147 | 265 |
public int getInt(final int index) { |
266 |
return array[index]; |
|
267 |
} |
|
268 |
||
269 |
@Override |
|
24719 | 270 |
public int getIntOptimistic(final int index, final int programPoint) { |
271 |
return array[index]; |
|
272 |
} |
|
273 |
||
274 |
@Override |
|
16147 | 275 |
public long getLong(final int index) { |
276 |
return array[index]; |
|
277 |
} |
|
278 |
||
279 |
@Override |
|
24719 | 280 |
public long getLongOptimistic(final int index, final int programPoint) { |
281 |
return array[index]; |
|
282 |
} |
|
283 |
||
284 |
@Override |
|
16147 | 285 |
public double getDouble(final int index) { |
286 |
return array[index]; |
|
287 |
} |
|
288 |
||
289 |
@Override |
|
24719 | 290 |
public double getDoubleOptimistic(final int index, final int programPoint) { |
291 |
return array[index]; |
|
292 |
} |
|
293 |
||
294 |
@Override |
|
16147 | 295 |
public Object getObject(final int index) { |
296 |
return array[index]; |
|
297 |
} |
|
298 |
||
299 |
@Override |
|
300 |
public boolean has(final int index) { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
301 |
return 0 <= index && index < length; |
16147 | 302 |
} |
303 |
||
304 |
@Override |
|
305 |
public ArrayData delete(final int index) { |
|
306 |
return new DeletedRangeArrayFilter(this, index, index); |
|
307 |
} |
|
308 |
||
309 |
@Override |
|
310 |
public ArrayData delete(final long fromIndex, final long toIndex) { |
|
311 |
return new DeletedRangeArrayFilter(this, fromIndex, toIndex); |
|
312 |
} |
|
313 |
||
314 |
@Override |
|
315 |
public Object pop() { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
316 |
if (length == 0) { |
16147 | 317 |
return ScriptRuntime.UNDEFINED; |
318 |
} |
|
319 |
||
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
320 |
final int newLength = (int)length - 1; |
16147 | 321 |
final int elem = array[newLength]; |
322 |
array[newLength] = 0; |
|
323 |
setLength(newLength); |
|
324 |
||
325 |
return elem; |
|
326 |
} |
|
327 |
||
328 |
@Override |
|
329 |
public ArrayData slice(final long from, final long to) { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
330 |
final long start = from < 0 ? from + length : from; |
16147 | 331 |
final long newLength = to - start; |
332 |
||
333 |
return new IntArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength); |
|
334 |
} |
|
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
335 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
336 |
@Override |
24720 | 337 |
public final ArrayData push(final boolean strict, final int item) { |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
338 |
final long len = length; |
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
339 |
final ArrayData newData = ensure(len); |
24720 | 340 |
if (newData == this) { |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
341 |
array[(int)len] = item; |
24720 | 342 |
return this; |
343 |
} |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
344 |
return newData.set((int)len, item, strict); |
24720 | 345 |
} |
346 |
||
347 |
@Override |
|
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
348 |
public ArrayData fastSplice(final int start, final int removed, final int added) throws UnsupportedOperationException { |
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
349 |
final long oldLength = length; |
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
350 |
final long newLength = oldLength - removed + added; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
351 |
if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
352 |
throw new UnsupportedOperationException(); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
353 |
} |
24720 | 354 |
final ArrayData returnValue = removed == 0 ? |
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
355 |
EMPTY_ARRAY : new IntArrayData(Arrays.copyOfRange(array, start, start + removed), removed); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
356 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
357 |
if (newLength != oldLength) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
358 |
final int[] newArray; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
359 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
360 |
if (newLength > array.length) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
361 |
newArray = new int[ArrayData.nextSize((int)newLength)]; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
362 |
System.arraycopy(array, 0, newArray, 0, start); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
363 |
} else { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
364 |
newArray = array; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
365 |
} |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
366 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
367 |
System.arraycopy(array, start + removed, newArray, start + added, (int)(oldLength - start - removed)); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
368 |
array = newArray; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
369 |
setLength(newLength); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
370 |
} |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
371 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
372 |
return returnValue; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
373 |
} |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
374 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
375 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
376 |
public long fastPush(final int arg) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
377 |
final int len = (int)length; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
378 |
if (len == array.length) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
379 |
array = Arrays.copyOf(array, nextSize(len)); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
380 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
381 |
array[len] = arg; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
382 |
return ++length; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
383 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
384 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
385 |
//length must not be zero |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
386 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
387 |
public int fastPopInt() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
388 |
if (length == 0) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
389 |
throw new ClassCastException(); //relink |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
390 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
391 |
final int newLength = (int)--length; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
392 |
final int elem = array[newLength]; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
393 |
array[newLength] = 0; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
394 |
return elem; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
395 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
396 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
397 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
398 |
public long fastPopLong() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
399 |
return fastPopInt(); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
400 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
401 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
402 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
403 |
public double fastPopDouble() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
404 |
return fastPopInt(); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
405 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
406 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
407 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
408 |
public Object fastPopObject() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
409 |
return fastPopInt(); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
410 |
} |
16147 | 411 |
} |