Skip to content

Commit

Permalink
Merge pull request #6197 from EVAST9919/performance-overlay-alloc
Browse files Browse the repository at this point in the history
Reduce allocations in `PerformanceOverlay` while in expanded state
  • Loading branch information
peppy committed Feb 26, 2024
2 parents 82cc944 + c35d537 commit 48decc3
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions osu.Framework/Graphics/Performance/PerformanceOverlay.cs
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
Expand Down Expand Up @@ -39,6 +40,8 @@ internal partial class PerformanceOverlay : FillFlowContainer, IStateful<FrameSt

private bool initialised;

private readonly List<FrameStatisticsDisplay> frameDisplays = new List<FrameStatisticsDisplay>();

public FrameStatisticsMode State
{
get => state;
Expand Down Expand Up @@ -77,22 +80,27 @@ protected override void LoadComplete()

// for some reason PerformanceOverlay has 0 width despite using AutoSizeAxes, and it doesn't look simple to fix.
// let's just work around it and consider frame statistics display dimensions for receiving input events.
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => Children.OfType<FrameStatisticsDisplay>().Any(d => d.ReceivePositionalInputAt(screenSpacePos));
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos)
{
foreach (var display in frameDisplays)
{
if (display.ReceivePositionalInputAt(screenSpacePos))
return true;
}

return false;
}

protected override bool OnKeyDown(KeyDownEvent e)
{
switch (e.Key)
{
case Key.ControlLeft:
foreach (var display in Children.OfType<FrameStatisticsDisplay>())
display.Expanded = true;

applyToDisplays(static d => d.Expanded = true);
break;

case Key.ShiftLeft:
foreach (var display in Children.OfType<FrameStatisticsDisplay>())
display.Running = false;

applyToDisplays(static d => d.Running = false);
break;
}

Expand All @@ -104,15 +112,11 @@ protected override void OnKeyUp(KeyUpEvent e)
switch (e.Key)
{
case Key.ControlLeft:
foreach (var display in Children.OfType<FrameStatisticsDisplay>())
display.Expanded = false;

applyToDisplays(static d => d.Expanded = false);
break;

case Key.ShiftLeft:
foreach (var display in Children.OfType<FrameStatisticsDisplay>())
display.Running = true;

applyToDisplays(static d => d.Running = true);
break;
}

Expand All @@ -124,15 +128,11 @@ protected override bool OnTouchDown(TouchDownEvent e)
switch (e.Touch.Source)
{
case TouchSource.Touch1:
foreach (var display in Children.OfType<FrameStatisticsDisplay>())
display.Expanded = true;

applyToDisplays(static d => d.Expanded = true);
break;

case TouchSource.Touch2:
foreach (var display in Children.OfType<FrameStatisticsDisplay>())
display.Running = false;

applyToDisplays(static d => d.Running = false);
break;
}

Expand All @@ -144,15 +144,11 @@ protected override void OnTouchUp(TouchUpEvent e)
switch (e.Touch.Source)
{
case TouchSource.Touch1:
foreach (var display in Children.OfType<FrameStatisticsDisplay>())
display.Expanded = false;

applyToDisplays(static d => d.Expanded = false);
break;

case TouchSource.Touch2:
foreach (var display in Children.OfType<FrameStatisticsDisplay>())
display.Running = true;

applyToDisplays(static d => d.Running = true);
break;
}

Expand Down Expand Up @@ -188,25 +184,34 @@ private void updateState()

foreach (GameThread t in host.Threads)
{
Add(new FrameStatisticsDisplay(t, uploadPool)
var display = new FrameStatisticsDisplay(t, uploadPool)
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
State = state
});
};

Add(display);
frameDisplays.Add(display);
}
}

this.FadeIn(100);
break;
}

foreach (FrameStatisticsDisplay d in Children.OfType<FrameStatisticsDisplay>())
d.State = state;
foreach (var display in frameDisplays)
display.State = state;

StateChanged?.Invoke(State);
}

private void applyToDisplays(Predicate<FrameStatisticsDisplay> predicate)
{
foreach (var display in frameDisplays)
predicate.Invoke(display);
}

private void updateInfoText()
{
if (infoText == null)
Expand Down

0 comments on commit 48decc3

Please sign in to comment.