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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public NotificationCallback() : base("com.unity.androidnotifications.Notificatio

public void onSentNotification(AndroidJavaObject notificationIntent)
{
AndroidReceivedNotificationMainThreadDispatcher.EnqueueReceivedNotification(notificationIntent);
AndroidReceivedNotificationMainThreadDispatcher.GetInstance().EnqueueReceivedNotification(notificationIntent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public class AndroidReceivedNotificationMainThreadDispatcher : MonoBehaviour
{
private static AndroidReceivedNotificationMainThreadDispatcher instance = null;

private static readonly Queue<AndroidJavaObject> s_ReceivedNotificationQueue = new Queue<AndroidJavaObject>();
private List<AndroidJavaObject> m_ReceivedNotificationQueue = new List<AndroidJavaObject>();

private static readonly List<AndroidJavaObject> s_ReceivedNotificationList = new List<AndroidJavaObject>();
private List<AndroidJavaObject> m_ReceivedNotificationList = new List<AndroidJavaObject>();

internal static void EnqueueReceivedNotification(AndroidJavaObject intent)
internal void EnqueueReceivedNotification(AndroidJavaObject intent)
{
lock (s_ReceivedNotificationQueue)
lock (this)
{
s_ReceivedNotificationQueue.Enqueue(intent);
m_ReceivedNotificationQueue.Add(intent);
}
}

Expand All @@ -34,18 +34,21 @@ public void Update()
{
// Note: Don't call callbacks while locking receivedNotificationQueue, otherwise there's a risk
// that callback might introduce an operations which would create a deadlock
lock (s_ReceivedNotificationQueue)
lock (this)
{
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 :)

return;
var temp = m_ReceivedNotificationQueue;
m_ReceivedNotificationQueue = m_ReceivedNotificationList;
m_ReceivedNotificationList = temp;
}

foreach (var notification in s_ReceivedNotificationList)
foreach (var notification in m_ReceivedNotificationList)
{
AndroidNotificationCenter.ReceivedNotificationCallback(notification);
}

s_ReceivedNotificationList.Clear();
m_ReceivedNotificationList.Clear();
}

void Awake()
Expand Down