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

Android full CPU usage with simple repeat animation #6853

Open
kristian-nst opened this issue Dec 26, 2024 · 1 comment
Open

Android full CPU usage with simple repeat animation #6853

kristian-nst opened this issue Dec 26, 2024 · 1 comment
Labels
Platform: Android This issue is specific to Android Repro provided A reproduction with a snippet of code, snack or repo is provided

Comments

@kristian-nst
Copy link

kristian-nst commented Dec 26, 2024

Description

iOS is fine, no CPU spike.
Adding this component to a screen jumps the CPU from a normal/stable ~20% emulator cpu usage. As soon as I add this to, it jumps to 100% (no other code changes)
(Macbook Pro - M4 Max)

Screenshots

image

vs

image

Info

  • RN 0.76.3
  • Expo 52.0.17
  • Android Studio Ladybug | 2024.2.1 Patch 2
  • Android Emulator with Android 15 (API 35) - arm64
  • openjdk version "22.0.2" - Corretto 64bit
  • reanimated 3.16.1

Currently using newArchitecture=false

Steps to reproduce

// @refresh reset
import React, { memo, type ReactElement } from 'react'
import { StyleSheet, View } from 'react-native'
import Animated, {
  Easing,
  useAnimatedStyle,
  useSharedValue,
  withRepeat,
  withTiming,
} from 'react-native-reanimated'

import { Icon } from '~/components/atoms/Icon'

type TSpinnerProps = {
  size?: 'small' | 'large'
  variant?: 'primary' | 'secondary' | 'tertiary'
}

export const Spinner = memo(
  ({ size = 'small', variant = 'primary' }: TSpinnerProps): ReactElement => {
    const isSmall = size === 'small'

    const variantColors: Record<string, string> = {
      primary: '#C32000',
      secondary: '#FFFFFF',
      tertiary: '#344450',
    }

    const rotation = useSharedValue(0)

    rotation.value = withRepeat(
      withTiming(360, {
        duration: 600,
        easing: Easing.linear,
      }),
      -1,
    )

    const animatedStyle = useAnimatedStyle(() => {
      return {
        transform: [{ rotate: `${rotation.value}deg` }],
      }
    })

    return (
      <View style={[styles.container, isSmall ? styles.small : styles.large]}>
        <Animated.View style={[styles.spinner, animatedStyle]}>
          <Icon
            name="spinner"
            size={isSmall ? 24 : 64}
            fill={variantColors[variant]}
          />
        </Animated.View>
      </View>
    )
  },
)

const styles = StyleSheet.create({
  container: {
    justifyContent: 'center',
    alignItems: 'center',
  },
  small: {
    height: 24,
    width: 24,
  },
  large: {
    height: 64,
    width: 64,
  },
  spinner: {
    position: 'absolute',
  },
})

Snack or a link to a repository

https://snack.expo.dev/@kristian-nst/repeat-animation-cpu?platform=android

Reanimated version

3.16.1

React Native version

0.76.3

Platforms

Android

JavaScript runtime

Hermes

Workflow

None

Architecture

Paper (Old Architecture)

Build type

Debug app & dev bundle

Device

Android emulator

Device model

Pixel 8 Pro

Acknowledgements

Yes

@github-actions github-actions bot added Missing info The user didn't precise the problem enough Missing repro This issue need minimum repro scenario Platform: Android This issue is specific to Android Repro provided A reproduction with a snippet of code, snack or repo is provided and removed Missing info The user didn't precise the problem enough Missing repro This issue need minimum repro scenario labels Dec 26, 2024
@hirbod
Copy link
Contributor

hirbod commented Dec 26, 2024

Likely unrelated, but you should not read or write .value in render.
Move to useEffect. https://snack.expo.dev/@hirbod/repeat-animation-cpu?platform=android

Just a side note, likely not gonna change your findings, but maybe worth a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: Android This issue is specific to Android Repro provided A reproduction with a snippet of code, snack or repo is provided
Projects
None yet
Development

No branches or pull requests

2 participants