Skip to content
Closed
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 @@ -677,33 +677,17 @@ private bool HandleItemAdded(int newStartingIndex, object newItem, int? viewInde
return false;
}

if (newStartingIndex == 0 || _view.Count == 0)
{
newViewIndex = 0;
}
else if (newStartingIndex == _source.Count - 1)
{
newViewIndex = _view.Count - 1;
}
else if (viewIndex.HasValue)
if (viewIndex.HasValue)
{
newViewIndex = viewIndex.Value;
}
else
{
for (int i = 0, j = 0; i < _source.Count; i++)
{
if (i == newStartingIndex)
{
newViewIndex = j;
break;
}

if (_view[j] == _source[i])
{
j++;
}
}
newViewIndex = _view.Select(x => _source.IndexOf(x)) // Get indexes of all items that are currently in view
Copy link
Member

Choose a reason for hiding this comment

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

Definitely worried about the performance impact here, this seems like a lot of extra work and memory to determine this calculation.

Copy link
Author

Choose a reason for hiding this comment

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

Yes it could impact on performance, but not sure about any other possible solutions right now. Someone should have to run benchmark stuff on this method to get a better overview.

Copy link
Member

Choose a reason for hiding this comment

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

If the unit test you added shows the failure case at least, then it'd be good if we can figure out how to fix the original algorithm here. We wouldn't want to add extra overhead to this helper.

This class is relatively isolated, so you may be able to pull it out to run benchmarking on it in isolation vs. trying to add benchmarking within the project. @Sergio0694 any tips/resources/articles to point @w-ahmad to for benchmarking code like this?

.Concat(new int[] { newStartingIndex }) // Add index of item that need to be inserted in view
.OrderBy(x => x)
.ToList()
.IndexOf(newStartingIndex); // Get the index for new item in view
}
}

Expand Down
49 changes: 49 additions & 0 deletions UnitTests/UnitTests.UWP/UI/Test_AdvancedCollectionView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,55 @@ public void Test_AdvancedCollectionView_Sorting_CustomComparable_With_Shaping()
Assert.AreEqual(42, ((Person)a.First()).Age);
}

[TestCategory("AdvancedCollectionView")]
[UITestMethod]
public void Test_AdvancedCollectionView_Filterd_Using_Shaping_Changing_Properties()
{
var l = new ObservableCollection<Person>
{
new Person()
{
Name = "lorem",
Age = 4
},
new Person()
{
Name = "imsum",
Age = 8
},
new Person()
{
Name = "dolor",
Age = 15
},
new Person()
{
Name = "sit",
Age = 16
},
new Person()
{
Name = "amet",
Age = 23
},
new Person()
{
Name = "consectetur",
Age = 42
},
};

var a = new AdvancedCollectionView(l, true);
a.ObserveFilterProperty(nameof(Person.Age));
a.Filter = p => ((Person)p).Age <= 5;

Assert.AreEqual(1, a.Count);

l[4].Age = 5;

Assert.AreEqual(2, a.Count);
}

private class DelegateComparable : IComparer
{
private Func<object, object, int> _func;
Expand Down