oleksandrbalan / programguide

Lazy layout to display program guide data on the two directional plane.
Apache License 2.0
39 stars 7 forks source link

Android TV performance question #12

Open jfayz opened 8 months ago

jfayz commented 8 months ago

I've been testing out the demo on an Android TV box and ran into some performance issues. The program guide is pretty laggy – animations are jerky, and it just doesn't feel responsive, especially considering there aren't many programs in the demo. I have been running release version, so it is not debug build slowness.

I tried to run it on emulator on my Mac and it still feels a bit jerky. However, it runs super smoothly on a Mac with desktopdemo. This makes me wonder if there's something specific with Android and/or animations or if is indeed resource-heavy? Are the plans for performance improvements?

rahat14 commented 7 months ago

@oleksandrbalan dude, I am also facing this issue. it would be grateful if you solve this issue also , i want to sponsor this library let me know.

oleksandrbalan commented 7 months ago

Implementation is build on LazyLayout, so it is basically limited by it's performance. When folks from Compose team improve the performance of the LazyLayout, the ProgramGuide will transitively benefit from it.

From my observations the most "heavy" part is a text rendering. So techinally you could try to do some tricks like render text with some delay, as LaunchedEffect works fine in cells.

    programs(
        // Count of programs
        count = ...,
        // Necessary layout info of the single program cell
        layoutInfo = { ProgramGuideItem.Program(...) }
    ) {
        var renderText by remember { mutableStateOf(false) }
        LaunchedEffect(Unit) {
            delay(300)
            renderText = true
        }
        if (renderText) {
            ...
        }
    }

It is a bit hacky, but could work 🙈

Eightyplus commented 7 months ago

Hi 👋 I use MinaBox for my Android TV program-guide and do not use this repository (programguide). And I have been tuning my use of MinaBox for some time without significant results.

But just something seems wrong on Android TV when navigating with the D-pad. However, my experience is that it has similar performance on Android TV as on mobile when using the mouse - this is possible in the emulator. So what is the actual difference? Does focus with d-pad change the actual rendered element / top level element or the like. When triggers the rendering of elements?

Illustrated with Googles recomposeHighlighter:

Using mouse Screenshot_1703070836

Using d-pad Screenshot_1703070845

Eightyplus commented 6 months ago

I finally managed to locate a bug in my code and the illustrated behavior above stopped.

I have some suggestions to enhance performance:

  1. use remember as suggested
  2. use derivedStateOf to avoid unnecessary recompositions
  3. use a ViewModel to do and keep calculations in memory
  4. cache position information in a hashmap

🚀

rahat14 commented 6 months ago

can you share sample code so that we can solve the performance issue. @Eightyplus

Eightyplus commented 6 months ago

A bit hard to share the exact improvement since I use MinaBox, but here is an example

val layoutInfos = remember {
     mutableMapOf<ProgramInfo, ProgramGuideItem.Program>()
}

fun ProgramInfo.layoutInfo(): ProgramGuideItem.Program {
    var layoutInfo = layoutInfos[this]
    if (layoutInfo == null) {
           layoutInfo = <do you calculations>
          layoutInfos[this] = layoutInfo
    }
    return layoutInfo
}

 programs(
        // Count of programs
        count = ...,
        // Necessary layout info of the single program cell
        layoutInfo = { it.layoutInfo() }
    ) {

    }
Eightyplus commented 6 months ago

@rahat14 , you can also share your code, then I can come with suggestions 😄

oxyroid commented 2 months ago

I guess you put all the programs as a list to minabox, consider using androidx-paging3.

Sample: https://github.com/oxyroid/M3UAndroid/blob/25e454045a82c46bf4611e9af51f8b5c10a9c9c4/features/stream/src/main/java/com/m3u/features/stream/components/ProgrammeGuide.kt#L151