Skip to content

perf: prevent iOS OOM kill with download concurrency limiter and reduced rebuilds#2950

Draft
9clg6 wants to merge 1 commit intomainfrom
perf/image-download-throttle
Draft

perf: prevent iOS OOM kill with download concurrency limiter and reduced rebuilds#2950
9clg6 wants to merge 1 commit intomainfrom
perf/image-download-throttle

Conversation

@9clg6
Copy link
Collaborator

@9clg6 9clg6 commented Mar 20, 2026

Summary

Prevent iOS out-of-memory kills caused by unbounded concurrent image downloads when scrolling fast through rooms with many images.

Changes

  • Add ImageDownloadQueue (semaphore pattern, max 4 concurrent): Throttles simultaneous image download/decode operations. Each MxcImage acquires a slot before loading and releases it when done or disposed.
  • Cancel pending downloads on dispose: When MxcImage is scrolled off-screen, its download ticket is cancelled, preventing wasted work and memory allocation for images the user will never see.
  • Integrate queue into GetPreviewUrlMixin: Throttles preview_url HTTP requests through the same shared queue, preventing dozens of URL preview fetches from firing simultaneously.
  • Add didUpdateWidget early-return in ChatList and AppAdaptiveScaffoldBody: Skips redundant processing when args haven't changed, avoiding unnecessary rebuilds on every parent rebuild.
  • Bump file_picker 10.1.7 → 10.3.10: Fixes ObjC FileUtils class name collision with OSAnalytics that caused random native crashes.
  • 6 unit tests for ImageDownloadQueue (FIFO ordering, cancel, release).

Performance measurements

Measured with Flutter Web --profile, CPU throttling 4x, Chrome DevTools trace, ~75s navigation (DMs, groups 51-85 members, contacts, settings, scrolling, member lists). Fresh Chrome instance.

Metric main image-throttle Delta
JS Heap delta +73.39 MB +27.19 MB -62.9%
JS Heap end 170.94 MB 126.59 MB -26.0%
Long tasks (>50ms) 51 38 -25.5%
GC events 5,538 1,953 -64.7%
GC total time 1,702 ms 1,556 ms -8.6%

The primary benefit is on iOS where the OOM killer is aggressive. The web measurements confirm reduced memory pressure. On iOS, this prevents the app from being killed during fast scrolling through image-heavy rooms.

Raw trace file attached: perf-trace-image-throttle.json.gz (load in Chrome DevTools > Performance > Load profile to verify).

Test recommendation

  1. Open a room with many images (e.g., Bug Me Harder)
  2. Scroll quickly up and down
  3. On iOS: the app should NOT be killed by the OS
  4. On web: monitor DevTools Memory — concurrent downloads should stay at ≤4
  5. Run flutter test test/utils/image_download_queue_test.dart

Ticket

No ticket — discovered during performance profiling.

…ced rebuilds

- Add ImageDownloadQueue (semaphore, max 4 concurrent) to throttle
  simultaneous image downloads and URL preview fetches
- Cancel pending downloads when MxcImage is disposed (scrolled off-screen)
- Integrate queue into GetPreviewUrlMixin to throttle preview_url HTTP requests
- Add early-return in ChatList.didUpdateWidget and
  AppAdaptiveScaffoldBodyController.didUpdateWidget to skip redundant
  processing when args haven't changed
- Bump file_picker 10.1.7 -> 10.3.10 to fix ObjC FileUtils class
  name collision with OSAnalytics that caused random native crashes
- Add 6 unit tests for ImageDownloadQueue (FIFO, cancel, release)
@coderabbitai
Copy link

coderabbitai bot commented Mar 20, 2026

Warning

Rate limit exceeded

@9clg6 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 12 minutes and 42 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 072fa0f4-6315-4b2c-93dd-3193fc674207

📥 Commits

Reviewing files that changed from the base of the PR and between 844b35b and ec6c684.

⛔ Files ignored due to path filters (1)
  • pubspec.lock is excluded by !**/*.lock
📒 Files selected for processing (6)
  • lib/pages/chat_list/chat_list.dart
  • lib/utils/image_download_queue.dart
  • lib/widgets/layouts/adaptive_layout/app_adaptive_scaffold_body.dart
  • lib/widgets/mixins/get_preview_url_mixin.dart
  • pubspec.yaml
  • test/utils/image_download_queue_test.dart
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/image-download-throttle
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@9clg6 9clg6 marked this pull request as draft March 20, 2026 12:56
@github-actions
Copy link
Contributor

This PR has been deployed to https://linagora.github.io/twake-on-matrix/2950

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant