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

perf(Subscription): use instanceof to avoid megamorphic LoadIC #4499

Merged
merged 1 commit into from
Jan 29, 2019

Conversation

bmeurer
Copy link
Contributor

@bmeurer bmeurer commented Jan 28, 2019

In Subscription#add() use instanceof to test whether subscription
must be wrapped in a Subscription (aka for presence of _addParent
method), instead of typeof subscription._addParent === 'function',
as the latter is going to turn into a megamorphic property access
in any realistic application and cause quite a bit of contention on
the megamorphic stub cache in Node (aka V8).

The instanceof is definitely faster, even if the property access
would hit the megamorphic stub cache. For example in the case of a
simple Angular Universal prerender test, just changing this can
reduce the number of megamorphic stub cache misses by around 2%.

Even in the case where the megamorphic stub cache hits all the time,
using instanceof is still faster. I put together a little micro-
benchmark here. Running this with the latest V8 we get:

$ out/Release/d8 bench-instanceof-versus-typeof-megamorphic.js
instanceOf: 91 ms.
typeOf: 138 ms.

@coveralls
Copy link

coveralls commented Jan 28, 2019

Pull Request Test Coverage Report for Build 7965

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 96.813%

Totals Coverage Status
Change from base Build 7960: 0.0%
Covered Lines: 5256
Relevant Lines: 5429

💛 - Coveralls

In `Subscription#add()` use `instanceof` to test whether `subscription`
must be wrapped in a `Subscription` (aka for presence of `_addParent`
method), instead of `typeof subscription._addParent === 'function'`,
as the latter is going to turn into a *megamorphic property access*
in any realistic application and cause quite a bit of contention on
the megamorphic stub cache in Node (aka V8).

The `instanceof` is definitely faster, even if the property access
would hit the megamorphic stub cache. For example in the case of a
simple [Angular Universal prerender][1] test, just changing this can
reduce the number of megamorphic stub cache misses by around **2%**.

Even in the case where the megamorphic stub cache hits all the time,
using `instanceof` is still faster. I put together a little micro-
benchmark [here][2]. Running this with the latest V8 we get:

```
$ out/Release/d8 bench-instanceof-versus-typeof-megamorphic.js
instanceOf: 91 ms.
typeOf: 138 ms.
```

[1]: https://gist.githubusercontent.com/bmeurer/5b9480ef1a74c5187180193abc73dcd4/raw/fc5d9a01a6b7fe841efa1c7773ae55e70435d16a/prerender.js
[2]: https://gist.github.com/bmeurer/d6d8d4e5d3a9937499ed738c93b3b7cf
Copy link
Collaborator

@cartant cartant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@benlesh benlesh merged commit 065b4e3 into ReactiveX:master Jan 29, 2019
@benlesh
Copy link
Member

benlesh commented Jan 29, 2019

Thanks, @bmeurer!!!

@bmeurer bmeurer deleted the perf_Subscription_add branch January 30, 2019 06:37
@lock lock bot locked as resolved and limited conversation to collaborators Mar 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants