From fbe91b5fbbe931e5bf946b2b3b1809fdd3159421 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Thu, 2 Sep 2021 15:47:44 -0400 Subject: [PATCH] perf(android): Simplify `Activity` instances tracking --- src/Uno.UI/BaseActivity.Android.cs | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/Uno.UI/BaseActivity.Android.cs b/src/Uno.UI/BaseActivity.Android.cs index 2b48883a2e0d..ccced5c081da 100644 --- a/src/Uno.UI/BaseActivity.Android.cs +++ b/src/Uno.UI/BaseActivity.Android.cs @@ -56,7 +56,7 @@ public static class TraceProvider public static event EventHandler CurrentChanged; private static int _instanceCount = 0; - private static IImmutableDictionary _instances = ImmutableDictionary.Empty; + private static Dictionary _instances = new Dictionary(); private static BaseActivity _current; /// @@ -67,7 +67,8 @@ public static class TraceProvider /// /// Gets a list of all activities which are currently alive. /// - public static IImmutableDictionary Instances => _instances; + public static IImmutableDictionary Instances + => ImmutableDictionary.Empty.AddRange(_instances) ; /// /// Gets the currently running activity, if any. @@ -246,37 +247,28 @@ private void ResignCurrent() private void NotifyCreatingInstance() { - IImmutableDictionary capture, updated; - do + lock (_instances) { - capture = _instances; - updated = capture.Add(Id, this); - } while (Interlocked.CompareExchange(ref _instances, updated, capture) != capture); + _instances.Add(Id, this); + } - InstancesChanged?.Invoke(null, ActivitiesCollectionChangedEventArgs.Added(Id, updated)); + InstancesChanged?.Invoke(null, ActivitiesCollectionChangedEventArgs.Added(Id, Instances)); } private void NotifyDestroyingInstance(bool isFinalizer) { try { - IImmutableDictionary capture, updated; - do + lock (_instances) { - capture = _instances; - if (!capture.ContainsKey(Id)) - { - return; - } - - updated = capture.Remove(Id); - } while (Interlocked.CompareExchange(ref _instances, updated, capture) != capture); + _instances.Remove(Id); + } DispatchedHandler notify = () => { try { - InstancesChanged?.Invoke(null, ActivitiesCollectionChangedEventArgs.Removed(Id, updated)); + InstancesChanged?.Invoke(null, ActivitiesCollectionChangedEventArgs.Removed(Id, Instances)); } catch (Exception e) {