Skip to content
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

Object constructor calls are slow #3629

Open
kunalspathak opened this issue Sep 1, 2017 · 4 comments
Open

Object constructor calls are slow #3629

kunalspathak opened this issue Sep 1, 2017 · 4 comments
Milestone

Comments

@kunalspathak
Copy link
Contributor

There was a discussion going on in webpack/webpack#5600 that wraps all references into Object () which was slow. v8 recently fixed it and I piggy-backed the test case from here that was used to demonstrate v8's behavior and verified for chakracore and the results are similar. We are slow too and will be good to optimize to get boost in webpack module.

const identity = x => x;

function callDirect(o) {
  const foo = o.foo;
  return foo(1, 2, 3);
}

function callViaCall(o) {
  return o.foo.call(undefined, 1, 2, 3);
}

function callViaObject(o) {
  return Object(o.foo)(1, 2, 3);
}

function callViaIdentity(o) {
  return identity(o.foo)(1, 2, 3);
}

var TESTS = [
    callDirect,
    callViaObject,
    callViaCall,
    callViaIdentity
];

class A { foo(x, y, z) { return x + y + z; } };
var o = new A;
var n = 1e8;

function test(fn) {
  var result;
  for (var i = 0; i < n; ++i) result = fn(o);
  return result;
}

// Warmup.
for (var j = 0; j < TESTS.length; ++j) {
  test(TESTS[j]);
}

// Measure.
for (var j = 0; j < TESTS.length; ++j) {
  var startTime = Date.now();
  test(TESTS[j]);
  console.log(TESTS[j].name + ':', (Date.now() - startTime), 'ms.');
}

Results :

E:\git\Chakra\core>node f:\temp\object.js
callDirect: 664 ms.
callViaObject: 1906 ms.
callViaCall: 329 ms.
callViaIdentity: 454 ms.

E:\git\Chakra\core>node -pe "process.versions"
{ http_parser: '2.7.0',
  node: '8.4.0',
  chakracore: '1.7.1.0',
  uv: '1.13.1',
  zlib: '1.2.11',
  ares: '1.10.1-DEV',
  modules: '57',
  nghttp2: '1.22.0',
  openssl: '1.0.2l',
  icu: '59.1',
  unicode: '9.0',
  cldr: '31.0.1',
  tz: '2017b' }
@obastemur
Copy link
Collaborator

callViaObject looks seriously slow. However not sure its popularity. Do you have v8 numbers?

@kunalspathak
Copy link
Contributor Author

Yes. Check the blog in the description of this issue.

@LouisLaf
Copy link
Collaborator

LouisLaf commented Sep 1, 2017

I tried playing with his code a bit:
Original:

Test Edge Chrome
callDirect 716 546
callViaObject 2156 1152
callViaCall 196 579
callViaIdentity 302 521

The reason for our poor callDirect score is that warmup wasn’t sufficient for us. Running the warmup twice, we get:

Test Edge Chrome
callDirect 198 545
callViaObject 2157 1154
callViaCall 190 582
callViaIdentity 304 519

I then used a switch to get rid of the polymorphic calls:

Test Edge Chrome
callDirect 159 43
callViaObject 1741 1309
callViaCall 168 718
callViaIdentity 281 667

Our numbers make sense here, but not sure what happened to Chrome…

We have some pretty easy optimizations (allowing non-fixed field inlining checks to be hoisted our of lops) that should speed up all of these significantly. Inlining Object() would certainly help the second case and we should look into it. It should be easy.

Firefox has pretty consistent results across all 3 runs:

Test FireFox
callDirect 199ms.
callViaObject 1505ms.
callViaCall 194ms.
callViaIdentity 192ms.

@bmeurer
Copy link

bmeurer commented Sep 2, 2017

Awesome.

Note however that the polymorphic call site inside test is there by design to make sure we measure the right thing, i.e. the cost of the actual functions under test and not our ability to inline everything into the driver and then hoist checks out of the loop.

@MikeHolman MikeHolman added this to the Backlog milestone May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants