Skip to content

Commit

Permalink
fix(perf): Improve performance of Grid layouting by caching temporary…
Browse files Browse the repository at this point in the history
… array
  • Loading branch information
dr1rrb committed Feb 9, 2021
1 parent bf9be02 commit 107f722
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/Grid/Grid.Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,17 @@ private static double GetAvailableSizeForPosition(Memory<double> calculatedPixel
/// </summary>
private static double GetAdjustmentForSpacing(int rowOrColumnSpan, double spacing) => spacing * (rowOrColumnSpan - 1);

private static Memory<ViewPosition> FindStarSizeChildren(Span<ViewPosition> positions, Memory<ViewPosition> pixelSizeChildren, List<ViewPosition> autoSizeChildren)
private ViewPosition[] _resCache;

private Memory<ViewPosition> FindStarSizeChildren(Span<ViewPosition> positions, Memory<ViewPosition> pixelSizeChildren, List<ViewPosition> autoSizeChildren)
{
var res = new Memory<ViewPosition>(new ViewPosition[positions.Length]);
if ((_resCache?.Length ?? -1) != positions.Length)
{
// As configuration of Grids are usually pretty stable, for perf consideration (Avoids GC) we try to re-use the same array
_resCache = new ViewPosition[positions.Length];
}

var res = new Memory<ViewPosition>(_resCache);
int count = 0;

// Use local function to avoid the use of Enumerable.Any. foreach on List<T> uses allocation less
Expand Down

0 comments on commit 107f722

Please sign in to comment.