jerboa icon indicating copy to clipboard operation
jerboa copied to clipboard

Reduce stuttering in post listings

Open pipe01 opened this issue 2 years ago • 8 comments

I've managed to alleviate the constant stuttering every time a new post listing enters the screen by grouping a few posts into groups. This way the LazyColumn only has to create and destroy components much less often, reducing the feeling of stuttering.

This of course means that, whenever a new group must be created or removed, the stutter is more noticeable. However, this stutter doesn't seem to scale linearly with the amount of items per chunk and it's only slightly more noticeable than the stutter caused by every single item, so I think the trade is worth it. If we decide that this is a good way to do it, it should be possible to make the same improvements to the comment nodes.

The current chunk size is set to 10 as a completely arbitrary number, we could play around with it to see what works best or even set it to the amount of items returned in a single post fetch call.

When/if #411 is implemented we could probably get away with grouping an entire page into a single chunk, making the experience completely stutter free.

I have also disabled the home top bar coming in and out as you scroll because it was a huge cause of lag, however this was only for testing purposes and I imagine we might wanna re-enable it later.

Fixes #445 somewhat.

pipe01 avatar Jun 09 '23 18:06 pipe01

We desperately need an android / jetpack-compose performance expert on this. I'm 99% sure we're missing something trivial in either the code or our release compilation, that would make these hacks unecessary.

dessalines avatar Jun 09 '23 19:06 dessalines

Yeah I don't know, I've seen tons of posts online about bad LazyColumn performance it could definitely be Jetpack Compose's fault. Or maybe the issue is that recomposition of a single PostListing component takes too long?

pipe01 avatar Jun 09 '23 20:06 pipe01

Yep, its very possible that the PostListing component is the issue, although comment scrolling feels similarly stuttered.

I don't see why it would be getting recomposed tho. When I did try to optimize things months ago, adding keys and such to lazylist, it didn't really improve anything, and I wasn't smart enough to be able to use the info from that android studio recomposition count view.

dessalines avatar Jun 09 '23 20:06 dessalines

I think it does recompose since it needs to know what has changed, which is only possible by calling the composable function again. It's only after calling this method that the LazyColumn is able to reuse some other composable's result, at least that's how I see it. If this is right, the issue would be that the PostListing function call takes too long on the main thread, causing the stutter.

pipe01 avatar Jun 09 '23 23:06 pipe01

It does seem like when a deadline is missed, the main time hog is the PostListing composable:

image

pipe01 avatar Jun 10 '23 00:06 pipe01

What's that tool you're using? If I could figure out how to read that I could help a bit. Looks like the CircularIcon might be the issue?

dessalines avatar Jun 10 '23 01:06 dessalines

@dessalines I may have found the tool he was using.

Go to https://medium.com/icerock/optimize-or-die-profiling-and-optimization-in-jetpack-compose-a165c8897b3f

and cntrl + f for In Threads, you can click a method to search for it across the entire recorded interval and see how many times the thread was called and the average time spent for each call.

MrBruz avatar Jun 10 '23 05:06 MrBruz

I followed this guide.

Looks like the CircularIcon might be the issue?

It does look like that but the graph doesn't make much sense. For example, it's implying that CircularIcon is calling PostFooterLine, so I think something is not quite right.

pipe01 avatar Jun 10 '23 11:06 pipe01

Stale PR. I can re-open if necessary.

dessalines avatar Sep 26 '23 01:09 dessalines