unbox-double-arrays.js 18 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 159 160 161 162 163 164

  // 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));
165 166 167 168 169
  %OptimizeFunctionOnNextCall(test_various_loads);
  test_various_loads(large_array,
                     expected_array_value(5),
                     expected_array_value(6),
                     expected_array_value(7));
170 171 172 173

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

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  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));
191 192 193 194 195 196 197

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

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  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));
215 216 217 218

  // Test the hole for the default runtime implementation.
  delete large_array[5];
  delete large_array[6];
219 220 221 222
  test_various_loads4(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));
223 224 225 226 227 228

  // 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));
229 230 231 232 233 234 235 236
  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));
237 238
  delete large_array[5];
  delete large_array[6];
239 240 241 242 243 244 245 246
  test_various_loads5(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));
  test_various_loads5(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));
247

248
  // Make sure Crankshaft code handles the hole correctly (bailout)
249 250
  var large_array = new allocator(large_array_size);
  force_to_fast_double_array(large_array);
251 252
  test_various_stores(large_array,
                      expected_array_value(5),
253 254 255 256 257 258 259 260 261 262 263 264
                      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,
265 266 267 268
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

269 270 271 272 273 274 275
  delete large_array[5];
  delete large_array[6];
  test_various_loads6(large_array,
                      undefined,
                      undefined,
                      expected_array_value(7));

276
  %DeoptimizeFunction(test_various_loads6);
277 278
  %ClearFunctionFeedback(test_various_stores);
  %ClearFunctionFeedback(test_various_loads7);
279

280
  // Test stores for non-NaN.
281 282
  var large_array = new allocator(large_array_size);
  force_to_fast_double_array(large_array);
283
  %OptimizeFunctionOnNextCall(test_various_stores);
284
  test_various_stores(large_array,
285 286 287 288
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

289 290 291 292
  test_various_stores(large_array,
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));
293

294 295 296 297 298 299 300 301 302 303 304 305 306
  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,
307 308 309 310 311
                      expected_array_value(5),
                      expected_array_value(6),
                      expected_array_value(7));

  // Test NaN behavior for stores.
312 313 314 315
  test_various_stores(large_array,
                      NaN,
                      -NaN,
                      expected_array_value(7));
316 317

  test_various_stores(large_array,
318 319
                      NaN,
                      -NaN,
320 321
                      expected_array_value(7));

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

327
  // Test Infinity behavior for stores.
328 329 330 331
  test_various_stores(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));
332

333 334 335 336
  test_various_stores(large_array,
                      Infinity,
                      -Infinity,
                      expected_array_value(7));
337

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

  // Make sure that we haven't converted from fast double.
344
  assertTrue(%HasDoubleElements(large_array));
345 346
}

347 348 349 350 351 352 353
class ArraySubclass extends Array {
  constructor(...args) {
    super(...args);
    this.marker = 42;
  }
}

354 355 356
// Force gc here to start with a clean heap if we repeat this test multiple
// times.
gc();
357
testOneArrayType(Array);
358
testOneArrayType(ArraySubclass);
359 360 361

var large_array = new Array(large_array_size);
force_to_fast_double_array(large_array);
362
assertTrue(%HasDoubleElements(large_array));
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

// 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();
384
assertTrue(%HasObjectElements(large_array2));
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
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]);
402
assertTrue(%HasDoubleElements(large_array3));
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
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);
418
assertTrue(%HasDoubleElements(large_array3));
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
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;
436
assertTrue(%HasDoubleElements(large_array3));
437 438 439 440 441 442 443 444 445
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();
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 491 492

// 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();
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 539 540

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