optimized-foreach-polymorph.js 2.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// 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

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 = [{}, {}];
var c = [,,,,,2,3,4];
var d = [0.5,3,4];
var e = [,,,,0.5,3,4];

// Make sure that calls to forEach handle a certain degree of polymorphism (no
// hole check)
(function() {
  var result = 0;
  var polymorph1 = function(arg) {
    var sum = function(v,i,o) {
      result += i;
    }
    arg.forEach(sum);
22 23
  };
  %PrepareFunctionForOptimization(polymorph1);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  polymorph1(a);
  polymorph1(a);
  polymorph1(b);
  polymorph1(a);
  polymorph1(a);
  %OptimizeFunctionOnNextCall(polymorph1);
  polymorph1(a);
  polymorph1(b);
  assertEquals(1757, result);
})();

// Make sure that calls to forEach handle a certain degree of polymorphism.
(function() {
  var result = 0;
  var polymorph1 = function(arg) {
    var sum = function(v,i,o) {
      result += i;
    }
    arg.forEach(sum);
43 44
  };
  %PrepareFunctionForOptimization(polymorph1);
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  polymorph1(a);
  polymorph1(a);
  polymorph1(b);
  polymorph1(a);
  polymorph1(c);
  polymorph1(a);
  %OptimizeFunctionOnNextCall(polymorph1);
  polymorph1(a);
  polymorph1(b);
  assertEquals(1775, result);
})();

// Make sure that calls to forEach with mixed object/double arrays don't inline
// forEach.
(function() {
  var result = 0;
  var polymorph1 = function(arg) {
    var sum = function(v,i,o) {
      result += i;
    }
    arg.forEach(sum);
66 67
  };
  %PrepareFunctionForOptimization(polymorph1);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  polymorph1(a);
  polymorph1(a);
  polymorph1(b);
  polymorph1(a);
  polymorph1(d);
  polymorph1(a);
  %OptimizeFunctionOnNextCall(polymorph1);
  polymorph1(a);
  polymorph1(b);
  assertEquals(1760, result);
})();

// Make sure that calls to forEach with double arrays get the right result
(function() {
  var result = 0;
  var polymorph1 = function(arg) {
    var sum = function(v,i,o) {
      result += v;
    }
    arg.forEach(sum);
88 89
  };
  %PrepareFunctionForOptimization(polymorph1);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  polymorph1(d);
  polymorph1(d);
  polymorph1(d);
  %OptimizeFunctionOnNextCall(polymorph1);
  polymorph1(d);
  polymorph1(d);
  assertEquals(37.5, result);
})();

// Make sure that calls to forEach with mixed double arrays get the right result
(function() {
  var result = 0;
  var polymorph1 = function(arg) {
    var sum = function(v,i,o) {
      result += v;
    }
    arg.forEach(sum);
107 108
  };
  %PrepareFunctionForOptimization(polymorph1);
109 110 111 112 113 114 115 116
  polymorph1(d);
  polymorph1(e);
  polymorph1(d);
  %OptimizeFunctionOnNextCall(polymorph1);
  polymorph1(d);
  polymorph1(e);
  assertEquals(37.5, result);
})();