• Benedikt Meurer's avatar
    [turbofan] Optimize O.p.hasOwnProperty inside for-in. · 06753c64
    Benedikt Meurer authored
    Optimize the common pattern
    
      for (var i in o) {
        if (Object.prototype.hasOwnProperty.call(o, i)) {
          // do something
        }
      }
    
    which is part of the guard-for-in style in ESLint (see the documentation
    at https://eslint.org/docs/rules/guard-for-in for details). This pattern
    also shows up in React and Ember applications quite a lot (and is tested
    by the appropriate Speedometer benchmarks, although not dominating those
    benchmarks, since they spent a lot of time in non-TurboFan'ed code).
    
    This improves the forInHasOwnProperty and forInHasOwnPropertySafe micro-
    benchmarks in v8:6702, which look like this
    
      function forInHasOwnProperty(o) {
        var result = 0;
        for (var i in o) {
          if (o.hasOwnProperty(i)) {
            result += 1;
          }
        }
        return result;
      }
    
      function forInHasOwnPropertySafe(o) {
        var result = 0;
        for (var i in o) {
          if (Object.prototype.hasOwnProperty.call(o, i)) {
            result += 1;
          }
        }
        return result;
      }
    
    by around 4x and allows for additional optimizations in the future, by
    also elimiating the megamorphic load when accessing the enumerated
    properties.
    
    This changes the interpreter ForInNext bytecode to collect more precise
    feedback about the for-in state, which now consists of three individual
    states: UNINITIALIZED, MEGAMORPHIC and GENERIC. The MEGAMORPHIC state
    means that the ForInNext has only seen objects with a usable enum cache
    thus far, whereas GENERIC means that we have seen some slow-mode for..in
    objects as well.
    
    R=jarin@chromium.org
    
    Bug: v8:6702
    Change-Id: Ibcd75ea9b58c3b4f9219f11bc37eb04a2b985604
    Reviewed-on: https://chromium-review.googlesource.com/636964
    Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
    Reviewed-by: 's avatarJaroslav Sevcik <jarin@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#47632}
    06753c64
builtins-definitions.h 88.3 KB