Skip to content

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

Merged
benlesh merged 1 commit into
ReactiveX:masterfrom
bmeurer:perf_Subscription_add
Jan 29, 2019
Merged

benlesh merged 1 commit into
ReactiveX:masterfrom
bmeurer:perf_Subscription_add

Conversation

@bmeurer

@bmeurer bmeurer commented Jan 28, 2019 •

Copy link
Copy Markdown
Contributor

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

coveralls commented Jan 28, 2019 •

Copy link
Copy Markdown

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

@bmeurer
bmeurer force-pushed the perf_Subscription_add branch from 77c1016 to 20053d4 Compare January 28, 2019 15:08
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
@bmeurer
bmeurer force-pushed the perf_Subscription_add branch from 20053d4 to 03fa8e8 Compare January 28, 2019 15:12

@cartant cartant left a comment

Copy link
Copy Markdown
Collaborator

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

benlesh commented Jan 29, 2019

Copy link
Copy Markdown
Member

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