Anki-Android icon indicating copy to clipboard operation
Anki-Android copied to clipboard

Improve card browser scrolling performance.

Open Galal-20 opened this issue 6 months ago • 7 comments

Purpose / Description

This PR addresses the jittery scrolling issue in the Card Browser. The performance degradation was primarily caused by frequent, synchronous JNI calls to the backend during onBindViewHolder and expensive UI operations like repeated background drawable updates and regex processing for every row being bound.

Fixes

  • Fixes #20994

Approach

To restore smooth scrolling, the following optimizations were implemented:

Backend Caching: Added an LruCache for BrowserRow data to minimize expensive JNI calls during scrolling.

  • ViewHolder Optimization: Cached UI states color, selection, truncation to skip redundant updates, particularly background/ripple creation.
  • Data Consistency: Implemented cache invalidation for all state changing operations marking, flags, search refreshes.

How Has This Been Tested?

  • Unit Tests: Executed CardBrowserTest and CardBrowserViewModelTest to ensure no regressions in browser logic.
  • Manual Verification: Verified the fix by simulating slow scrolling in the Card Browser with multiple columns enabled.
  • Android Version: Android 16 and 12.

https://github.com/user-attachments/assets/50d9a564-89c8-4f05-83fc-672d474d98db

Checklist

  • [x] You have a descriptive commit message with a short title.
  • [x] You have commented your code, particularly in hard-to-understand areas
  • [x] You have performed a self-review of your own code
  • [x] UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • [x] UI Changes: You have tested your change using the [Google Accessibility Scanner]

Galal-20 avatar May 17 '26 12:05 Galal-20

Unit test is failing

Alok-Silswal avatar May 21 '26 20:05 Alok-Silswal

I think the MacOs unit tests were flaky. Please rebase your branch and resolve the conflicts

DoomsCoder avatar May 22 '26 09:05 DoomsCoder

From what I understand of the issue, this was a bug in RecyclerFastScroller's logic when scrolling slowly.

The performance degradation was primarily caused by frequent, synchronous JNI calls to the backend during onBindViewHolder and expensive UI operations like repeated background drawable updates and regex processing for every row being bound.

Did you replicate this bug beforehand and afterwards, and prove that it's due to the JNI overhead?

david-allison avatar May 29 '26 11:05 david-allison

From what I understand of the issue, this was a bug in RecyclerFastScroller's logic when scrolling slowly.

The performance degradation was primarily caused by frequent, synchronous JNI calls to the backend during onBindViewHolder and expensive UI operations like repeated background drawable updates and regex processing for every row being bound.

Did you replicate this bug beforehand and afterwards, and prove that it's due to the JNI overhead?

You are absolutely right, The performance degradation was indeed a combination of the JNI overhead and expensive operations within onBindViewHolder.

To verify this, I have observed that:

  1. JNI Overhead: Each call to browserRowForId crosses the JNI boundary, During fast or slow scrolling with RecyclerFastScroller, this happens dozens of times per second, By introducing the LruCache in the ViewModel, i have reduced these synchronous calls significantly, making the data retrieval almost instantaneous for cached rows.

  2. UI & Regex: removeSounds Regex is now only called for Question/Answer columns, and setColor now skips redundant background updates if the color hasn't changed, This avoids unnecessary layout passes and drawable creations.

  3. FastScroller: While the scroller might be aggressive in triggering updates, optimizing onBindViewHolder makes it lightweight enough to handle those frequent calls without dropping frames, even when scrolling slowly.

These changes combined ensure the UI thread remains responsive by moving the bottleneck out of the hot path.

Galal-20 avatar Jun 01 '26 15:06 Galal-20

This doesn't make sense to me, and the issue isn't fixed.

https://github.com/user-attachments/assets/505af800-c472-4e2a-9a0b-ba528fc47566

david-allison avatar Jun 01 '26 18:06 david-allison

This doesn't make sense to me, and the issue isn't fixed.

screen-20260601-191603-1780337751396.mp4

Thanks for your comment, what stuff that doesn’t make sense for you?
 I tested it on a lot of phones(Samsung, Pixel, Xiaomi) and the results good same as the video which’s i send it, so i will investigate again and find the perfect solution for this issue shortly and if you have more explanation for this problem I would be appreciated if you told me that.

Thanks again.

https://github.com/user-attachments/assets/dfe00c91-5869-44f7-b0e8-b7cb33fd8ce8

Galal-20 avatar Jun 02 '26 16:06 Galal-20

The submitter states:

Not reproducible in 2.23.1.

As far as I'm aware, none of the code which is changed in this PR was materially changed between 2.23.1 and 2.24.0 from a performance perspective, and I can reproduce the issue after your change.

I think the likelihood is that it's an issue in RecyclerFastScroller

Next steps: A git bisect should determine when the issue was introduced.

david-allison avatar Jun 04 '26 10:06 david-allison

Hello 👋, this PR has had no activity for more than 2 weeks and needs a reply from the author. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically

github-actions[bot] avatar Jun 29 '26 17:06 github-actions[bot]

I'm sorry for replying late, I will check again and working on for this pr

Galal-20 avatar Jul 03 '26 00:07 Galal-20

Hello 👋, this PR has had no activity for more than 2 weeks and needs a reply from the author. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically

github-actions[bot] avatar Jul 17 '26 02:07 github-actions[bot]

Hello @david-allison, Thanks for the pointer, you were spot on the issue was indeed layout thrashing in RecyclerFastScroller.

Every onScrolled frame was calling show true, which invoked requestLayout(). This forced continuous view layout passes (doTraversal()) on the UI thread.

Profiler Results:

Thread Count: Reduced from 27 to 16 threads (40.7% decrease, removing 11 unnecessary threads). UI Thread Load: Event/Layout processing time dropped from 16.12s (51.83%) down to 0.20s (0.62%) (freed the Main Thread from continuous layout thrashing). CPU Idle Time: Main Thread idle time (nativePollOnce) reached 55.56% (18.70s) (immediate responsiveness with zero lag during scrolling).

Review again please and till me if you have any comments.

Galal-20 avatar Jul 22 '26 12:07 Galal-20