Skip to content

Commit

Permalink
Merge pull request #6521 from peppy/async-disposal-fix
Browse files Browse the repository at this point in the history
Reduce overhead of async disposal
  • Loading branch information
smoogipoo authored Feb 3, 2025
2 parents 87dc5a6 + e1b28eb commit 82f37c8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
35 changes: 35 additions & 0 deletions osu.Framework.Benchmarks/BenchmarkAsyncDisposal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using BenchmarkDotNet.Attributes;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;

namespace osu.Framework.Benchmarks
{
[MemoryDiagnoser]
public partial class BenchmarkAsyncDisposal
{
private readonly List<Drawable> objects = new List<Drawable>();

[GlobalSetup]
[OneTimeSetUp]
public virtual void SetUp()
{
objects.Clear();
for (int i = 0; i < 10000; i++)
objects.Add(new Box());
}

[Test]
[Benchmark]
public void Run()
{
objects.ForEach(AsyncDisposalQueue.Enqueue);
AsyncDisposalQueue.WaitForEmpty();
}
}
}
4 changes: 2 additions & 2 deletions osu.Framework/Allocation/AsyncDisposalQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace osu.Framework.Allocation
/// </summary>
internal static class AsyncDisposalQueue
{
private static readonly GlobalStatistic<string> last_disposal = GlobalStatistics.Get<string>("Drawable", "Last disposal");
private static readonly GlobalStatistic<Type> last_disposal = GlobalStatistics.Get<Type>("Drawable", "Last disposal");

private static Task runTask;

Expand Down Expand Up @@ -62,7 +62,7 @@ public static void Enqueue(IDisposable disposable)
{
ref var item = ref itemsToDispose[i];

last_disposal.Value = item.ToString();
last_disposal.Value = item.GetType();

item.Dispose();
item = null;
Expand Down
6 changes: 6 additions & 0 deletions osu.Framework/Statistics/GlobalStatistic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#nullable disable

using System;
using osu.Framework.Extensions.TypeExtensions;

namespace osu.Framework.Statistics
{
public class GlobalStatistic<T> : IGlobalStatistic
Expand All @@ -23,6 +26,9 @@ public string DisplayValue
{
switch (Value)
{
case Type t:
return t.ReadableName();

case double d:
return d.ToString("#,0.##");

Expand Down

0 comments on commit 82f37c8

Please sign in to comment.