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

Make JSCRuntime::createValue 35% faster #27016

Closed
wants to merge 2 commits into from
Closed

Conversation

radex
Copy link
Contributor

@radex radex commented Oct 26, 2019

Summary

JSC does some sort of thread safety locking on every single JSC API call, which makes them ridiculously expensive for a large number of calls (such as when passing a large array over the RN bridge). It would be great if we could lock and unlock once for an entire sequence of JSC calls… but in the meantime, the less we call JSC the better.

unknown

In my benchmark environment (Nozbe/WatermelonDB#541), the time spent in JSCRuntime::createValue went down from 1.07s to 0.69s by changing JSValueIsXXXX calls to a single JSValueGetType call.

The underlying implementation does the same thing: https://github.com/WebKit/webkit/blob/master/Source/JavaScriptCore/API/JSValueRef.cpp#L58

Changelog

[General] [Fixed] - Make JSCRuntime::createValue faster

Test Plan

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 26, 2019
@radex
Copy link
Contributor Author

radex commented Oct 26, 2019

Doh, I accidentally broke Symbols.

btw. @mhorowitz -- dcc40a6#diff-44cf263c203a5949e25721f41394e9aeR418 — I haven't tested yet, but I think this was broken already on iOS 13 / macOS Catalina, and community Android builds of JSC, where there is a kJSTypeSymbol:

WebKit/WebKit@76b4e1b#diff-7cc5e4afb4c7092e07c92db27a5c328eR89-R90

}
// case kJSTypeSymbol:
default:
// WHAT ARE YOU
Copy link
Contributor

Choose a reason for hiding this comment

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

This is neat! But do you need to check smellsLikeES6Symbol here?

Copy link
Contributor

Choose a reason for hiding this comment

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

There are tests for this in https://github.com/facebook/react-native/blob/master/ReactCommon/jsi/jsi/test/testlib.cpp#L1160 but I am not sure if they actually run in CI. But you should be able to compile/run them by hand.

If we want RN to build and pass these tests against JSC libraries with and without this symbol, it may be tricky. Under Xcode, there are macros to test library versions, but I don't know if that works when using community JSC on Android.


JSType type = JSValueGetType(ctx, ref);

if (type == /* kJSTypeSymbol */ 6) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this should work, no?

@radex
Copy link
Contributor Author

radex commented Nov 4, 2019

@jacobp100 @mhorowitz fixed!

@radex
Copy link
Contributor Author

radex commented Nov 14, 2019

@mhorowitz any update? I've been running this patch in production for a week now, haven't seen any issues.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@mhorowitz has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@radex
Copy link
Contributor Author

radex commented Feb 19, 2020

@mhorowitz what's holding up this PR?

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @radex in 24e0bad.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Feb 20, 2020
@radex radex deleted the patch-2 branch February 20, 2020 18:36
osdnk pushed a commit to osdnk/react-native that referenced this pull request Mar 9, 2020
Summary:
JSC does some sort of thread safety locking on every single JSC API call, which makes them ridiculously expensive for a large number of calls (such as when passing a large array over the RN bridge). It would be great if we could lock and unlock once for an entire sequence of JSC calls… but in the meantime, the less we call JSC the better.

![unknown](https://user-images.githubusercontent.com/183747/67624956-08bd6e00-f838-11e9-8f65-e077693f113d.png)

In my benchmark environment (Nozbe/WatermelonDB#541), the time spent in JSCRuntime::createValue went down from 1.07s to 0.69s by changing JSValueIsXXXX calls to a single JSValueGetType call.

The underlying implementation does the same thing: https://github.com/WebKit/webkit/blob/master/Source/JavaScriptCore/API/JSValueRef.cpp#L58

## Changelog

[General] [Fixed] - Make JSCRuntime::createValue faster
Pull Request resolved: facebook#27016

Reviewed By: RSNara

Differential Revision: D18769047

Pulled By: mhorowitz

fbshipit-source-id: 9d1ee28840303f7721e065c1b3c347e354cd7fef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants