unbox-double-arrays.js 18.5 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
  for (var i= 0; i < approx_dict_to_elements_threshold; ++i ) {
    a[i] = expected_array_value(i);
  }
50
  assertTrue(%HasDoubleElements(a));
51 52
}

53

54
function testOneArrayType(allocator) {
55
  var large_array = new allocator(large_array_size);
56
  force_to_fast_double_array(large_array);
57
  var six = 6;
58 59 60 61 62

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

63 64 65
  // This function has a constant and won't get inlined.
  function computed_6() {
    return six;
66 67
  }

68 69 70
  // 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) {
71
    assertTrue(%HasDoubleElements(a));
72 73 74 75 76
    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);
77
    assertTrue(%HasDoubleElements(a));
78 79 80
  }

  function test_various_loads2(a, value_5, value_6, value_7) {
81
    assertTrue(%HasDoubleElements(a));
82 83 84 85 86
    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);
87
    assertTrue(%HasDoubleElements(a));
88 89 90
  }

  function test_various_loads3(a, value_5, value_6, value_7) {
91
    assertTrue(%HasDoubleElements(a));
92 93 94 95 96
    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);
97
    assertTrue(%HasDoubleElements(a));
98 99 100
  }

  function test_various_loads4(a, value_5, value_6, value_7) {
101
    assertTrue(%HasDoubleElements(a));
102 103 104 105 106
    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);
107
    assertTrue(%HasDoubleElements(a));
108 109
  }

110
  function test_various_loads5(a, value_5, value_6, value_7) {
111
    assertTrue(%HasDoubleElements(a));
112 113 114 115 116 117 118 119 120
    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);
121
    assertTrue(%HasDoubleElements(a));
122 123 124
  }

  function test_various_loads6(a, value_5, value_6, value_7) {
125
    assertTrue(%HasDoubleElements(a));
126 127 128 129 130
    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);
131
    assertTrue(%HasDoubleElements(a));
132 133
  }

134
  function test_various_loads7(a, value_5, value_6, value_7) {
135
    assertTrue(%HasDoubleElements(a));
136 137 138 139 140
    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);
141
    assertTrue(%HasDoubleElements(a));
142 143 144
  }

  function test_various_stores(a, value_5, value_6, value_7) {
145
    assertTrue(%HasDoubleElements(a));
146 147 148
    a[5] = value_5;
    a[computed_6()] = value_6;
    a[7] = value_7;
149
    assertTrue(%HasDoubleElements(a));
150
  }
151

152 153 154 155 156 157 158
  %PrepareFunctionForOptimization(test_various_loads);
  %PrepareFunctionForOptimization(test_various_loads2);
  %PrepareFunctionForOptimization(test_various_loads3);
  %PrepareFunctionForOptimization(test_various_loads6);
  %PrepareFunctionForOptimization(test_various_loads7);
  %PrepareFunctionForOptimization(test_various_stores);

159 160 161 162 163 164 165 166 167 168 169 170 171
  // 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));
172 173 174 175 176
  %OptimizeFunctionOnNextCall(test_various_loads);
  test_various_loads(large_array,
                     expected_array_value(5),
                     expected_array_value(6),
                     expected_array_value(7));
177 178 179 180

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

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  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));
198 199 200 201 202 203 204

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

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  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));
222 223 224 225

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

  // 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));
236 237 238 239 240 241 242 243
  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));
244 245
  delete large_array[5];
  delete large_array[6];
246 247 248 249 250 251 252 253
  test_various_loads5(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));
  test_various_loads5(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));
254

255
  // Make sure Crankshaft code handles the hole correctly (bailout)
256 257
  var large_array = new allocator(large_array_size);
  force_to_fast_double_array(large_array);
258 259
  test_various_stores(large_array,
                      expected_array_value(5),
260 261 262 263 264 265 266 267 268 269 270 271
                      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,
272 273 274 275
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

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

283
  %DeoptimizeFunction(test_various_loads6);
284 285
  %ClearFunctionFeedback(test_various_stores);
  %ClearFunctionFeedback(test_various_loads7);
286

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

296 297 298 299
  test_various_stores(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));
300

301 302 303 304 305 306 307 308 309 310 311 312 313
  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,
314 315 316 317 318
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

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

  test_various_stores(large_array,
325 326
                      NaN,
                      -NaN,
327 328
                      expected_array_value(7));

329
  test_various_loads7(large_array,
330 331 332
                      NaN,
                      -NaN,
                      expected_array_value(7));
333

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

340 341 342 343
  test_various_stores(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));
344

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

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

354 355 356 357 358 359 360
class ArraySubclass extends Array {
  constructor(...args) {
    super(...args);
    this.marker = 42;
  }
}

361 362 363
// Force gc here to start with a clean heap if we repeat this test multiple
// times.
gc();
364
testOneArrayType(Array);
365
testOneArrayType(ArraySubclass);
366 367 368

var large_array = new Array(large_array_size);
force_to_fast_double_array(large_array);
369
assertTrue(%HasDoubleElements(large_array));
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390

// 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();
391
assertTrue(%HasObjectElements(large_array2));
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
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]);
409
assertTrue(%HasDoubleElements(large_array3));
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
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);
425
assertTrue(%HasDoubleElements(large_array3));
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
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;
443
assertTrue(%HasDoubleElements(large_array3));
444 445 446 447 448 449 450 451 452
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();
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472

// 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);
}

473
%PrepareFunctionForOptimization(call_apply);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
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);
}

494
%PrepareFunctionForOptimization(test_for_in);
495 496 497 498 499 500 501
test_for_in();
test_for_in();
test_for_in();
%OptimizeFunctionOnNextCall(test_for_in);
test_for_in();
test_for_in();
test_for_in();
502 503 504 505 506 507 508 509 510 511 512 513 514

// 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]);
}

515
%PrepareFunctionForOptimization(test_getter);
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
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]);
}

544
%PrepareFunctionForOptimization(test_setter);
545 546 547 548 549 550 551
test_setter();
test_setter();
test_setter();
%OptimizeFunctionOnNextCall(test_setter);
test_setter();
test_setter();
test_setter();