Merge pull request #27391 from smoogipoo/high-performance-session-redux

Use high performance session during gameplay
This commit is contained in:
Bartłomiej Dach 2024-02-27 16:28:04 +01:00 committed by GitHub
commit 2889cf39d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 88 additions and 47 deletions

View File

@ -7,6 +7,7 @@ using System.IO;
using System.Reflection; using System.Reflection;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using Microsoft.Win32; using Microsoft.Win32;
using osu.Desktop.Performance;
using osu.Desktop.Security; using osu.Desktop.Security;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game; using osu.Game;
@ -15,9 +16,11 @@ using osu.Framework;
using osu.Framework.Logging; using osu.Framework.Logging;
using osu.Game.Updater; using osu.Game.Updater;
using osu.Desktop.Windows; using osu.Desktop.Windows;
using osu.Framework.Allocation;
using osu.Game.IO; using osu.Game.IO;
using osu.Game.IPC; using osu.Game.IPC;
using osu.Game.Online.Multiplayer; using osu.Game.Online.Multiplayer;
using osu.Game.Performance;
using osu.Game.Utils; using osu.Game.Utils;
using SDL2; using SDL2;
@ -28,6 +31,9 @@ namespace osu.Desktop
private OsuSchemeLinkIPCChannel? osuSchemeLinkIPCChannel; private OsuSchemeLinkIPCChannel? osuSchemeLinkIPCChannel;
private ArchiveImportIPCChannel? archiveImportIPCChannel; private ArchiveImportIPCChannel? archiveImportIPCChannel;
[Cached(typeof(IHighPerformanceSessionManager))]
private readonly HighPerformanceSessionManager highPerformanceSessionManager = new HighPerformanceSessionManager();
public OsuGameDesktop(string[]? args = null) public OsuGameDesktop(string[]? args = null)
: base(args) : base(args)
{ {

View File

@ -0,0 +1,43 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Runtime;
using osu.Framework.Allocation;
using osu.Framework.Logging;
using osu.Game.Performance;
namespace osu.Desktop.Performance
{
public class HighPerformanceSessionManager : IHighPerformanceSessionManager
{
private GCLatencyMode originalGCMode;
public IDisposable BeginSession()
{
enableHighPerformanceSession();
return new InvokeOnDisposal<HighPerformanceSessionManager>(this, static m => m.disableHighPerformanceSession());
}
private void enableHighPerformanceSession()
{
Logger.Log("Starting high performance session");
originalGCMode = GCSettings.LatencyMode;
GCSettings.LatencyMode = GCLatencyMode.LowLatency;
// Without doing this, the new GC mode won't kick in until the next GC, which could be at a more noticeable point in time.
GC.Collect(0);
}
private void disableHighPerformanceSession()
{
Logger.Log("Ending high performance session");
if (GCSettings.LatencyMode == GCLatencyMode.LowLatency)
GCSettings.LatencyMode = originalGCMode;
// No GC.Collect() as we were already collecting at a higher frequency in the old mode.
}
}
}

View File

@ -55,7 +55,6 @@ using osu.Game.Overlays.Notifications;
using osu.Game.Overlays.SkinEditor; using osu.Game.Overlays.SkinEditor;
using osu.Game.Overlays.Toolbar; using osu.Game.Overlays.Toolbar;
using osu.Game.Overlays.Volume; using osu.Game.Overlays.Volume;
using osu.Game.Performance;
using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Mods;
using osu.Game.Scoring; using osu.Game.Scoring;
using osu.Game.Screens; using osu.Game.Screens;
@ -792,8 +791,6 @@ namespace osu.Game
protected virtual UpdateManager CreateUpdateManager() => new UpdateManager(); protected virtual UpdateManager CreateUpdateManager() => new UpdateManager();
protected virtual HighPerformanceSession CreateHighPerformanceSession() => new HighPerformanceSession();
protected override Container CreateScalingContainer() => new ScalingContainer(ScalingMode.Everything); protected override Container CreateScalingContainer() => new ScalingContainer(ScalingMode.Everything);
#region Beatmap progression #region Beatmap progression
@ -1088,8 +1085,6 @@ namespace osu.Game
loadComponentSingleFile(new AccountCreationOverlay(), topMostOverlayContent.Add, true); loadComponentSingleFile(new AccountCreationOverlay(), topMostOverlayContent.Add, true);
loadComponentSingleFile<IDialogOverlay>(new DialogOverlay(), topMostOverlayContent.Add, true); loadComponentSingleFile<IDialogOverlay>(new DialogOverlay(), topMostOverlayContent.Add, true);
loadComponentSingleFile(CreateHighPerformanceSession(), Add);
loadComponentSingleFile(new BackgroundDataStoreProcessor(), Add); loadComponentSingleFile(new BackgroundDataStoreProcessor(), Add);
Add(difficultyRecommender); Add(difficultyRecommender);

View File

@ -1,42 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Screens.Play;
namespace osu.Game.Performance
{
public partial class HighPerformanceSession : Component
{
private readonly IBindable<bool> localUserPlaying = new Bindable<bool>();
[BackgroundDependencyLoader]
private void load(ILocalUserPlayInfo localUserInfo)
{
localUserPlaying.BindTo(localUserInfo.IsPlaying);
}
protected override void LoadComplete()
{
base.LoadComplete();
localUserPlaying.BindValueChanged(playing =>
{
if (playing.NewValue)
EnableHighPerformanceSession();
else
DisableHighPerformanceSession();
}, true);
}
protected virtual void EnableHighPerformanceSession()
{
}
protected virtual void DisableHighPerformanceSession()
{
}
}
}

View File

@ -0,0 +1,23 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
namespace osu.Game.Performance
{
/// <summary>
/// Allows creating a temporary "high performance" session, with the goal of optimising runtime
/// performance for gameplay purposes.
///
/// On desktop platforms, this will set a low latency GC mode which collects more frequently to avoid
/// GC spikes.
/// </summary>
public interface IHighPerformanceSessionManager
{
/// <summary>
/// Start a new high performance session.
/// </summary>
/// <returns>An <see cref="IDisposable"/> which will end the session when disposed.</returns>
IDisposable BeginSession();
}
}

View File

@ -25,6 +25,7 @@ using osu.Game.Input;
using osu.Game.Localisation; using osu.Game.Localisation;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
using osu.Game.Performance;
using osu.Game.Screens.Menu; using osu.Game.Screens.Menu;
using osu.Game.Screens.Play.PlayerSettings; using osu.Game.Screens.Play.PlayerSettings;
using osu.Game.Skinning; using osu.Game.Skinning;
@ -140,6 +141,8 @@ namespace osu.Game.Screens.Play
private bool quickRestart; private bool quickRestart;
private IDisposable? highPerformanceSession;
[Resolved] [Resolved]
private INotificationOverlay? notificationOverlay { get; set; } private INotificationOverlay? notificationOverlay { get; set; }
@ -152,6 +155,9 @@ namespace osu.Game.Screens.Play
[Resolved] [Resolved]
private BatteryInfo? batteryInfo { get; set; } private BatteryInfo? batteryInfo { get; set; }
[Resolved]
private IHighPerformanceSessionManager? highPerformanceSessionManager { get; set; }
public PlayerLoader(Func<Player> createPlayer) public PlayerLoader(Func<Player> createPlayer)
{ {
this.createPlayer = createPlayer; this.createPlayer = createPlayer;
@ -262,6 +268,9 @@ namespace osu.Game.Screens.Play
Debug.Assert(CurrentPlayer != null); Debug.Assert(CurrentPlayer != null);
highPerformanceSession?.Dispose();
highPerformanceSession = null;
// prepare for a retry. // prepare for a retry.
CurrentPlayer = null; CurrentPlayer = null;
playerConsumed = false; playerConsumed = false;
@ -303,6 +312,9 @@ namespace osu.Game.Screens.Play
BackgroundBrightnessReduction = false; BackgroundBrightnessReduction = false;
Beatmap.Value.Track.RemoveAdjustment(AdjustableProperty.Volume, volumeAdjustment); Beatmap.Value.Track.RemoveAdjustment(AdjustableProperty.Volume, volumeAdjustment);
highPerformanceSession?.Dispose();
highPerformanceSession = null;
return base.OnExiting(e); return base.OnExiting(e);
} }
@ -478,6 +490,10 @@ namespace osu.Game.Screens.Play
if (scheduledPushPlayer != null) if (scheduledPushPlayer != null)
return; return;
// Now that everything's been loaded, we can safely switch to a higher performance session without incurring too much overhead.
// Doing this prior to the game being pushed gives us a bit of time to stabilise into the high performance mode before gameplay starts.
highPerformanceSession ??= highPerformanceSessionManager?.BeginSession();
scheduledPushPlayer = Scheduler.AddDelayed(() => scheduledPushPlayer = Scheduler.AddDelayed(() =>
{ {
// ensure that once we have reached this "point of no return", readyForPush will be false for all future checks (until a new player instance is prepared). // ensure that once we have reached this "point of no return", readyForPush will be false for all future checks (until a new player instance is prepared).