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