I've run some more tests on Pager performance and ...
# compose
m
I've run some more tests on Pager performance and if I haven't messed up the test itself, it seems that original ViewPager is still quite a bit ahead when it comes to performance (which is understandable given how young it is, but still, good to know). More details in thread.
💪 2
Copy code
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent { PagerPerformanceExample() }
    }

}

@OptIn(ExperimentalPagerApi::class)
@Composable
fun PagerPerformanceExample(){

    val pages = 50
    val viewsOnPage = 50

    Column(modifier = Modifier.fillMaxSize()) {

        HorizontalPager(
            state = rememberPagerState(pageCount = pages),
            modifier = Modifier.fillMaxWidth().weight(1f).background(color = Color(0xFFEEEEFF))
        ) { pageNo ->
            Column(
                modifier = Modifier.fillMaxSize()
            ) {
                repeat(viewsOnPage) { Text("\uD83E\uDD16") }
            }
        }

        AndroidView(
            modifier = Modifier.fillMaxWidth().weight(1f).background(color = Color(0xFFFFEEEE)),
            factory = { context ->
                ViewPager(context).apply {
                    val matchParentParams = LayoutParams(MATCH_PARENT, MATCH_PARENT)
                    layoutParams = matchParentParams
                    adapter = object : PagerAdapter () {

                        override fun getCount() = pages

                        override fun isViewFromObject(view: View, obj: Any): Boolean {
                            return view == obj
                        }

                        override fun instantiateItem(container: ViewGroup, position: Int): Any {
                            val pageLayout = LinearLayout(context).apply {
                                orientation = LinearLayout.VERTICAL
                                layoutParams = matchParentParams
                                gravity = Gravity.CENTER_HORIZONTAL
                            }
                            repeat(viewsOnPage) {
                                pageLayout.addView(TextView(context).apply { text = "\uD83E\uDD16" })
                            }
                            container.addView(pageLayout)
                            return pageLayout
                        }

                        override fun destroyItem(
                            container: ViewGroup,
                            position: Int,
                            view: Any
                        ) {
                            container.removeView(view as View)
                        }

                    }
                }
            }
        )

    }

}
Testing this on a mid-range (I think?) Redmi 5 Plus.
This is how they compare after the app has settled down a bit (after a minute or so of interactions to rule out the initial overhead on initial compose optimisation). Blueish is compose Pager, reddish is ViewPager. Compose is noticeably less smooth which is confirmed by the gpu profile, I think.
cold version looks a lot worse, but afaiu it's expected so I won't post video unless someone wants it
@cb when you're here, let me know if it's something that should be filed on github issue tracker or not necessarily ;)
n
did you run the sample in release or debug ? with R8 enabled ?
c
m
release with R8 (learned my lesson the last time ;))
👍 1
Wait, @cb do you say that you will be adding the related bug or you want me to do it? You linked to my other issue report
c
As I said in the bug(s), I think this is a general Compose performance issue rather than something unique to Pager. Let’s stick to the Compose issue for now
m
this is something a bit different, a general performance issue rather than the fling calculation. But I can see that somebody responded on the original issue and mentioned the perf as well. So like you say, let's leave it here 🙂