Skip to content

Commit

Permalink
Merge pull request #24 from mewlist/optimization
Browse files Browse the repository at this point in the history
Reduce GC allocation
  • Loading branch information
mewlist authored Feb 17, 2024
2 parents f785cfc + 3931132 commit 29d63f4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
7 changes: 3 additions & 4 deletions Runtime/Assets/Scene/SceneLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ private async ValueTask UnloadAsyncInternal(ISceneHandle targetHandle)

public async ValueTask UnloadAllAsync()
{
var toRemove = SceneHandles.ToArray();
await Task.WhenAll(
toRemove.Select(x =>
UnloadAsyncInternal(x).AsTask()));
await Task.WhenAll(SceneHandles.Select(x =>
UnifiedSceneLoader.UnloadAsync(x).AsTask()));
SceneHandles.Clear();
}

public async ValueTask DisposeAsync()
Expand Down
36 changes: 31 additions & 5 deletions Runtime/Helpers/TaskHelper.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace Mew.Core.TaskHelpers
{
public static class TaskHelper
{
#if UNITY_2023_2_OR_NEWER
private static readonly Stack<AwaitableCompletionSource> pool = new();
private static readonly Queue<AwaitableCompletionSource> queued = new();
private static readonly List<AwaitableCompletionSource> running = new();
private static bool registered;
#endif

public static async Task NextFrame()
{
#if UNITY_2023_2_OR_NEWER
AwaitableCompletionSource awaitableCompletionSource = new();
MewLoop.Add<MewUnityUpdate>(OnUpdate);
if (!registered)
{
MewLoop.Add<MewUnityUpdate>(OnUpdate);
registered = true;
}
if (pool.Count == 0)
pool.Push(new AwaitableCompletionSource());
var awaitableCompletionSource = pool.Pop();
awaitableCompletionSource.Reset();
queued.Enqueue(awaitableCompletionSource);
await awaitableCompletionSource.Awaitable;
MewLoop.Remove<MewUnityUpdate>(OnUpdate);
return;

void OnUpdate() => awaitableCompletionSource.TrySetResult();
void OnUpdate()
{
while (queued.Count > 0)
running.Add(queued.Dequeue());

foreach (var completionSource in running)
{
completionSource.TrySetResult();
pool.Push(completionSource);
}
running.Clear();
}
#else
TaskCompletionSource<bool> taskCompletionSource = new();
MewLoop.Add<MewUnityUpdate>(OnUpdate);
Expand Down
3 changes: 1 addition & 2 deletions Runtime/TaskQueue/TaskQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Mew.Core.Tasks
Expand Down Expand Up @@ -216,7 +215,7 @@ private async void UpdateInternal()

lock (SyncRoot)
{
task = queue.First();
task = queue[0];
queue.RemoveAt(0);
taskProcessing = true;
processingPriority = task.Priority;
Expand Down

0 comments on commit 29d63f4

Please sign in to comment.