optimized-foreach.js 9.44 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax --expose-gc --turbo-inline-array-builtins

7 8 9 10 11 12 13 14 15 16 17 18
var a = [
  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13,
  14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0,  0
];
var b = [
  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13,
  14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
];
var c = [
  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13,
  14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
];
19 20 21 22

// Unknown field access leads to soft-deopt unrelated to forEach, should still
// lead to correct result.
(function() {
23 24 25 26 27 28
var result = 0;
var eagerDeoptInCalled = function(deopt) {
  var sum = function(v, i, o) {
    result += v;
    if (i == 13 && deopt) {
      a.abc = 25;
29
    }
30 31 32 33 34 35 36 37 38 39 40
  };
  a.forEach(sum);
};
%PrepareFunctionForOptimization(eagerDeoptInCalled);
eagerDeoptInCalled();
eagerDeoptInCalled();
%OptimizeFunctionOnNextCall(eagerDeoptInCalled);
eagerDeoptInCalled();
eagerDeoptInCalled(true);
eagerDeoptInCalled();
assertEquals(1500, result);
41 42 43 44
})();

// Length change detected during loop, must cause properly handled eager deopt.
(function() {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
var result = 0;
var eagerDeoptInCalled = function(deopt) {
  var sum = function(v, i, o) {
    result += v;
    a.length = i == 13 && deopt ? 25 : 27;
  };
  a.forEach(sum);
};
%PrepareFunctionForOptimization(eagerDeoptInCalled);
eagerDeoptInCalled();
eagerDeoptInCalled();
%OptimizeFunctionOnNextCall(eagerDeoptInCalled);
eagerDeoptInCalled();
eagerDeoptInCalled(true);
eagerDeoptInCalled();
assertEquals(1500, result);
61 62 63 64
})();

// Escape analyzed array
(function() {
65 66 67 68 69 70 71
var result = 0;
var eagerDeoptInCalled = function(deopt) {
  var a_noescape = [0, 1, 2, 3, 4, 5];
  var sum = function(v, i, o) {
    result += v;
    if (i == 13 && deopt) {
      a_noescape.length = 25;
72
    }
73 74 75 76 77 78 79 80 81 82 83
  };
  a_noescape.forEach(sum);
};
%PrepareFunctionForOptimization(eagerDeoptInCalled);
eagerDeoptInCalled();
eagerDeoptInCalled();
%OptimizeFunctionOnNextCall(eagerDeoptInCalled);
eagerDeoptInCalled();
eagerDeoptInCalled(true);
eagerDeoptInCalled();
assertEquals(75, result);
84 85 86 87 88 89
})();

// Escape analyzed array where sum function isn't inlined, forcing a lazy deopt
// with GC that relies on the stashed-away return result fro the lazy deopt
// being properly stored in a place on the stack that gets GC'ed.
(function() {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
var result = 0;
var lazyDeopt = function(deopt) {
  var b = [1, 2, 3];
  var sum = function(v, i, o) {
    result += i;
    if (i == 1 && deopt) {
      %DeoptimizeFunction(lazyDeopt);
    }
    gc();
    gc();
  };
  %NeverOptimizeFunction(sum);
  b.forEach(sum);
};
%PrepareFunctionForOptimization(lazyDeopt);
lazyDeopt();
lazyDeopt();
%OptimizeFunctionOnNextCall(lazyDeopt);
lazyDeopt();
lazyDeopt(true);
lazyDeopt();
111 112 113 114
})();

// Lazy deopt from runtime call from inlined callback function.
(function() {
115 116 117 118 119 120
var result = 0;
var lazyDeopt = function(deopt) {
  var sum = function(v, i, o) {
    result += i;
    if (i == 13 && deopt) {
      %DeoptimizeNow();
121
    }
122 123 124 125 126 127 128 129 130 131 132
  };
  b.forEach(sum);
};
%PrepareFunctionForOptimization(lazyDeopt);
lazyDeopt();
lazyDeopt();
%OptimizeFunctionOnNextCall(lazyDeopt);
lazyDeopt();
lazyDeopt(true);
lazyDeopt();
assertEquals(1500, result);
133 134 135 136
})();

// Lazy deopt from runtime call from non-inline callback function.
(function() {
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
var result = 0;
var lazyDeopt = function(deopt) {
  var sum = function(v, i, o) {
    result += i;
    if (i == 13 && deopt) {
      %DeoptimizeNow();
    }
  };
  %NeverOptimizeFunction(sum);
  b.forEach(sum);
};
%PrepareFunctionForOptimization(lazyDeopt);
lazyDeopt();
lazyDeopt();
%OptimizeFunctionOnNextCall(lazyDeopt);
lazyDeopt();
lazyDeopt(true);
lazyDeopt();
assertEquals(1500, result);
156 157 158
})();

(function() {
159 160 161 162 163 164 165 166 167
var result = 0;
var lazyDeopt = function(deopt) {
  var sum = function(v, i, o) {
    result += i;
    if (i == 13 && deopt) {
      %DeoptimizeNow();
      gc();
      gc();
      gc();
168
    }
169 170 171 172 173 174 175 176 177 178 179
  };
  c.forEach(sum);
};
%PrepareFunctionForOptimization(lazyDeopt);
lazyDeopt();
lazyDeopt();
%OptimizeFunctionOnNextCall(lazyDeopt);
lazyDeopt();
lazyDeopt(true);
lazyDeopt();
assertEquals(1500, result);
180 181 182 183 184
})();

// Call to a.forEach is done inside a try-catch block and the callback function
// being called actually throws.
(function() {
185 186 187 188 189 190 191
var caught = false;
var result = 0;
var lazyDeopt = function(deopt) {
  var sum = function(v, i, o) {
    result += i;
    if (i == 1 && deopt) {
      throw 'a';
192
    }
193 194 195 196 197
  };
  try {
    c.forEach(sum);
  } catch (e) {
    caught = true;
198
  }
199 200 201 202 203 204 205 206 207
};
%PrepareFunctionForOptimization(lazyDeopt);
lazyDeopt();
lazyDeopt();
%OptimizeFunctionOnNextCall(lazyDeopt);
lazyDeopt();
assertDoesNotThrow(lazyDeopt.bind(this, true));
assertTrue(caught);
lazyDeopt();
208 209 210 211 212
})();

// Call to a.forEach is done inside a try-catch block and the callback function
// being called actually throws, but the callback is not inlined.
(function() {
213 214 215 216 217 218 219
var caught = false;
var result = 0;
var lazyDeopt = function(deopt) {
  var sum = function(v, i, o) {
    result += i;
    if (i == 1 && deopt) {
      throw 'a';
220
    }
221 222 223 224 225 226
  };
  %NeverOptimizeFunction(sum);
  try {
    c.forEach(sum);
  } catch (e) {
    caught = true;
227
  }
228 229 230 231 232 233 234 235 236
};
%PrepareFunctionForOptimization(lazyDeopt);
lazyDeopt();
lazyDeopt();
%OptimizeFunctionOnNextCall(lazyDeopt);
lazyDeopt();
assertDoesNotThrow(lazyDeopt.bind(this, true));
assertTrue(caught);
lazyDeopt();
237 238
})();

239 240 241
// Call to a.forEach is done inside a try-catch block and the callback function
// being called throws into a deoptimized caller function.
(function TestThrowIntoDeoptimizedOuter() {
242
  var a = [1, 2, 3, 4];
243
  var lazyDeopt = function(deopt) {
244
    var sum = function(v, i, o) {
245 246 247 248 249 250 251 252 253 254 255
      result += v;
      if (i == 1 && deopt) {
        %DeoptimizeFunction(lazyDeopt);
        throw "some exception";
      }
    };
    %NeverOptimizeFunction(sum);
    var result = 0;
    try {
      a.forEach(sum);
    } catch (e) {
256
      assertEquals('some exception', e);
257 258 259
      result += 100;
    }
    return result;
260 261 262
  };
  ;
  %PrepareFunctionForOptimization(lazyDeopt);
263 264 265 266 267 268 269 270 271
  assertEquals(10, lazyDeopt(false));
  assertEquals(10, lazyDeopt(false));
  assertEquals(103, lazyDeopt(true));
  assertEquals(103, lazyDeopt(true));
  %OptimizeFunctionOnNextCall(lazyDeopt);
  assertEquals(10, lazyDeopt(false));
  assertEquals(103, lazyDeopt(true));
})();

272
(function() {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
var re = /Array\.forEach/;
var lazyDeopt = function foobar(deopt) {
  var b = [1, 2, 3];
  var result = 0;
  var sum = function(v, i, o) {
    result += v;
    if (i == 1) {
      var e = new Error();
      assertTrue(re.exec(e.stack) !== null);
    }
  };
  var o = [1, 2, 3];
  b.forEach(sum);
};
%PrepareFunctionForOptimization(lazyDeopt);
lazyDeopt();
lazyDeopt();
%OptimizeFunctionOnNextCall(lazyDeopt);
lazyDeopt();
292 293 294
})();

(function() {
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
var re = /Array\.forEach/;
var lazyDeopt = function(deopt) {
  var b = [1, 2, 3];
  var result = 0;
  var sum = function(v, i, o) {
    result += v;
    if (i == 1) {
      var e = new Error();
      assertTrue(re.exec(e.stack) !== null);
    }
  };
  %NeverOptimizeFunction(sum);
  var o = [1, 2, 3];
  b.forEach(sum);
};
%PrepareFunctionForOptimization(lazyDeopt);
lazyDeopt();
lazyDeopt();
%OptimizeFunctionOnNextCall(lazyDeopt);
lazyDeopt();
315 316 317
})();

(function() {
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
var re = /Array\.forEach/;
var lazyDeopt = function(deopt) {
  var b = [1, 2, 3];
  var result = 0;
  var sum = function(v, i, o) {
    result += v;
    if (i == 1) {
      %DeoptimizeNow();
    } else if (i == 2) {
      var e = new Error();
      assertTrue(re.exec(e.stack) !== null);
    }
  };
  var o = [1, 2, 3];
  b.forEach(sum);
};
%PrepareFunctionForOptimization(lazyDeopt);
lazyDeopt();
lazyDeopt();
%OptimizeFunctionOnNextCall(lazyDeopt);
lazyDeopt();
339 340 341
})();

(function() {
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
var re = /Array\.forEach/;
var a = [1, 2, 3];
var result = 0;
var lazyDeopt = function() {
  var sum = function(v, i, o) {
    result += i;
    if (i == 1) {
      %DeoptimizeFunction(lazyDeopt);
      throw new Error();
    }
  };
  a.forEach(sum);
};
%PrepareFunctionForOptimization(lazyDeopt);
assertThrows(() => lazyDeopt());
assertThrows(() => lazyDeopt());
try {
  lazyDeopt();
} catch (e) {
  assertTrue(re.exec(e.stack) !== null);
}
%OptimizeFunctionOnNextCall(lazyDeopt);
try {
  lazyDeopt();
} catch (e) {
  assertTrue(re.exec(e.stack) !== null);
}
369
})();
370 371 372 373 374 375 376 377 378 379

// Verify holes are skipped.
(() => {
  const a = [1, 2, , 3, 4];
  function withHoles() {
    const callback_values = [];
    a.forEach(v => {
      callback_values.push(v);
    });
    return callback_values;
380 381
  };
  %PrepareFunctionForOptimization(withHoles);
382 383 384 385 386 387 388 389 390 391 392 393 394 395
  withHoles();
  withHoles();
  %OptimizeFunctionOnNextCall(withHoles);
  assertArrayEquals([1, 2, 3, 4], withHoles());
})();

(() => {
  const a = [1.5, 2.5, , 3.5, 4.5];
  function withHoles() {
    const callback_values = [];
    a.forEach(v => {
      callback_values.push(v);
    });
    return callback_values;
396 397
  };
  %PrepareFunctionForOptimization(withHoles);
398 399 400 401 402
  withHoles();
  withHoles();
  %OptimizeFunctionOnNextCall(withHoles);
  assertArrayEquals([1.5, 2.5, 3.5, 4.5], withHoles());
})();
403 404 405

// Ensure that we handle side-effects between load and call.
(() => {
406 407 408 409
  function side_effect(a, b) {
    if (b) a.foo = 3;
    return a;
  }
410 411 412 413 414
  %NeverOptimizeFunction(side_effect);

  function unreliable(a, b) {
    let sum = 0;
    return a.forEach(x => sum += x, side_effect(a, b));
415 416
  };
  %PrepareFunctionForOptimization(unreliable);
417 418 419 420 421 422 423 424
  let a = [1, 2, 3];
  unreliable(a, false);
  unreliable(a, false);
  %OptimizeFunctionOnNextCall(unreliable);
  unreliable(a, false);
  // Now actually do change the map.
  unreliable(a, true);
})();