unbox-double-arrays.js 18.1 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Test dictionary -> double elements -> dictionary elements round trip

30 31
// Flags: --allow-natives-syntax --unbox-double-arrays --expose-gc

32
var large_array_size = 100000;
33
var approx_dict_to_elements_threshold = 70000;
34

35 36
var name = 0;

37
function expected_array_value(i) {
38
  if ((i % 50) != 0) {
39 40 41
    return i;
  } else {
    return i + 0.5;
42 43 44
  }
}

45
function force_to_fast_double_array(a) {
46
  a[large_array_size - 2] = 1;
47 48 49 50
  for (var i= 0; i < approx_dict_to_elements_threshold; ++i ) {
    a[i] = expected_array_value(i);
  }
  assertTrue(%HasFastDoubleElements(a));
51 52
}

53 54 55 56 57 58
function make_object_like_array(size) {
  obj = new Object();
  obj.length = size;
  return obj;
}

59
function testOneArrayType(allocator) {
60
  var large_array = new allocator(large_array_size);
61
  force_to_fast_double_array(large_array);
62
  var six = 6;
63 64 65 66 67

  for (var i= 0; i < approx_dict_to_elements_threshold; i += 501 ) {
    assertEquals(expected_array_value(i), large_array[i]);
  }

68 69 70
  // This function has a constant and won't get inlined.
  function computed_6() {
    return six;
71 72
  }

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  // Multiple versions of the test function makes sure that IC/Crankshaft state
  // doesn't get reused.
  function test_various_loads(a, value_5, value_6, value_7) {
    assertTrue(%HasFastDoubleElements(a));
    assertEquals(value_5, a[5]);
    assertEquals(value_6, a[6]);
    assertEquals(value_6, a[computed_6()]); // Test non-constant key
    assertEquals(value_7, a[7]);
    assertEquals(large_array_size, a.length);
    assertTrue(%HasFastDoubleElements(a));
  }

  function test_various_loads2(a, value_5, value_6, value_7) {
    assertTrue(%HasFastDoubleElements(a));
    assertEquals(value_5, a[5]);
    assertEquals(value_6, a[6]);
    assertEquals(value_6, a[computed_6()]); // Test non-constant key
    assertEquals(value_7, a[7]);
    assertEquals(large_array_size, a.length);
    assertTrue(%HasFastDoubleElements(a));
  }

  function test_various_loads3(a, value_5, value_6, value_7) {
    assertTrue(%HasFastDoubleElements(a));
    assertEquals(value_5, a[5]);
    assertEquals(value_6, a[6]);
    assertEquals(value_6, a[computed_6()]); // Test non-constant key
    assertEquals(value_7, a[7]);
    assertEquals(large_array_size, a.length);
    assertTrue(%HasFastDoubleElements(a));
  }

  function test_various_loads4(a, value_5, value_6, value_7) {
    assertTrue(%HasFastDoubleElements(a));
    assertEquals(value_5, a[5]);
    assertEquals(value_6, a[6]);
    assertEquals(value_6, a[computed_6()]); // Test non-constant key
    assertEquals(value_7, a[7]);
    assertEquals(large_array_size, a.length);
    assertTrue(%HasFastDoubleElements(a));
113 114
  }

115
  function test_various_loads5(a, value_5, value_6, value_7) {
116 117 118 119 120 121 122 123 124 125 126 127 128 129
    assertTrue(%HasFastDoubleElements(a));
    if (value_5 != undefined) {
      assertEquals(value_5, a[5]);
    };
    if (value_6 != undefined) {
      assertEquals(value_6, a[6]);
      assertEquals(value_6, a[computed_6()]); // Test non-constant key
    }
    assertEquals(value_7, a[7]);
    assertEquals(large_array_size, a.length);
    assertTrue(%HasFastDoubleElements(a));
  }

  function test_various_loads6(a, value_5, value_6, value_7) {
130 131 132 133 134 135 136 137 138
    assertTrue(%HasFastDoubleElements(a));
    assertEquals(value_5, a[5]);
    assertEquals(value_6, a[6]);
    assertEquals(value_6, a[computed_6()]); // Test non-constant key
    assertEquals(value_7, a[7]);
    assertEquals(large_array_size, a.length);
    assertTrue(%HasFastDoubleElements(a));
  }

139
  function test_various_loads7(a, value_5, value_6, value_7) {
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    assertTrue(%HasFastDoubleElements(a));
    assertEquals(value_5, a[5]);
    assertEquals(value_6, a[6]);
    assertEquals(value_6, a[computed_6()]); // Test non-constant key
    assertEquals(value_7, a[7]);
    assertEquals(large_array_size, a.length);
    assertTrue(%HasFastDoubleElements(a));
  }

  function test_various_stores(a, value_5, value_6, value_7) {
    assertTrue(%HasFastDoubleElements(a));
    a[5] = value_5;
    a[computed_6()] = value_6;
    a[7] = value_7;
    assertTrue(%HasFastDoubleElements(a));
  }
156 157 158 159 160 161 162 163 164 165 166 167 168 169

  // Test double and integer values
  test_various_loads(large_array,
                     expected_array_value(5),
                     expected_array_value(6),
                     expected_array_value(7));
  test_various_loads(large_array,
                     expected_array_value(5),
                     expected_array_value(6),
                     expected_array_value(7));
  test_various_loads(large_array,
                     expected_array_value(5),
                     expected_array_value(6),
                     expected_array_value(7));
170 171 172 173 174
  %OptimizeFunctionOnNextCall(test_various_loads);
  test_various_loads(large_array,
                     expected_array_value(5),
                     expected_array_value(6),
                     expected_array_value(7));
175 176 177 178

  // Test NaN values
  test_various_stores(large_array, NaN, -NaN, expected_array_value(7));

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  test_various_loads2(large_array,
                      NaN,
                      -NaN,
                      expected_array_value(7));
  test_various_loads2(large_array,
                      NaN,
                      -NaN,
                      expected_array_value(7));
  test_various_loads2(large_array,
                      NaN,
                      -NaN,
                      expected_array_value(7));
  %OptimizeFunctionOnNextCall(test_various_loads2);
  test_various_loads2(large_array,
                      NaN,
                      -NaN,
                      expected_array_value(7));
196 197 198 199 200 201 202

  // Test Infinity values
  test_various_stores(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  test_various_loads3(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));
  test_various_loads3(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));
  test_various_loads3(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));
  %OptimizeFunctionOnNextCall(test_various_loads3);
  test_various_loads3(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));
220 221 222 223

  // Test the hole for the default runtime implementation.
  delete large_array[5];
  delete large_array[6];
224 225 226 227
  test_various_loads4(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));
228 229 230 231 232 233

  // Test the keyed load IC implementation when the value is the hole.
  test_various_stores(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));
234 235 236 237 238 239 240 241
  test_various_loads5(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));
  test_various_loads5(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));
242 243
  delete large_array[5];
  delete large_array[6];
244 245 246 247 248 249 250 251
  test_various_loads5(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));
  test_various_loads5(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));
252

253
  // Make sure Crankshaft code handles the hole correctly (bailout)
254 255
  var large_array = new allocator(large_array_size);
  force_to_fast_double_array(large_array);
256 257
  test_various_stores(large_array,
                      expected_array_value(5),
258 259 260 261 262 263 264 265 266 267 268 269
                      expected_array_value(6),
                      expected_array_value(7));
  test_various_loads6(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));
  test_various_loads6(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));
  %OptimizeFunctionOnNextCall(test_various_loads6);
  test_various_loads6(large_array,
270 271 272 273
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

274 275 276 277 278 279 280
  delete large_array[5];
  delete large_array[6];
  test_various_loads6(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));

281
  %DeoptimizeFunction(test_various_loads6);
282 283
  %ClearFunctionFeedback(test_various_stores);
  %ClearFunctionFeedback(test_various_loads7);
284

285
  // Test stores for non-NaN.
286 287
  var large_array = new allocator(large_array_size);
  force_to_fast_double_array(large_array);
288
  %OptimizeFunctionOnNextCall(test_various_stores);
289
  test_various_stores(large_array,
290 291 292 293
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

294 295 296 297
  test_various_stores(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));
298

299 300 301 302 303 304 305 306 307 308 309 310 311
  test_various_loads7(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

  test_various_loads7(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

  %OptimizeFunctionOnNextCall(test_various_loads7);

  test_various_loads7(large_array,
312 313 314 315 316
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

  // Test NaN behavior for stores.
317 318 319 320
  test_various_stores(large_array,
                      NaN,
                      -NaN,
                      expected_array_value(7));
321 322

  test_various_stores(large_array,
323 324
                      NaN,
                      -NaN,
325 326
                      expected_array_value(7));

327
  test_various_loads7(large_array,
328 329 330
                      NaN,
                      -NaN,
                      expected_array_value(7));
331

332
  // Test Infinity behavior for stores.
333 334 335 336
  test_various_stores(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));
337

338 339 340 341
  test_various_stores(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));
342

343
  test_various_loads7(large_array,
344 345
                      Infinity,
                      -Infinity,
346
                      expected_array_value(7));
347 348 349

  // Make sure that we haven't converted from fast double.
  assertTrue(%HasFastDoubleElements(large_array));
350 351
}

352 353 354
// Force gc here to start with a clean heap if we repeat this test multiple
// times.
gc();
355
testOneArrayType(make_object_like_array);
356
testOneArrayType(Array);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

var large_array = new Array(large_array_size);
force_to_fast_double_array(large_array);
assertTrue(%HasFastDoubleElements(large_array));

// Cause the array to grow beyond it's JSArray length. This will double the
// size of the capacity and force the array into "slow" dictionary case.
large_array[5] = Infinity;
large_array[large_array_size+10001] = 50;
assertTrue(%HasDictionaryElements(large_array));
assertEquals(50, large_array[large_array_size+10001]);
assertEquals(large_array_size+10002, large_array.length);
assertEquals(Infinity, large_array[5]);
assertEquals(undefined, large_array[large_array_size-1]);
assertEquals(undefined, large_array[-1]);
assertEquals(large_array_size+10002, large_array.length);

// Test dictionary -> double elements -> fast elements.
var large_array2 = new Array(large_array_size);
force_to_fast_double_array(large_array2);
delete large_array2[5];

// Convert back to fast elements and make sure the contents of the array are
// unchanged.
large_array2[25] = new Object();
382
assertTrue(%HasFastObjectElements(large_array2));
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
for (var i= 0; i < approx_dict_to_elements_threshold; i += 500 ) {
  if (i != 25 && i != 5) {
    assertEquals(expected_array_value(i), large_array2[i]);
  }
}
assertEquals(undefined, large_array2[5]);
assertEquals(undefined, large_array2[large_array_size-1]);
assertEquals(undefined, large_array2[-1]);
assertEquals(large_array_size, large_array2.length);

// Make sure it's possible to change the array's length and that array is still
// intact after the resize.
var large_array3 = new Array(large_array_size);
force_to_fast_double_array(large_array3);
large_array3.length = 60000;
assertEquals(60000, large_array3.length);
assertEquals(undefined, large_array3[60000]);
assertTrue(%HasFastDoubleElements(large_array3));
assertEquals(expected_array_value(5), large_array3[5]);
assertEquals(expected_array_value(6), large_array3[6]);
assertEquals(expected_array_value(7), large_array3[7]);
assertEquals(expected_array_value(large_array3.length-1),
             large_array3[large_array3.length-1]);
assertEquals(undefined, large_array3[large_array_size-1]);
assertEquals(undefined, large_array3[-1]);
gc();

for (var i= 0; i < large_array3.length; i += 501 ) {
  assertEquals(expected_array_value(i), large_array3[i]);
}

large_array3.length = 25;
assertEquals(25, large_array3.length);
assertTrue(%HasFastDoubleElements(large_array3));
assertEquals(undefined, large_array3[25]);
assertEquals(expected_array_value(5), large_array3[5]);
assertEquals(expected_array_value(6), large_array3[6]);
assertEquals(expected_array_value(7), large_array3[7]);
assertEquals(expected_array_value(large_array3.length-1),
             large_array3[large_array3.length-1]);
assertEquals(undefined, large_array3[large_array_size-1]);
assertEquals(undefined, large_array3[-1]);
gc();

for (var i= 0; i < large_array3.length; ++i) {
  assertEquals(expected_array_value(i), large_array3[i]);
}

large_array3.length = 100;
assertEquals(100, large_array3.length);
large_array3[95] = 95;
assertTrue(%HasFastDoubleElements(large_array3));
assertEquals(undefined, large_array3[100]);
assertEquals(95, large_array3[95]);
assertEquals(expected_array_value(5), large_array3[5]);
assertEquals(expected_array_value(6), large_array3[6]);
assertEquals(expected_array_value(7), large_array3[7]);
assertEquals(undefined, large_array3[large_array3.length-1]);
assertEquals(undefined, large_array3[large_array_size-1]);
assertEquals(undefined, large_array3[-1]);
gc();
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

// Test apply on arrays backed by double elements.
function called_by_apply(arg0, arg1, arg2, arg3, arg4, arg5, arg6) {
  assertEquals(expected_array_value(0), arg0);
  assertEquals(NaN, arg1);
  assertEquals(-NaN, arg2);
  assertEquals(Infinity, arg3);
  assertEquals(-Infinity, arg4);
  assertEquals(expected_array_value(5), arg5);
}

large_array3[1] = NaN;
large_array3[2] = -NaN;
large_array3[3] = Infinity;
large_array3[4] = -Infinity;

function call_apply() {
  called_by_apply.apply({}, large_array3);
}

call_apply();
call_apply();
call_apply();
%OptimizeFunctionOnNextCall(call_apply);
call_apply();
call_apply();
call_apply();

function test_for_in() {
  // Due to previous tests, keys 0..25 and 95 should be present.
  next_expected = 0;
  for (x in large_array3) {
    assertTrue(next_expected++ == x);
    if (next_expected == 25) {
      next_expected = 95;
    }
  }
  assertTrue(next_expected == 96);
}

test_for_in();
test_for_in();
test_for_in();
%OptimizeFunctionOnNextCall(test_for_in);
test_for_in();
test_for_in();
test_for_in();
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538

// Test elements getters.
assertEquals(expected_array_value(10), large_array3[10]);
assertEquals(expected_array_value(-NaN), large_array3[2]);
large_array3.__defineGetter__("2", function(){
    return expected_array_value(10);
});

function test_getter() {
  assertEquals(expected_array_value(10), large_array3[10]);
  assertEquals(expected_array_value(10), large_array3[2]);
}

test_getter();
test_getter();
test_getter();
%OptimizeFunctionOnNextCall(test_getter);
test_getter();
test_getter();
test_getter();

// Test element setters.
large_array4 = new Array(large_array_size);
force_to_fast_double_array(large_array4);

var setter_called = false;

assertEquals(expected_array_value(10), large_array4[10]);
assertEquals(expected_array_value(2), large_array4[2]);
large_array4.__defineSetter__("10", function(value){
    setter_called = true;
  });

function test_setter() {
  setter_called = false;
  large_array4[10] = 119;
  assertTrue(setter_called);
  assertEquals(undefined, large_array4[10]);
  assertEquals(expected_array_value(2), large_array4[2]);
}

test_setter();
test_setter();
test_setter();
%OptimizeFunctionOnNextCall(test_setter);
test_setter();
test_setter();
test_setter();