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

Use native jsi::Runtime::evaluateJavaScript instead of JS eval() thru global #1863

Closed
wants to merge 28 commits into from

Conversation

mrousavy
Copy link
Contributor

@mrousavy mrousavy commented Mar 23, 2021

Description

This PR refactors the worklet creation code to make it cleaner to read and a bit faster.

Changes

  • Replace the retrieval of global.eval(...), calling it, and getting the returned function with a simple Runtime::evaluateJavaScript(...). That's cleaner to read and by a few microseconds faster.

Benchmarks

The time (in microseconds) it takes to create a worklet jsi::Function from a string:

Before:

2021-03-18 09:14:12.946440+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 56ms
2021-03-18 09:14:12.947215+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 68ms
2021-03-18 09:14:12.947460+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 51ms
2021-03-18 09:14:12.947821+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 86ms
2021-03-18 09:14:12.947999+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 19ms
2021-03-18 09:14:12.948240+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 49ms
2021-03-18 09:14:12.948516+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 31ms
2021-03-18 09:14:12.948730+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 37ms
2021-03-18 09:14:12.949116+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 50ms
2021-03-18 09:14:12.949329+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 40ms
2021-03-18 09:14:12.949512+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 21ms
2021-03-18 09:14:12.949845+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 147ms
2021-03-18 09:14:12.950113+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 54ms
2021-03-18 09:14:12.950468+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 126ms
2021-03-18 09:14:12.950890+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 49ms
2021-03-18 09:14:12.951242+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 63ms
2021-03-18 09:14:12.951637+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 21ms
2021-03-18 09:14:12.951850+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 18ms
2021-03-18 09:14:12.952636+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 17ms
2021-03-18 09:14:12.952805+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 19ms
2021-03-18 09:14:12.952964+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 20ms
2021-03-18 09:14:12.953175+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 24ms
2021-03-18 09:14:12.953382+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 23ms
2021-03-18 09:14:12.953557+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 19ms
2021-03-18 09:14:12.953706+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 23ms
2021-03-18 09:14:12.954333+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 57ms
2021-03-18 09:14:12.954695+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 38ms
2021-03-18 09:14:12.954908+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 57ms
2021-03-18 09:14:12.955136+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 58ms
2021-03-18 09:14:12.955427+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 59ms
2021-03-18 09:14:12.955612+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 48ms
2021-03-18 09:14:12.955995+0100 ReanimatedExample[9344:189303] Evaluating Worklet took 20ms

After

2021-03-18 09:14:42.995224+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 54ms
2021-03-18 09:14:42.995998+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 59ms
2021-03-18 09:14:42.996217+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 42ms
2021-03-18 09:14:42.996516+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 46ms
2021-03-18 09:14:42.996696+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 17ms
2021-03-18 09:14:42.996898+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 43ms
2021-03-18 09:14:42.997110+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 16ms
2021-03-18 09:14:42.997349+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 41ms
2021-03-18 09:14:42.997713+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 43ms
2021-03-18 09:14:42.997960+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 39ms
2021-03-18 09:14:42.998144+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 15ms
2021-03-18 09:14:42.998422+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 99ms
2021-03-18 09:14:42.998655+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 49ms
2021-03-18 09:14:42.998978+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 117ms
2021-03-18 09:14:42.999374+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 52ms
2021-03-18 09:14:42.999786+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 35ms
2021-03-18 09:14:43.000217+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 17ms
2021-03-18 09:14:43.000452+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 18ms
2021-03-18 09:14:43.001388+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 15ms
2021-03-18 09:14:43.001571+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 15ms
2021-03-18 09:14:43.001734+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 17ms
2021-03-18 09:14:43.001910+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 17ms
2021-03-18 09:14:43.002085+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 18ms
2021-03-18 09:14:43.002272+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 16ms
2021-03-18 09:14:43.002458+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 20ms
2021-03-18 09:14:43.003233+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 61ms
2021-03-18 09:14:43.003608+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 35ms
2021-03-18 09:14:43.003878+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 63ms
2021-03-18 09:14:43.004151+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 56ms
2021-03-18 09:14:43.004401+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 54ms
2021-03-18 09:14:43.004579+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 40ms
2021-03-18 09:14:43.004937+0100 ReanimatedExample[9361:190498] Evaluating Worklet took 13ms

Worklet creation

I noticed that a lot of worklets are being created for just a simple useAnimatedStyle, aka this code runs very often:

auto codeBuffer = std::make_shared<const jsi::StringBuffer>("(" + ValueWrapper::asString(frozenObj->map["asString"]->valueContainer) + ")");
auto func = rt.evaluateJavaScript(codeBuffer, ValueWrapper::asString(frozenObj->map["__location"]->valueContainer)).asObject(rt).asFunction(rt);
worklets[workletHash] = std::make_shared<jsi::Function>(std::move(func));

I feel like there can be a few more memoizations done on the JS side to avoid re-creating the worklet when the worklet hash changes but the actual code stays the same - e.g. by correctly utilizing useMemo, useCallback, and it's dependencies. wdyt @piaskowyk ?

@mrousavy mrousavy mentioned this pull request Apr 12, 2021
4 tasks
@piaskowyk
Copy link
Member

Did you test it on an emulator or physical device?

@mrousavy
Copy link
Contributor Author

Emulator.

@piaskowyk
Copy link
Member

I used your changes here: #1879 so I'll close this PR, and these changes will be merged with #1879

@piaskowyk piaskowyk closed this May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants