Skip to content

Commit

Permalink
perf(android): Simplify Activity instances tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Sep 7, 2021
1 parent 986d922 commit fbe91b5
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/Uno.UI/BaseActivity.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static class TraceProvider
public static event EventHandler<CurrentActivityChangedEventArgs> CurrentChanged;

private static int _instanceCount = 0;
private static IImmutableDictionary<int, BaseActivity> _instances = ImmutableDictionary<int, BaseActivity>.Empty;
private static Dictionary<int, BaseActivity> _instances = new Dictionary<int, BaseActivity>();
private static BaseActivity _current;

/// <summary>
Expand All @@ -67,7 +67,8 @@ public static class TraceProvider
/// <summary>
/// Gets a list of all activities which are currently alive.
/// </summary>
public static IImmutableDictionary<int, BaseActivity> Instances => _instances;
public static IImmutableDictionary<int, BaseActivity> Instances
=> ImmutableDictionary<int, BaseActivity>.Empty.AddRange(_instances) ;

/// <summary>
/// Gets the currently running activity, if any.
Expand Down Expand Up @@ -246,37 +247,28 @@ private void ResignCurrent()

private void NotifyCreatingInstance()
{
IImmutableDictionary<int, BaseActivity> 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<int, BaseActivity> 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)
{
Expand Down

0 comments on commit fbe91b5

Please sign in to comment.