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

Case 1340464 - remove GC allocation every frame on Android #101

Merged
merged 3 commits into from Jul 29, 2021

Conversation

aurimasc
Copy link
Collaborator

Improve Android implementation to be more performance and not make GC allocation every frame.
Changes:

  • Replace Queue with List (that alone fixes allocation when AddRange is used)
  • Rotate the two lists
  • Quick return if no queued notifications
  • Some refactoring

To QA: try to break it (flood notifications).

Copy link
Contributor

@vaidasma vaidasma left a comment

Choose a reason for hiding this comment

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

Nothing out of the ordinary noticed when using the app for an extensive amount of time while monitoring the memory consumption and the behavior of the application.

{
s_ReceivedNotificationList.AddRange(s_ReceivedNotificationQueue);
s_ReceivedNotificationQueue.Clear();
if (m_ReceivedNotificationQueue.Count == 0)
Copy link
Member

Choose a reason for hiding this comment

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

This feels fragile, for this code to work correctly, m_ReceivedNotificationList has to be empty, when the switch occurs. If it's not empty then out of order processing of notifications is possible. Only by looking at the whole file I understand that m_ReceivedNotificationList is empty when this switch occurs, but still if in the future someone will mess with m_ReceivedNotificationList, it will be easy to miss this place

Consider adding a safeguard
if (m_ReceivedNotificationList.Count != 0)
throw new Exception(..)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it's not empty, it means there are leftover notifications from last processing which shouldn't happen. Actually, this code still has a bug where this could happen (in case of exception), but that's addressed in different PR. When such leftovers happen, you get callbacks called again for the same notification next frame, most likely leading to the same exception again and postponing the same issue to the next frame etc. This change would scramble the ordering, but you'd still get bombarded with callbacks every frame when this happens.
Throwing exception like you propose would prevent callbacks from being called with logging about a bug. IMO, not throwing exception gives a clearer indication of bug (but it doesn't log the exact issue).

Copy link
Member

Choose a reason for hiding this comment

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

"clearer indication of bug" - really? :) it's really not that apparent, especially for a green user. Well, if you're a fan of hidden bugs, so be it :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's for sure clearer in our test project, reducing chances of us releasing a bug :)

Copy link
Member

@todi1856 todi1856 left a comment

Choose a reason for hiding this comment

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

Waiting for reply

@aurimasc aurimasc merged commit 1b271a7 into master Jul 29, 2021
@aurimasc aurimasc deleted the case-1340464 branch July 29, 2021 09:39
@aurimasc aurimasc mentioned this pull request Apr 7, 2022
Needle-Mirror-Bot pushed a commit to needle-mirror/com.unity.mobile.notifications that referenced this pull request Apr 26, 2022
## [1.4.3] - 2022-04-15

### Fixes:
- [Android] [issue 101](Unity-Technologies/com.unity.mobile.notifications#101) Fix GC allocation every frame.
- [1360115](https://issuetracker.unity3d.com/issues/mobile-notifications-project-settings-window-loses-focus-when-clicking-on-any-category-while-mobile-notifications-pane-is-open) Fixed focus loss when trying to switching from notifications settings.
- [Android] [case 1376849] Fixed receivers needs to be marked as exported in manifest file, disable exact alarms on Android 12 because they require special permissions.
- [iOS] [1375744](https://issuetracker.unity3d.com/issues/ios-app-freezes-slash-crashes-when-both-mobile-notifications-and-firebase-are-used) Fixed application startup hang when application uses Unity notifications or Firebase.
- [iOS] [case 1382960] Handle numeric data in UserInfo.
- [iOS] Correctly handle boolean values in UserInfo, previously boolean values were detected as numbers and get parsed as 1 or 0, now they will be correctly handled as 'true' and 'false'.
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.

None yet

3 participants