[Performance]Implement timers storage #824
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🎯 Goal
Reduce the number of Timers during runtime.
📝 Summary
🔥 Known Issues or Risks
Main Thread Saturation (if scheduled on RunLoop.main):
By default, Timer runs on the main run loop. A large number of timers firing frequently can block the main thread, degrading UI performance and responsiveness.
Retain Cycles / Memory Leaks:
Timers strongly retain their target unless you invalidate them or use a weak reference. This is a common source of memory leaks.
Battery and CPU Usage:
Timers that fire often (e.g., every 10ms or 100ms) and in large numbers increase CPU wakeups and can significantly drain battery and impact thermal state.
Accuracy / Jitter:
The more timers you have, the more the system may coalesce them, or introduce jitter, especially in low power states.
RunLoop Overhead:
Too many timers can lead to excessive overhead in RunLoop management, especially if their firing schedules are not aligned or if timers are added/removed frequently.
🛠 Implementation
We implement a centralised storage for Timers. We then use the storage to access timers rather than creating them. The storage keeps reference to TimerPublishers. The benefits of TimerPublisher can be found below:
🧪 Manual Testing Notes
✅ Call Test Summary
Scenario
1:1 call
for 10 minutesnoise-cancellation: active
background-filter: inactive
profiler: connected
debugger: disconnected
🧵 Tasks
🌡️ Thermal State Degradation
☑️ Contributor Checklist