perf: fix ChatListSortRooms redundant rebuilds and lifecycle leaks#2932
perf: fix ChatListSortRooms redundant rebuilds and lifecycle leaks#2932
Conversation
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the 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 configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughThis pull request introduces a generation-invalidated sorting pipeline for room list ordering in the chat interface. The sorting logic is refactored to use futures with stale result detection, allowing the system to discard in-flight sort operations when invalidated. Early exit conditions are added to handle widget unmounting and newer sort generation detection. A disposal method cancels subscriptions and increments the generation counter. Additionally, an unused import is removed from a utility file. 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
|
This PR has been deployed to https://linagora.github.io/twake-on-matrix/2932 |
1158615 to
ba164a4
Compare
tddang-linagora
left a comment
There was a problem hiding this comment.
- Add before after screenshots of the improvement (Either Flutter devtools or Chrome devtools)
ba164a4 to
046d017
Compare
bee06f0 to
4ea663d
Compare
4ea663d to
7cb83ee
Compare
Summary
Fix the chat list sort widget that was re-running an expensive async sort on every frame rebuild, and fix pre-existing lifecycle bugs causing memory leaks.
Problems
Redundant sort on every build():
FutureBuilder(future: sortRooms())was called directly inbuild(), creating a new Future on every rebuild. Since a parentStreamBuildertriggers rebuilds ~1x/second (rate-limited sync), this launched a full async sort of all rooms every second.Memory leak (pre-existing): No
dispose()method —StreamSubscriptioninstances (one per room, listening toroom.onUpdate) were never cancelled when the widget was destroyed.Crash risk (pre-existing):
sortRooms()accessescontextandwidgetafter multipleawaitcalls without checkingmounted. If the widget is disposed mid-sort, this throws"Looking up a deactivated widget's ancestor".Concurrent stale futures (pre-existing): Each
didUpdateWidgetcall launched a new sort without invalidating the previous one. Multiple sort futures could run in parallel, mutating shared state and writing to the parent'sValueNotifier.Solution
Futurein_sortFuture, computed only ininitState()anddidUpdateWidget()— not inbuild()dispose()to cancel all room subscriptionsmountedguards after everyawaitin_sortRooms()_sortGenerationcounter: each new sort increments it, and in-flight sorts bail out early if their generation is staleResults
Files changed
lib/pages/chat_list/chat_list_sort_rooms.dartSummary by CodeRabbit
Bug Fixes
Chores