Skip to content

Commit

Permalink
chore: Adjust for thread safety, reentrancy
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Nov 25, 2024
1 parent 7288875 commit cff4405
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/Uno.UI/UI/Xaml/DependencyObjectCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -25,7 +26,27 @@ public partial class DependencyObjectCollectionBase : DependencyObject
public partial class DependencyObjectCollection<T> : DependencyObjectCollectionBase, IList<T>, IEnumerable<T>, IEnumerable, IObservableVector<T>
where T : DependencyObject
{
public event VectorChangedEventHandler<T> VectorChanged;
// Explicit handlers list to avoid the cost of multicast delegates handling
private List<VectorChangedEventHandler<T>> _vectorChangedHandlers;

public event VectorChangedEventHandler<T> VectorChanged
{
add
{
lock (_vectorChangedHandlers)
{
(_vectorChangedHandlers ??= new()).Add(value);
}
}

remove
{
lock (_vectorChangedHandlers)
{
(_vectorChangedHandlers ??= new()).Remove(value);
}
}
}

private readonly List<T> _list = new List<T>();

Expand Down Expand Up @@ -205,7 +226,32 @@ internal List<T>.Enumerator GetEnumeratorFast()
=> _list.GetEnumerator();

private void RaiseVectorChanged(CollectionChange change, int index)
=> VectorChanged?.Invoke(this, new VectorChangedEventArgs(change, (uint)index));
{
if (_vectorChangedHandlers is { Count: > 0 })
{
var args = new VectorChangedEventArgs(change, (uint)index);

if (_vectorChangedHandlers.Count == 1)
{
_vectorChangedHandlers[0].Invoke(this, args);
}
else
{
// Clone the array to account for reentrancy.
var handlersClone = ArrayPool<VectorChangedEventHandler<T>>.Shared.Rent(_vectorChangedHandlers.Count);
_vectorChangedHandlers.CopyTo(handlersClone, 0);

// Use the original count, the rented array may be larger.
var count = _vectorChangedHandlers.Count;

Check notice on line 245 in src/Uno.UI/UI/Xaml/DependencyObjectCollection.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI/UI/Xaml/DependencyObjectCollection.cs#L245

Remove the unused local variable 'count'.
foreach (var handler in handlersClone)
{
handler.Invoke(this, args);
}

ArrayPool<VectorChangedEventHandler<T>>.Shared.Return(handlersClone);
}
}
}

private protected virtual void OnAdded(T d)
{
Expand Down

0 comments on commit cff4405

Please sign in to comment.