Skip to content

Commit 654b98b

Browse files
authored
Remove closure/delegate allocation in ItemContainerGenerator (#6396)
1 parent 169d086 commit 654b98b

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemContainerGenerator.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ public DependencyObject ContainerFromItem(object item)
596596
int index;
597597

598598
DoLinearSearch(
599-
delegate(object o, DependencyObject d) { return ItemsControl.EqualsEx(o, item); },
599+
static (state, o, d) => ItemsControl.EqualsEx(o, state), item,
600600
out dummy, out container, out index, false);
601601

602602
return container;
@@ -627,18 +627,17 @@ public int IndexFromContainer(DependencyObject container, bool returnLocalIndex)
627627
DependencyObject dummy;
628628

629629
DoLinearSearch(
630-
delegate(object o, DependencyObject d) { return (d == container); },
630+
static (state, o, d) => d == state, container,
631631
out item, out dummy, out index, returnLocalIndex);
632632

633633
return index;
634634
}
635635

636636
// expose DoLinearSearch to internal code
637-
internal bool FindItem(Func<object, DependencyObject, bool> match,
637+
internal bool FindItem<TState>(Func<TState, object, DependencyObject, bool> match, TState matchState,
638638
out DependencyObject container, out int itemIndex)
639639
{
640-
object item;
641-
return DoLinearSearch(match, out item, out container, out itemIndex, false);
640+
return DoLinearSearch(match, matchState, out _, out container, out itemIndex, false);
642641
}
643642

644643
/// <summary>
@@ -685,7 +684,7 @@ internal bool FindItem(Func<object, DependencyObject, bool> match,
685684
/// <returns>
686685
/// true if found, false otherwise.
687686
/// </returns>
688-
private bool DoLinearSearch(Func<object, DependencyObject, bool> match,
687+
private bool DoLinearSearch<TState>(Func<TState, object, DependencyObject, bool> match, TState matchState,
689688
out object item, out DependencyObject container, out int itemIndex,
690689
bool returnLocalIndex)
691690
{
@@ -746,7 +745,7 @@ private bool DoLinearSearch(Func<object, DependencyObject, bool> match,
746745
for (; offset < endOffset; ++offset)
747746
{
748747
CollectionViewGroup group;
749-
bool found = match(rib.ItemAt(offset), rib.ContainerAt(offset));
748+
bool found = match(matchState, rib.ItemAt(offset), rib.ContainerAt(offset));
750749

751750
if (found)
752751
{
@@ -758,7 +757,7 @@ private bool DoLinearSearch(Func<object, DependencyObject, bool> match,
758757
// found a group; see if the group contains the item
759758
GroupItem groupItem = (GroupItem)rib.ContainerAt(offset);
760759
int indexInGroup;
761-
found = groupItem.Generator.DoLinearSearch(match, out item, out container, out indexInGroup, false);
760+
found = groupItem.Generator.DoLinearSearch(match, matchState, out item, out container, out indexInGroup, false);
762761
if (found)
763762
{
764763
itemIndex = indexInGroup;

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemsControl.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3690,9 +3690,8 @@ internal void AdjustItemInfosAfterGeneratorChange(IEnumerable<ItemInfo> list, bo
36903690
// otherwise see if an unclaimed container matches the item
36913691
object item = info.Item;
36923692
ItemContainerGenerator.FindItem(
3693-
delegate(object o, DependencyObject d)
3694-
{ return ItemsControl.EqualsEx(o, item) &&
3695-
!claimedContainers.Contains(d); },
3693+
static (state, o, d) => ItemsControl.EqualsEx(o, state.item) && !state.claimedContainers.Contains(d),
3694+
(item, claimedContainers),
36963695
out container, out index);
36973696
}
36983697

0 commit comments

Comments
 (0)