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 Object has been |
|
38 |
* written to the array |
|
39 |
*/ |
|
24720 | 40 |
final class ObjectArrayData extends ContinuousArrayData { |
16147 | 41 |
|
42 |
/** |
|
43 |
* The wrapped array |
|
44 |
*/ |
|
45 |
private Object[] array; |
|
46 |
||
47 |
/** |
|
48 |
* Constructor |
|
49 |
* @param array an int array |
|
50 |
* @param length a length, not necessarily array.length |
|
51 |
*/ |
|
52 |
ObjectArrayData(final Object array[], final int length) { |
|
53 |
super(length); |
|
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
54 |
assert array.length >= length; |
16147 | 55 |
this.array = array; |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
56 |
} |
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
57 |
|
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
58 |
@Override |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
59 |
public Class<?> getElementType() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
60 |
return Object.class; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
61 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
62 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
63 |
@Override |
19236
73d242d205f9
8020132: Big object literal with numerical keys exceeds method size
hannesw
parents:
18842
diff
changeset
|
64 |
public ArrayData copy() { |
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
65 |
return new ObjectArrayData(array.clone(), (int)length); |
16147 | 66 |
} |
67 |
||
68 |
@Override |
|
69 |
public Object[] asObjectArray() { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
70 |
return array.length == length ? array.clone() : asObjectArrayCopy(); |
24719 | 71 |
} |
72 |
||
73 |
private Object[] asObjectArrayCopy() { |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
74 |
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
|
75 |
assert len <= Integer.MAX_VALUE; |
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
76 |
final Object[] copy = new Object[(int)len]; |
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
77 |
System.arraycopy(array, 0, copy, 0, (int)len); |
24719 | 78 |
return copy; |
16147 | 79 |
} |
80 |
||
81 |
@Override |
|
82 |
public ArrayData convert(final Class<?> type) { |
|
83 |
return this; |
|
84 |
} |
|
85 |
||
86 |
@Override |
|
87 |
public void shiftLeft(final int by) { |
|
88 |
System.arraycopy(array, by, array, 0, array.length - by); |
|
89 |
} |
|
90 |
||
91 |
@Override |
|
92 |
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
|
93 |
final ArrayData newData = ensure(by + length - 1); |
16147 | 94 |
if (newData != this) { |
95 |
newData.shiftRight(by); |
|
96 |
return newData; |
|
97 |
} |
|
98 |
System.arraycopy(array, 0, array, by, array.length - by); |
|
99 |
return this; |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public ArrayData ensure(final long safeIndex) { |
|
24720 | 104 |
if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) { |
16147 | 105 |
return new SparseArrayData(this, safeIndex + 1); |
106 |
} |
|
24720 | 107 |
final int alen = array.length; |
108 |
if (safeIndex >= alen) { |
|
109 |
final int newLength = ArrayData.nextSize((int)safeIndex); |
|
110 |
array = Arrays.copyOf(array, newLength); //fill with undefined or OK? TODO |
|
16147 | 111 |
} |
112 |
setLength(safeIndex + 1); |
|
113 |
return this; |
|
114 |
} |
|
115 |
||
116 |
@Override |
|
117 |
public ArrayData shrink(final long newLength) { |
|
118 |
Arrays.fill(array, (int) newLength, array.length, ScriptRuntime.UNDEFINED); |
|
119 |
return this; |
|
120 |
} |
|
121 |
||
122 |
@Override |
|
123 |
public ArrayData set(final int index, final Object value, final boolean strict) { |
|
124 |
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
|
125 |
setLength(Math.max(index + 1, length)); |
16147 | 126 |
return this; |
127 |
} |
|
128 |
||
129 |
@Override |
|
130 |
public ArrayData set(final int index, final int value, final boolean strict) { |
|
131 |
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
|
132 |
setLength(Math.max(index + 1, length)); |
16147 | 133 |
return this; |
134 |
} |
|
135 |
||
136 |
@Override |
|
137 |
public ArrayData set(final int index, final long value, final boolean strict) { |
|
138 |
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
|
139 |
setLength(Math.max(index + 1, length)); |
16147 | 140 |
return this; |
141 |
} |
|
142 |
||
143 |
@Override |
|
144 |
public ArrayData set(final int index, final double value, final boolean strict) { |
|
145 |
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
|
146 |
setLength(Math.max(index + 1, length)); |
16147 | 147 |
return this; |
148 |
} |
|
149 |
||
150 |
@Override |
|
18606 | 151 |
public ArrayData setEmpty(final int index) { |
152 |
array[index] = ScriptRuntime.EMPTY; |
|
153 |
return this; |
|
154 |
} |
|
155 |
||
156 |
@Override |
|
157 |
public ArrayData setEmpty(final long lo, final long hi) { |
|
18842
3c3be808b593
8019585: Sometimes a var declaration using itself in its init wasn't declared as canBeUndefined, causing erroneous bytecode
lagergren
parents:
18606
diff
changeset
|
158 |
Arrays.fill(array, (int)Math.max(lo, 0L), (int)Math.min(hi, Integer.MAX_VALUE), ScriptRuntime.EMPTY); |
18606 | 159 |
return this; |
160 |
} |
|
161 |
||
162 |
@Override |
|
24719 | 163 |
public Type getOptimisticType() { |
164 |
return Type.OBJECT; |
|
165 |
} |
|
166 |
||
24741
4232289c3235
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents:
24720
diff
changeset
|
167 |
private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), ObjectArrayData.class, "getElem", Object.class, int.class).methodHandle(); |
4232289c3235
8040102: Remove all references to Unsafe and definition of anonymous clases from the code
lagergren
parents:
24720
diff
changeset
|
168 |
private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), ObjectArrayData.class, "setElem", void.class, int.class, Object.class).methodHandle(); |
24719 | 169 |
|
170 |
@SuppressWarnings("unused") |
|
171 |
private Object getElem(final int index) { |
|
172 |
if (has(index)) { |
|
173 |
return array[index]; |
|
174 |
} |
|
175 |
throw new ClassCastException(); |
|
176 |
} |
|
177 |
||
178 |
@SuppressWarnings("unused") |
|
179 |
private void setElem(final int index, final Object elem) { |
|
24720 | 180 |
if (hasRoomFor(index)) { |
181 |
array[index] = elem; |
|
182 |
return; |
|
183 |
} |
|
184 |
throw new ClassCastException(); |
|
24719 | 185 |
} |
186 |
||
187 |
@Override |
|
188 |
public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) { |
|
189 |
if (returnType.isPrimitive()) { |
|
190 |
return null; |
|
191 |
} |
|
192 |
return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint); |
|
193 |
} |
|
194 |
||
195 |
@Override |
|
196 |
public MethodHandle getElementSetter(final Class<?> elementType) { |
|
197 |
return getContinuousElementSetter(SET_ELEM, Object.class); |
|
198 |
} |
|
199 |
||
200 |
||
201 |
@Override |
|
16147 | 202 |
public int getInt(final int index) { |
203 |
return JSType.toInt32(array[index]); |
|
204 |
} |
|
205 |
||
206 |
@Override |
|
207 |
public long getLong(final int index) { |
|
208 |
return JSType.toLong(array[index]); |
|
209 |
} |
|
210 |
||
211 |
@Override |
|
212 |
public double getDouble(final int index) { |
|
213 |
return JSType.toNumber(array[index]); |
|
214 |
} |
|
215 |
||
216 |
@Override |
|
217 |
public Object getObject(final int index) { |
|
218 |
return array[index]; |
|
219 |
} |
|
220 |
||
221 |
@Override |
|
222 |
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
|
223 |
return 0 <= index && index < length; |
16147 | 224 |
} |
225 |
||
226 |
@Override |
|
227 |
public ArrayData delete(final int index) { |
|
18606 | 228 |
setEmpty(index); |
16147 | 229 |
return new DeletedRangeArrayFilter(this, index, index); |
230 |
} |
|
231 |
||
232 |
@Override |
|
233 |
public ArrayData delete(final long fromIndex, final long toIndex) { |
|
18606 | 234 |
setEmpty(fromIndex, toIndex); |
16147 | 235 |
return new DeletedRangeArrayFilter(this, fromIndex, toIndex); |
236 |
} |
|
237 |
||
238 |
@Override |
|
26768
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
239 |
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
|
240 |
return fastPush((Object)arg); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
241 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
242 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
243 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
244 |
public long fastPush(final long arg) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
245 |
return fastPush((Object)arg); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
246 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
247 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
248 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
249 |
public long fastPush(final double arg) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
250 |
return fastPush((Object)arg); |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
251 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
252 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
253 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
254 |
public long fastPush(final Object arg) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
255 |
final int len = (int)length; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
256 |
if (len == array.length) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
257 |
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
|
258 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
259 |
array[len] = arg; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
260 |
return ++length; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
261 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
262 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
263 |
@Override |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
264 |
public Object fastPopObject() { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
265 |
if (length == 0) { |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
266 |
return ScriptRuntime.UNDEFINED; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
267 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
268 |
final int newLength = (int)--length; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
269 |
final Object elem = array[newLength]; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
270 |
array[newLength] = ScriptRuntime.EMPTY; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
271 |
return elem; |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
272 |
} |
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
273 |
|
751b0f427090
8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
lagergren
parents:
25865
diff
changeset
|
274 |
@Override |
16147 | 275 |
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
|
276 |
if (length == 0) { |
16147 | 277 |
return ScriptRuntime.UNDEFINED; |
278 |
} |
|
279 |
||
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
280 |
final int newLength = (int)length - 1; |
16147 | 281 |
final Object elem = array[newLength]; |
18606 | 282 |
setEmpty(newLength); |
16147 | 283 |
setLength(newLength); |
284 |
return elem; |
|
285 |
} |
|
286 |
||
287 |
@Override |
|
288 |
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
|
289 |
final long start = from < 0 ? from + length : from; |
16147 | 290 |
final long newLength = to - start; |
291 |
return new ObjectArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength); |
|
292 |
} |
|
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
293 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
294 |
@Override |
24720 | 295 |
public ArrayData push(final boolean strict, final Object 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
|
296 |
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
|
297 |
final ArrayData newData = ensure(len); |
24720 | 298 |
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
|
299 |
array[(int)len] = item; |
24720 | 300 |
return this; |
301 |
} |
|
26886
18c744ab4df2
8059211: Changed ArrayData.length accessor to use the protected field and fixed javadoc warnings related to this
lagergren
parents:
26768
diff
changeset
|
302 |
return newData.set((int)len, item, strict); |
24720 | 303 |
} |
304 |
||
305 |
@Override |
|
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
306 |
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
|
307 |
final long oldLength = length; |
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
308 |
final long newLength = oldLength - removed + added; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
309 |
if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
310 |
throw new UnsupportedOperationException(); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
311 |
} |
24720 | 312 |
final ArrayData returnValue = removed == 0 ? |
21438
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
313 |
EMPTY_ARRAY : new ObjectArrayData(Arrays.copyOfRange(array, start, start + removed), removed); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
314 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
315 |
if (newLength != oldLength) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
316 |
final Object[] newArray; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
317 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
318 |
if (newLength > array.length) { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
319 |
newArray = new Object[ArrayData.nextSize((int)newLength)]; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
320 |
System.arraycopy(array, 0, newArray, 0, start); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
321 |
} else { |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
322 |
newArray = array; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
323 |
} |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
324 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
325 |
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
|
326 |
array = newArray; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
327 |
setLength(newLength); |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
328 |
} |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
329 |
|
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
330 |
return returnValue; |
4292865c758b
8026701: Array.prototype.splice is slow on dense arrays
hannesw
parents:
19236
diff
changeset
|
331 |
} |
16147 | 332 |
} |