Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High CPU usage while typing #3372

Merged

Conversation

laevandus
Copy link
Contributor

@laevandus laevandus commented Aug 12, 2024

🔗 Issue Links

Resolves PBE-5497

🎯 Goal

Reduce CPU usage caused by recreating CurrentChatUserController when sending typing events

📝 Summary

  • Add ChatClient.UserSession for reusing objects from connect to logout
  • User the shared CurrentChatUserController for reading privacy settings

🛠 Implementation

We check if we should send typing events using an internal property in the ChatChannelController which created a new instance of the CurrentChatUserController. This is called often when typing to a channel. This led to high CPU usage because controller is created and the local database is read on every character change. If the current user has, for example, a lot of muted channels, then recreating CurrentChatUser gets really expensive.

internal var shouldSendTypingEvents: Bool {
  let currentUserPrivacySettings = client.currentUserController().currentUser?.privacySettings
  
}      

🎨 Showcase

iPhone 12 Pro Max (low power mode was on)
An account with 10 muted channels (important). I used Chewbacca.

  1. Open a channel
  2. Type 10 characters
  3. Investigate main thread usage, especially shouldSendTypingEvents
Before After
666 ms 56 ms
------ -----
Before After

shouldSendTypingEvents does not spend ~66 ms per character change just for checking this property. Instead, there is a single ~56 ms cost once (or almost none if the shared controller had been already created. Currently it is created on demand.)

🧪 Manual Testing Notes

  • Try typing events and make sure these appear as expected.
  • Log in and log out, trigger more typing events

☑️ Contributor Checklist

  • I have signed the Stream CLA (required)
  • This change should be manually QAed
  • Changelog is updated with client-facing changes
  • Changelog is updated with new localization keys
  • New code is covered by unit tests
  • Comparison screenshots added for visual changes
  • Affected documentation updated (docusaurus, tutorial, CMS)

@laevandus laevandus added 🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK ⚡ Performance An Issue or PR related to performance improvements 🤞 Ready For QA A PR that is Ready for QA labels Aug 12, 2024
@laevandus laevandus requested a review from a team as a code owner August 12, 2024 12:46
@Stream-SDK-Bot
Copy link
Collaborator

SDK Size

title develop branch diff status
StreamChat 6.73MB 6.73MB 0.0MB 🟢
StreamChatUI 4.41MB 4.41MB 0.0MB 🟢

@Stream-SDK-Bot
Copy link
Collaborator

SDK Performance

target metric benchmark branch performance status
MessageList Hitches total duration 10 ms 8.34 ms 16.6% 🔼 🟢
Duration 2.6 s 2.54 s 2.31% 🔼 🟢
Hitch time ratio 4 ms per s 3.27 ms per s 18.25% 🔼 🟢
Frame rate 75 fps 78.48 fps 4.64% 🔼 🟢
Number of hitches 1 0.8 20.0% 🔼 🟢
ChannelList Hitches total duration 12.5 ms 26.7 ms -113.6% 🔽 🔴
Duration 2.6 s 2.55 s 1.92% 🔼 🟢
Hitch time ratio 5 ms per s 10.46 ms per s -109.2% 🔽 🔴
Frame rate 72 fps 74.2 fps 3.06% 🔼 🟢
Number of hitches 1.2 2.2 -83.33% 🔽 🔴

Copy link
Contributor

@martinmitrevski martinmitrevski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

Sources/StreamChat/ChatClient+UserSession.swift Outdated Show resolved Hide resolved
Copy link
Member

@nuno-vieira nuno-vieira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! ✅

@laevandus laevandus force-pushed the perf/typing-events-current-user-controller-recreation branch from e205a31 to 37c07fa Compare August 13, 2024 06:26
@Stream-SDK-Bot
Copy link
Collaborator

Stream-SDK-Bot commented Aug 13, 2024

SDK Size

title develop branch diff status
StreamChat 6.76MB 6.76MB +0KB 🟢
StreamChatUI 4.41MB 4.41MB 0KB 🟢

@testableapple testableapple added 🟢 QAed A PR that was QAed and removed 🤞 Ready For QA A PR that is Ready for QA labels Aug 13, 2024
@laevandus laevandus force-pushed the perf/typing-events-current-user-controller-recreation branch from 37c07fa to 030a3bd Compare August 14, 2024 07:17
Copy link

sonarcloud bot commented Aug 14, 2024

@laevandus laevandus enabled auto-merge (squash) August 14, 2024 07:40
@laevandus laevandus merged commit f0d8ba5 into develop Aug 14, 2024
15 checks passed
@laevandus laevandus deleted the perf/typing-events-current-user-controller-recreation branch August 14, 2024 08:00
@Stream-SDK-Bot Stream-SDK-Bot mentioned this pull request Aug 15, 2024
glennposadas added a commit to fotonotes/stream-chat-swift that referenced this pull request Sep 7, 2024
* main: (154 commits)
  Unblock release on workflow dispatch (GetStream#3389)
  [CI] Run release action on workflow dispatch (GetStream#3386)
  [CI] Do not unshallow branches on CI
  [CI] Do not unshallow branches on CI
  [CI] Update git config on merging develop and release branches
  [CI] Update git config on merging develop and release branches
  [CI] Pull origin release branch on release
  [CI] Pull origin release branch on release
  [CI] Fetch all branches on release (GetStream#3385)
  [CI] Fetch all branches on release (GetStream#3385)
  Bump 4.62.0
  Store pending message along with regular messages (GetStream#3378)
  [CI] Set up a default base branch when automatically creating PRs (GetStream#3383)
  Fix reading the local database state just after the initial write (GetStream#3373)
  [CI] Add space to SDK size badge (GetStream#3379)
  Update channel type documentation to reflect allowed characters (GetStream#3377)
  Reuse CurrentChatUserController instead of recreating it when sending typing events (GetStream#3372)
  Run offline state sync in the background (GetStream#3367)
  [CI] Bump fastlane plugin version (GetStream#3376)
  [CI] Bump fastlane actions plugin (GetStream#3375)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚡ Performance An Issue or PR related to performance improvements 🟢 QAed A PR that was QAed 🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants