react-native-skia icon indicating copy to clipboard operation
react-native-skia copied to clipboard

[Android] Performance regression on JS thread when applying transformation on UI

Open orca-adria opened this issue 3 years ago • 15 comments

Description

Hello,

I'm doing a migration from Skia values to reanimated shared values, with the goal of freeing up the JS thread and performing all computations in UI. So far so good in terms of API, but I found a strange behaviour when trying to apply a transformation to a group using a derived value.

See the code example below, where I calculate the derived value in the first one, but I'm not applying it to the group, and the only difference with the second one is that I'm actually applying it. As far as I understood, this should only affect the UI thread, but I see a significant regression in fps in both threads, especially on the JS one. See also two screenshots of the performance monitor before and after the change.

Am I doing something wrong in the implementation? Or can this be an internal bug? I can provide code for reproducing it if necessary. Keep in mind that the component where this code is from is rendered 10 times in 10 different canvas.

Thanks a lot for your work in this amazing library!

Version

0.1.210

Steps to reproduce

Apply transformation to a group using a derived value. I can provide code examples to reproduce.

Snack, code example, screenshot, or link to a repository

[... other code]

  const transform = useDerivedValue(
    () => [{translateX: progress.value}],
    [progress],
  );

  return (
    <Group> // -> Without applying transformation
      <Path path={gradientPath} style="fill">
        <CornerPathEffect r={16} />
        <LinearGradient
          start={vec(size.width, 0)}
          end={vec(size.width, height * 1.1)}
          colors={colors}
        />
      </Path>
      <Path
        path={linePathData}
        style="stroke"
        strokeWidth={height * STROKE_WIDTH_RATIO}
        color={Color(tintColor).alpha(0.6).toString()}
      >
        <CornerPathEffect r={16} />
      </Path>
    </Group>
  );
[... other code]

  const transform = useDerivedValue(
    () => [{translateX: progress.value}],
    [progress],
  );

  return (
    <Group transform={transform}> // -> Applying transformation
      <Path path={gradientPath} style="fill">
        <CornerPathEffect r={16} />
        <LinearGradient
          start={vec(size.width, 0)}
          end={vec(size.width, height * 1.1)}
          colors={colors}
        />
      </Path>
      <Path
        path={linePathData}
        style="stroke"
        strokeWidth={height * STROKE_WIDTH_RATIO}
        color={Color(tintColor).alpha(0.6).toString()}
      >
        <CornerPathEffect r={16} />
      </Path>
    </Group>
  );

orca-adria avatar Oct 03 '23 17:10 orca-adria

A small reproducible example we be nice. We have an example that animates on the transform property of a group and we didn't notice any performance issues/regressions (didn't try on Android yet).

As a test, what happens if you use Matrix instead of transform?

const matrix = useDerivedValue(() => {
  const m3 = Skia.Matrix();
  m3.translate(progress.value, 0);
  return m3;
});
return (
  <Group matrix={matrix}>
  ...

Just curious to see how that example behaves (since we use also advanced matrix examples with reanimated v3 both on Android and iOS that work well).

wcandillon avatar Oct 03 '23 17:10 wcandillon

Just tried with matrix, and looks similar to transform. This is quite a heavy test, as there are many parallel elements going on and probably no user will have the setup I'm testing, so I expected a low fps. What surprises me, tho, is that JS is also heavily affected.

Will prepare an example tomorrow. Thanks!

orca-adria avatar Oct 03 '23 18:10 orca-adria

Hi @wcandillon, I just updated the repo I shared with you some months ago. You can see how the JS thread significantly decreases fps when applying transform/matrix to the group (line 145 in widgets/components/Timeplot.tsx)

I've tested it on iOS, and looks more like an Android-only issue... on iOS there is some drop in the UI thread (expected for 46 simultaneous animated timeplots), but the JS thread remains intact at 60fps. However, Android has an even more significant drop in JS.

I hope this helps, let me know if you need anything else.

See the comparison after applying animation. Before both were at 60-60: iOS -> UI: ~35 | JS:60 Android -> UI: ~30 | JS: ~20

orca-adria avatar Oct 04 '23 11:10 orca-adria

Thanks a lot, currently looking into it, will ping you offline.

wcandillon avatar Oct 04 '23 11:10 wcandillon

I can reproduce the dramatic frame drop by animating on the group. Currently investigating. It might be difficult to build a reproducible example. I'm a bit baffled at the different of behaviour when removing the top-level transform: why does the plot still move forward? (but jerky)

The logic seems extremely complex for such a small plot, still I agree that there is something interesting too look at here. I am also in parallel migrating the examples from our example app and will be on the hunt for such quirks.

wcandillon avatar Oct 04 '23 11:10 wcandillon

In the example the logic looks a bit too complex, but we need it for other usages, as this is quite a flexible component that refreshes in real time.

You can see it moving because we ingest a new value every second, and the graph is recreated. The goal of the animation is to smooth these gaps - see useAnimatedReaction callback in line 97

orca-adria avatar Oct 04 '23 11:10 orca-adria

I suspect the issue may come from useAnimatedReaction, what where you using there because the reanimated migration? Do you still have this version of the code that you could share privately?

have you looked at https://docs.swmansion.com/react-native-reanimated/docs/advanced/useFrameCallback/ instead?

@kmagiera is is possible that useAnimatedReaction would engage with the JS Thread in some way? Why do we see a drop on the js-thread could it be that a slow ui-thread causes a frame drop on the js thread as well?

wcandillon avatar Oct 04 '23 11:10 wcandillon

Before the migration I was using a useEffect, You can have a look here, it was even more complex, as was inherited from a previous implementation.

useAnimatedReaction is still computing when the transformation is not applied, and fps looks fine then. Why should it behave differently when we apply the transformation to the group?

Will have a look at useFrameCallback now.

orca-adria avatar Oct 04 '23 13:10 orca-adria

Tested with useFrameCallback and got similar results in terms of performace if I just move all my logic inside this if statement:

  const graphLoop = useFrameCallback(({timestamp}) => {
    if (timestamp - lastTimestamp.value >= timeUnit.second) {
      lastTimestamp.value = timestamp;
      
      . . .

However, if I try to manually compute the chart offset and create a new matrix on every frame it is much slower. The code looks cleaner tho, so I'll keep this hybrid version :)

orca-adria avatar Oct 04 '23 14:10 orca-adria

I'm closing this for now, as I've been doing further tests and it looks like it is working as expected in terms of performance.

See these two screenshots doing the same comparison (matrix applied / not applied), here JS thread looks fine and is not affected by the transformation.

Then I guess there is some bug from the react native performance monitor on Android?

Screenshot 2023-10-09 at 12 28 07 Screenshot 2023-10-09 at 12 19 14

orca-adria avatar Oct 09 '23 11:10 orca-adria

We are seeing similar issues when applying a matrix to a group that contains many paths on Android. Wondering if we could open this back up to continue investigation?

iOS appears to work great, but Android FPS drops to 10-16 FPS when panning and applying the matrix.

edsztukowski avatar Oct 11 '23 20:10 edsztukowski

Yes, I am eager to investigate this further. @edsztukowski any small examples you could share with us?

wcandillon avatar Oct 12 '23 03:10 wcandillon

@orca-adria I was able to reproduce that the framerate when using Skia value was completely bogus. The performance monitor on Android is not necessarily very precise (there are cases where it doesn't seem to give the right value). However when using the Skia values, we were tricking the perf monitor completely: instead of dropping frames we would just play the next frame even though its rendering time was way more than a frame.

We've been doing a lot of benchmarking this week and we're quite excited about our performance roadmap.

@edsztukowski let us know if there is an example you would like us to look at.

wcandillon avatar Nov 05 '23 09:11 wcandillon

Yes, I just finished the migration of our app and the performance improvements are impressive! So congrats and thank you @wcandillon , @chrfalch and all contributors!

See this as an example: Screenshot 2023-11-06 at 11 46 55

I have some before/after videos that I can share if you are interested

orca-adria avatar Nov 06 '23 10:11 orca-adria

This great to hear :) We wrote a little note about performance when animating with Reanimated: https://shopify.github.io/react-native-skia/docs/animations/animations#performance The main idea is not do any JSI allocation when animating. And we think/hope that we provides the means to do that.

We also mentioned textures which can help with repeating paths/patterns: https://shopify.github.io/react-native-skia/docs/animations/textures (these should be extremely fast).

We are working on some substantial performance improvements, one of which should come out within a few weeks. Thank you for your trust and support and stay tuned :)

wcandillon avatar Nov 06 '23 11:11 wcandillon