Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -416,17 +416,16 @@ public override void RequestLayout(float scrollPosition, bool force = false)
//Console.WriteLine("[NUI] {0} :visibleArea before [{1},{2}] after [{3},{4}]", scrollPosition, prevFirstVisible, prevLastVisible, FirstVisible, LastVisible);

// 2. Unrealize invisible items.
List<RecyclerViewItem> unrealizedItems = new List<RecyclerViewItem>();
foreach (RecyclerViewItem item in VisibleItems)
VisibleItems.RemoveAll(item =>
{
if (item.Index < FirstVisible || item.Index > LastVisible)
{
//Console.WriteLine("[NUI] Unrealize{0}!", item.Index);
unrealizedItems.Add(item);
collectionView.UnrealizeItem(item);
return true;
}
}
VisibleItems.RemoveAll(unrealizedItems.Contains);
return false;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

While this change significantly improves performance by moving from O(N×M) to O(N), it doesn't strictly achieve "zero per-call allocation" as stated in the PR description. In C#, a lambda that captures local variables or this (which happens here via FirstVisible, LastVisible, and collectionView) results in a heap allocation for a closure object and a delegate instance on every call.

Since RequestLayout is on the scroll hot path, you could achieve true zero allocation by using a manual for loop to shift elements in-place, followed by a RemoveRange call. However, RemoveAll is much more idiomatic and readable, so this is only worth changing if absolute zero allocation is required for this performance-critical path.


//Console.WriteLine("Realize Begin [{0} to {1}]", FirstVisible, LastVisible);
// 3. Realize and placing visible items.
Expand Down Expand Up @@ -1029,22 +1028,20 @@ public override void NotifyItemRangeRemoved(IItemSource source, int startIndex,
else collectionView.ContentContainer.SizeHeight = ScrollContentSize;

// 3. Update Visible Items.
List<RecyclerViewItem> unrealizedItems = new List<RecyclerViewItem>();
foreach (RecyclerViewItem item in VisibleItems)
VisibleItems.RemoveAll(item =>
{
if ((item.Index >= startIndex)
&& (item.Index < startIndex + count))
{
unrealizedItems.Add(item);
collectionView.UnrealizeItem(item);
return true;
}
else if (item.Index >= startIndex + count)
if (item.Index >= startIndex + count)
{
item.Index -= count;
}
}
VisibleItems.RemoveAll(unrealizedItems.Contains);
unrealizedItems.Clear();
return false;
});

// Position Adjust
float scrollPosition = PrevScrollPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,17 +419,15 @@ public override void RequestLayout(float scrollPosition, bool force = false)
LastVisible = end;

// 2. Unrealize invisible items.
List<RecyclerViewItem> unrealizedItems = new List<RecyclerViewItem>();
foreach (RecyclerViewItem item in VisibleItems)
VisibleItems.RemoveAll(item =>
{
if (item.Index < FirstVisible || item.Index > LastVisible)
{
unrealizedItems.Add(item);
collectionView.UnrealizeItem(item);
return true;
}
}
VisibleItems.RemoveAll(unrealizedItems.Contains);
unrealizedItems.Clear();
return false;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Similar to the change in GridLayouter, this RemoveAll call involves a closure allocation because the lambda captures state. To completely eliminate heap pressure on the scroll hot path, a manual iteration and shifting approach would be required. Given the PR's focus on eliminating allocations, this is a relevant detail to consider.


// 3. Realize and placing visible items.
for (int i = FirstVisible; i <= LastVisible; i++)
Expand Down Expand Up @@ -1134,22 +1132,20 @@ public override void NotifyItemRangeRemoved(IItemSource source, int startIndex,
else collectionView.ContentContainer.SizeHeight = ScrollContentSize;

// 4. Update Visible Items.
List<RecyclerViewItem> unrealizedItems = new List<RecyclerViewItem>();
foreach (RecyclerViewItem item in VisibleItems)
VisibleItems.RemoveAll(item =>
{
if ((item.Index >= startIndex)
&& (item.Index < startIndex + count))
{
unrealizedItems.Add(item);
collectionView.UnrealizeItem(item);
return true;
}
else if (item.Index >= startIndex + count)
if (item.Index >= startIndex + count)
{
item.Index -= count;
}
}
VisibleItems.RemoveAll(unrealizedItems.Contains);
unrealizedItems.Clear();
return false;
});

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🤖 [AI Review]
🟡 Suggestion: The lambda still captures startIndex, count, and collectionView, forcing a per-call closure allocation — so the PR's stated "zero per-call allocation" goal is not fully achieved here. A reverse-iteration for loop with RemoveAt eliminates both the temp list and the closure with no behavior change.

Suggested change
VisibleItems.RemoveAll(item =>
{
if ((item.Index >= startIndex)
&& (item.Index < startIndex + count))
{
unrealizedItems.Add(item);
collectionView.UnrealizeItem(item);
return true;
}
else if (item.Index >= startIndex + count)
if (item.Index >= startIndex + count)
{
item.Index -= count;
}
}
VisibleItems.RemoveAll(unrealizedItems.Contains);
unrealizedItems.Clear();
return false;
});
for (int i = VisibleItems.Count - 1; i >= 0; i--)
{
RecyclerViewItem item = VisibleItems[i];
if (item.Index >= startIndex && item.Index < startIndex + count)
{
collectionView.UnrealizeItem(item);
VisibleItems.RemoveAt(i);
}
else if (item.Index >= startIndex + count)
{
item.Index -= count;
}
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🤖 [AI Review]
Addressed in 03a8955 — replaced RemoveAll(lambda) with a reverse for loop using RemoveAt, eliminating both the temp list and the closure allocation on this path.


if (startIndex <= FirstVisible)
{
Expand Down
Loading