New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polymorphic vs monomorphic #9

Closed
mcollina opened this Issue Jul 29, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@mcollina
Collaborator

mcollina commented Jul 29, 2017

Jakob Kummerow wrote:

I can't reproduce (or explain) your results for "polymorphic functions" with a plain d8 binary. While it always has been and always will be true that polymorphism has a speed cost, it doesn't make sense that a single "obj.length" load amid all the other stuff (creating objects, branching, calling, looping) would cause a 2x slowdown overall, because all the other code (like the empty loop) is not affected by that one polymorphic property load.

How can we update the benchmark in a way that is meaningful? cc @bmeurer @fhinkel @hashseed @TimothyGu

@jakobkummerow

This comment has been minimized.

Show comment
Hide comment
@jakobkummerow

jakobkummerow Jul 29, 2017

function polymorphic() {
  var objects = [{a:1}, {b:1, a:2}, {c:1, b:2, a:3}, {d:1, c:2, b:3, a:4}];
  var sum = 0;
  for (var i = 0; i < 10000; i++) {
    var o = objects[i & 3];
    sum += o.a;
  }
  return sum;
}

function monomorphic() {
  var objects = [{a:1}, {a:2}, {a:3}, {a:4}];
  var sum = 0;
  for (var i = 0; i < 10000; i++) {
    var o = objects[i & 3];
    sum += o.a;
  }
  return sum;
}

(only var objects is different between the two)

The higher the degree of polymorphism, the bigger the performance cost. For two different shapes, I'm seeing about 10-30% more time in the polymorphic compared to the monomorphic version; for four different shapes the delta goes up to 60-100% (it's pretty flaky in repeated runs).

Note that all these numbers are very specific to the situation you're testing. If you swap the order of properties in the object -- it makes a difference. If some of the object shapes show up more frequently than others -- it makes a difference. For polymorphism degrees greater than four, even what the rest of your program is doing makes a (possibly huge!) difference. It's extremely difficult to draw simple, generally-true conclusions from this, or give general advice. Polymorphism can be anything from "nothing to worry about" to "the single biggest performance problem with this app".

jakobkummerow commented Jul 29, 2017

function polymorphic() {
  var objects = [{a:1}, {b:1, a:2}, {c:1, b:2, a:3}, {d:1, c:2, b:3, a:4}];
  var sum = 0;
  for (var i = 0; i < 10000; i++) {
    var o = objects[i & 3];
    sum += o.a;
  }
  return sum;
}

function monomorphic() {
  var objects = [{a:1}, {a:2}, {a:3}, {a:4}];
  var sum = 0;
  for (var i = 0; i < 10000; i++) {
    var o = objects[i & 3];
    sum += o.a;
  }
  return sum;
}

(only var objects is different between the two)

The higher the degree of polymorphism, the bigger the performance cost. For two different shapes, I'm seeing about 10-30% more time in the polymorphic compared to the monomorphic version; for four different shapes the delta goes up to 60-100% (it's pretty flaky in repeated runs).

Note that all these numbers are very specific to the situation you're testing. If you swap the order of properties in the object -- it makes a difference. If some of the object shapes show up more frequently than others -- it makes a difference. For polymorphism degrees greater than four, even what the rest of your program is doing makes a (possibly huge!) difference. It's extremely difficult to draw simple, generally-true conclusions from this, or give general advice. Polymorphism can be anything from "nothing to worry about" to "the single biggest performance problem with this app".

@davidmarkclements

This comment has been minimized.

Show comment
Hide comment
@davidmarkclements

davidmarkclements Jul 29, 2017

Owner

Awesome thanks @jakobkummerow

I think the generalism we can make is stay away from polymorphism in hot paths because it could trigger a significant performance issue

Owner

davidmarkclements commented Jul 29, 2017

Awesome thanks @jakobkummerow

I think the generalism we can make is stay away from polymorphism in hot paths because it could trigger a significant performance issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment