Help with avoiding memory allocations
Posted: 28 Jan 2025, 21:43
Hi, I need some help in the hope to get rid of some memory allocations, if possible.
I see some happening here during measure and arrange and I'm not sure what's causing them:
Might be related to this custom panel (just there to overlay things and meant to be more lightweight than Grid)
Also I see some more allocations here related to PropertyChanged. Is this a boxing issue?
I have a notifying property of type Bracket, which is a Struct containing a Point, a Size and a bool.
Am I seeing these GetPropertyValue allocations because the structs get boxed? Can I avoid that somehow?
Also, I'm guessing feeding a Value Type into a IValueConverter will also always lead to boxing? For WPF it's fine but I'd love to avoid them in Unity if at all possible.
Also what's the Dictionary being used here?
I see some happening here during measure and arrange and I'm not sure what's causing them:
Might be related to this custom panel (just there to overlay things and meant to be more lightweight than Grid)
Code: Select all
/// <summary>A panel that stacks all its children on top of each other, letting them overlay each other.</summary>
public class OverlayStackPanel : Panel
{
public OverlayStackPanel()
{
Loaded += OnLoaded;
}
#if UNITY_5_3_OR_NEWER
private List<UIElement> InternalChildren { get; set; } = null;
#endif
/// <inheritdoc/>
protected override Size MeasureOverride(Size availableSize)
{
if (InternalChildren is null)
{
return new Size(0, 0);
}
double maxWidth = 0;
double maxHeight = 0;
for (var index = 0; index < InternalChildren.Count; index++)
{
var child = InternalChildren[index];
child?.Measure(availableSize);
if (child is null)
{
continue;
}
var childDesiredSize = child.DesiredSize;
maxWidth = Math.Max(maxWidth, childDesiredSize.Width);
maxHeight = Math.Max(maxHeight, childDesiredSize.Height);
}
// Panel size matches the largest child's size
return new Size((float)maxWidth, (float)maxHeight);
}
/// <inheritdoc/>
protected override Size ArrangeOverride(Size finalSize)
{
if (InternalChildren is null)
{
return finalSize;
}
// Arrange each child to occupy the same space (stacked on top of each other)
for (var index = 0; index < InternalChildren.Count; index++)
{
var child = InternalChildren[index];
// Positioning each child at (0, 0) ensures they are stacked on top of each other
child?.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
}
return finalSize;
}
private void OnLoaded(object sender, RoutedEventArgs args)
{
#if UNITY_5_3_OR_NEWER
// Cache children since calling Children in a loop seems to allocate
InternalChildren = Children.ToList();
#endif
Loaded -= OnLoaded;
}
}
Am I seeing these GetPropertyValue allocations because the structs get boxed? Can I avoid that somehow?
Also, I'm guessing feeding a Value Type into a IValueConverter will also always lead to boxing? For WPF it's fine but I'd love to avoid them in Unity if at all possible.
Also what's the Dictionary being used here?