src/jdk/nashorn/internal/runtime/arrays/NumberArrayData.java

Thu, 25 Sep 2014 15:53:47 +0200

author
lagergren
date
Thu, 25 Sep 2014 15:53:47 +0200
changeset 1028
d79265f2fa92
parent 963
e2497b11a021
child 1056
db675278b4d3
permissions
-rw-r--r--

8025435: Optimistic builtins support, implemented initial optimistic versions of push, pop, and charCodeAt
Reviewed-by: hannesw, attila, sundar

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@3 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@3 9 * by Oracle in the LICENSE file that accompanied this code.
jlaskey@3 10 *
jlaskey@3 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 14 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 15 * accompanied this code).
jlaskey@3 16 *
jlaskey@3 17 * You should have received a copy of the GNU General Public License version
jlaskey@3 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 20 *
jlaskey@3 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 22 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 23 * questions.
jlaskey@3 24 */
jlaskey@3 25
jlaskey@3 26 package jdk.nashorn.internal.runtime.arrays;
jlaskey@3 27
attila@963 28 import static jdk.nashorn.internal.codegen.CompilerConstants.specialCall;
attila@963 29 import static jdk.nashorn.internal.lookup.Lookup.MH;
jlaskey@3 30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
attila@963 31 import java.lang.invoke.MethodHandle;
attila@963 32 import java.lang.invoke.MethodHandles;
jlaskey@3 33 import java.util.Arrays;
attila@963 34 import jdk.nashorn.internal.codegen.types.Type;
jlaskey@3 35
jlaskey@3 36 /**
jlaskey@3 37 * Implementation of {@link ArrayData} as soon as a double has been
jlaskey@3 38 * written to the array
jlaskey@3 39 */
lagergren@1028 40 final class NumberArrayData extends ContinuousArrayData implements NumericElements {
jlaskey@3 41 /**
jlaskey@3 42 * The wrapped array
jlaskey@3 43 */
jlaskey@3 44 private double[] array;
jlaskey@3 45
jlaskey@3 46 /**
jlaskey@3 47 * Constructor
jlaskey@3 48 * @param array an int array
jlaskey@3 49 * @param length a length, not necessarily array.length
jlaskey@3 50 */
jlaskey@3 51 NumberArrayData(final double array[], final int length) {
jlaskey@3 52 super(length);
hannesw@482 53 assert array.length >= length;
jlaskey@3 54 this.array = array;
hannesw@482 55 }
hannesw@482 56
hannesw@482 57 @Override
lagergren@1028 58 public Class<?> getElementType() {
lagergren@1028 59 return double.class;
lagergren@1028 60 }
lagergren@1028 61
lagergren@1028 62 @Override
hannesw@482 63 public ArrayData copy() {
lagergren@1028 64 return new NumberArrayData(array.clone(), (int)length);
jlaskey@3 65 }
jlaskey@3 66
jlaskey@3 67 @Override
jlaskey@3 68 public Object[] asObjectArray() {
lagergren@1028 69 return toObjectArray(array, (int)length);
jlaskey@3 70 }
jlaskey@3 71
jlaskey@3 72 private static Object[] toObjectArray(final double[] array, final int length) {
hannesw@442 73 assert length <= array.length : "length exceeds internal array size";
hannesw@442 74 final Object[] oarray = new Object[array.length];
hannesw@442 75
jlaskey@3 76 for (int index = 0; index < length; index++) {
jlaskey@3 77 oarray[index] = Double.valueOf(array[index]);
jlaskey@3 78 }
jlaskey@3 79 return oarray;
jlaskey@3 80 }
jlaskey@3 81
jlaskey@3 82 @Override
jlaskey@3 83 public Object asArrayOfType(final Class<?> componentType) {
jlaskey@3 84 if(componentType == double.class) {
lagergren@1028 85 return array.length == length ? array.clone() : Arrays.copyOf(array, (int)length);
jlaskey@3 86 }
jlaskey@3 87 return super.asArrayOfType(componentType);
jlaskey@3 88 }
jlaskey@3 89
jlaskey@3 90 @Override
jlaskey@3 91 public ArrayData convert(final Class<?> type) {
jlaskey@3 92 if (type != Double.class && type != Integer.class && type != Long.class) {
lagergren@1028 93 final int len = (int)length;
lagergren@1028 94 return new ObjectArrayData(NumberArrayData.toObjectArray(array, len), len);
jlaskey@3 95 }
jlaskey@3 96 return this;
jlaskey@3 97 }
jlaskey@3 98
jlaskey@3 99 @Override
jlaskey@3 100 public void shiftLeft(final int by) {
jlaskey@3 101 System.arraycopy(array, by, array, 0, array.length - by);
jlaskey@3 102 }
jlaskey@3 103
jlaskey@3 104 @Override
jlaskey@3 105 public ArrayData shiftRight(final int by) {
lagergren@1028 106 final ArrayData newData = ensure(by + length - 1);
jlaskey@3 107 if (newData != this) {
jlaskey@3 108 newData.shiftRight(by);
jlaskey@3 109 return newData;
jlaskey@3 110 }
jlaskey@3 111 System.arraycopy(array, 0, array, by, array.length - by);
jlaskey@3 112 return this;
jlaskey@3 113 }
jlaskey@3 114
jlaskey@3 115 @Override
jlaskey@3 116 public ArrayData ensure(final long safeIndex) {
attila@963 117 if (safeIndex >= SparseArrayData.MAX_DENSE_LENGTH) {
jlaskey@3 118 return new SparseArrayData(this, safeIndex + 1);
jlaskey@3 119 }
attila@963 120 final int alen = array.length;
attila@963 121 if (safeIndex >= alen) {
attila@963 122 final int newLength = ArrayData.nextSize((int)safeIndex);
attila@963 123 array = Arrays.copyOf(array, newLength); //todo fill with nan or never accessed?
attila@963 124 }
attila@963 125 setLength(safeIndex + 1);
attila@963 126 return this;
jlaskey@3 127
jlaskey@3 128 }
jlaskey@3 129
jlaskey@3 130 @Override
jlaskey@3 131 public ArrayData shrink(final long newLength) {
jlaskey@3 132 Arrays.fill(array, (int) newLength, array.length, 0.0);
jlaskey@3 133 return this;
jlaskey@3 134 }
jlaskey@3 135
jlaskey@3 136 @Override
jlaskey@3 137 public ArrayData set(final int index, final Object value, final boolean strict) {
hannesw@442 138 if (value instanceof Double || value instanceof Integer || value instanceof Long) {
hannesw@442 139 return set(index, ((Number)value).doubleValue(), strict);
hannesw@442 140 } else if (value == UNDEFINED) {
hannesw@442 141 return new UndefinedArrayFilter(this).set(index, value, strict);
jlaskey@3 142 }
hannesw@442 143
jlaskey@3 144 final ArrayData newData = convert(value == null ? Object.class : value.getClass());
jlaskey@3 145 return newData.set(index, value, strict);
jlaskey@3 146 }
jlaskey@3 147
jlaskey@3 148 @Override
jlaskey@3 149 public ArrayData set(final int index, final int value, final boolean strict) {
jlaskey@3 150 array[index] = value;
lagergren@1028 151 setLength(Math.max(index + 1, length));
jlaskey@3 152 return this;
jlaskey@3 153 }
jlaskey@3 154
jlaskey@3 155 @Override
jlaskey@3 156 public ArrayData set(final int index, final long value, final boolean strict) {
jlaskey@3 157 array[index] = value;
lagergren@1028 158 setLength(Math.max(index + 1, length));
jlaskey@3 159 return this;
jlaskey@3 160 }
jlaskey@3 161
jlaskey@3 162 @Override
jlaskey@3 163 public ArrayData set(final int index, final double value, final boolean strict) {
jlaskey@3 164 array[index] = value;
lagergren@1028 165 setLength(Math.max(index + 1, length));
jlaskey@3 166 return this;
jlaskey@3 167 }
jlaskey@3 168
jlaskey@3 169 @Override
attila@963 170 public Type getOptimisticType() {
attila@963 171 return Type.NUMBER;
attila@963 172 }
attila@963 173
attila@963 174 private static final MethodHandle HAS_GET_ELEM = specialCall(MethodHandles.lookup(), NumberArrayData.class, "getElem", double.class, int.class).methodHandle();
attila@963 175 private static final MethodHandle SET_ELEM = specialCall(MethodHandles.lookup(), NumberArrayData.class, "setElem", void.class, int.class, double.class).methodHandle();
attila@963 176
attila@963 177 @SuppressWarnings("unused")
attila@963 178 private double getElem(final int index) {
attila@963 179 if (has(index)) {
attila@963 180 return array[index];
attila@963 181 }
attila@963 182 throw new ClassCastException();
attila@963 183 }
attila@963 184
attila@963 185 @SuppressWarnings("unused")
attila@963 186 private void setElem(final int index, final double elem) {
attila@963 187 if (hasRoomFor(index)) {
attila@963 188 array[index] = elem;
attila@963 189 return;
attila@963 190 }
attila@963 191 throw new ClassCastException();
attila@963 192 }
attila@963 193
attila@963 194 @Override
attila@963 195 public MethodHandle getElementGetter(final Class<?> returnType, final int programPoint) {
attila@963 196 if (returnType == int.class || returnType == long.class) {
attila@963 197 return null;
attila@963 198 }
attila@963 199 return getContinuousElementGetter(HAS_GET_ELEM, returnType, programPoint);
attila@963 200 }
attila@963 201
attila@963 202 @Override
attila@963 203 public MethodHandle getElementSetter(final Class<?> elementType) {
attila@963 204 return elementType.isPrimitive() ? getContinuousElementSetter(MH.asType(SET_ELEM, SET_ELEM.type().changeParameterType(2, elementType)), elementType) : null;
attila@963 205 }
attila@963 206
attila@963 207 @Override
jlaskey@3 208 public int getInt(final int index) {
jlaskey@3 209 return (int)array[index];
jlaskey@3 210 }
jlaskey@3 211
jlaskey@3 212 @Override
jlaskey@3 213 public long getLong(final int index) {
jlaskey@3 214 return (long)array[index];
jlaskey@3 215 }
jlaskey@3 216
jlaskey@3 217 @Override
jlaskey@3 218 public double getDouble(final int index) {
jlaskey@3 219 return array[index];
jlaskey@3 220 }
jlaskey@3 221
jlaskey@3 222 @Override
attila@963 223 public double getDoubleOptimistic(final int index, final int programPoint) {
attila@963 224 return array[index];
attila@963 225 }
attila@963 226
attila@963 227 @Override
jlaskey@3 228 public Object getObject(final int index) {
jlaskey@3 229 return array[index];
jlaskey@3 230 }
jlaskey@3 231
jlaskey@3 232 @Override
jlaskey@3 233 public boolean has(final int index) {
lagergren@1028 234 return 0 <= index && index < length;
jlaskey@3 235 }
jlaskey@3 236
jlaskey@3 237 @Override
jlaskey@3 238 public ArrayData delete(final int index) {
jlaskey@3 239 return new DeletedRangeArrayFilter(this, index, index);
jlaskey@3 240 }
jlaskey@3 241
jlaskey@3 242 @Override
jlaskey@3 243 public ArrayData delete(final long fromIndex, final long toIndex) {
jlaskey@3 244 return new DeletedRangeArrayFilter(this, fromIndex, toIndex);
jlaskey@3 245 }
jlaskey@3 246
jlaskey@3 247 @Override
jlaskey@3 248 public Object pop() {
lagergren@1028 249 if (length == 0) {
jlaskey@3 250 return UNDEFINED;
jlaskey@3 251 }
jlaskey@3 252
lagergren@1028 253 final int newLength = (int)length - 1;
jlaskey@3 254 final double elem = array[newLength];
jlaskey@3 255 array[newLength] = 0;
jlaskey@3 256 setLength(newLength);
jlaskey@3 257 return elem;
jlaskey@3 258 }
jlaskey@3 259
jlaskey@3 260 @Override
jlaskey@3 261 public ArrayData slice(final long from, final long to) {
lagergren@1028 262 final long start = from < 0 ? from + length : from;
jlaskey@3 263 final long newLength = to - start;
jlaskey@3 264 return new NumberArrayData(Arrays.copyOfRange(array, (int)from, (int)to), (int)newLength);
jlaskey@3 265 }
hannesw@633 266
hannesw@633 267 @Override
attila@963 268 public final ArrayData push(final boolean strict, final double item) {
lagergren@1028 269 final long len = length;
lagergren@1028 270 final ArrayData newData = ensure(len);
attila@963 271 if (newData == this) {
lagergren@1028 272 array[(int)len] = item;
attila@963 273 return this;
attila@963 274 }
lagergren@1028 275 return newData.set((int)len, item, strict);
attila@963 276 }
attila@963 277
attila@963 278 @Override
hannesw@633 279 public ArrayData fastSplice(final int start, final int removed, final int added) throws UnsupportedOperationException {
lagergren@1028 280 final long oldLength = length;
hannesw@633 281 final long newLength = oldLength - removed + added;
hannesw@633 282 if (newLength > SparseArrayData.MAX_DENSE_LENGTH && newLength > array.length) {
hannesw@633 283 throw new UnsupportedOperationException();
hannesw@633 284 }
attila@963 285 final ArrayData returnValue = removed == 0 ?
hannesw@633 286 EMPTY_ARRAY : new NumberArrayData(Arrays.copyOfRange(array, start, start + removed), removed);
hannesw@633 287
hannesw@633 288 if (newLength != oldLength) {
hannesw@633 289 final double[] newArray;
hannesw@633 290
hannesw@633 291 if (newLength > array.length) {
hannesw@633 292 newArray = new double[ArrayData.nextSize((int)newLength)];
hannesw@633 293 System.arraycopy(array, 0, newArray, 0, start);
hannesw@633 294 } else {
hannesw@633 295 newArray = array;
hannesw@633 296 }
hannesw@633 297
hannesw@633 298 System.arraycopy(array, start + removed, newArray, start + added, (int)(oldLength - start - removed));
hannesw@633 299 array = newArray;
hannesw@633 300 setLength(newLength);
hannesw@633 301 }
hannesw@633 302
hannesw@633 303 return returnValue;
hannesw@633 304 }
lagergren@1028 305
lagergren@1028 306 @Override
lagergren@1028 307 public long fastPush(final int arg) {
lagergren@1028 308 return fastPush((double)arg);
lagergren@1028 309 }
lagergren@1028 310
lagergren@1028 311 @Override
lagergren@1028 312 public long fastPush(final long arg) {
lagergren@1028 313 return fastPush((double)arg);
lagergren@1028 314 }
lagergren@1028 315
lagergren@1028 316 @Override
lagergren@1028 317 public long fastPush(final double arg) {
lagergren@1028 318 final int len = (int)length;
lagergren@1028 319 if (len == array.length) {
lagergren@1028 320 //note that fastpush never creates spares arrays, there is nothing to gain by that - it will just use even more memory
lagergren@1028 321 array = Arrays.copyOf(array, nextSize(len));
lagergren@1028 322 }
lagergren@1028 323 array[len] = arg;
lagergren@1028 324 return ++length;
lagergren@1028 325 }
lagergren@1028 326
lagergren@1028 327 @Override
lagergren@1028 328 public double fastPopDouble() {
lagergren@1028 329 if (length == 0) {
lagergren@1028 330 throw new ClassCastException();
lagergren@1028 331 }
lagergren@1028 332 final int newLength = (int)--length;
lagergren@1028 333 final double elem = array[newLength];
lagergren@1028 334 array[newLength] = 0;
lagergren@1028 335 return elem;
lagergren@1028 336 }
lagergren@1028 337
lagergren@1028 338 @Override
lagergren@1028 339 public Object fastPopObject() {
lagergren@1028 340 return fastPopDouble();
lagergren@1028 341 }
jlaskey@3 342 }

mercurial