Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix allocation on tickables #279

Merged
merged 6 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
#if VCONTAINER_UNITASK_INTEGRATION
using System.Threading;
using Cysharp.Threading.Tasks;
Expand Down Expand Up @@ -149,12 +150,12 @@ public bool MoveNext()

sealed class FixedTickableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IFixedTickable> entries;
readonly IReadOnlyList<IFixedTickable> entries;
readonly EntryPointExceptionHandler exceptionHandler;
bool disposed;

public FixedTickableLoopItem(
IEnumerable<IFixedTickable> entries,
IReadOnlyList<IFixedTickable> entries,
EntryPointExceptionHandler exceptionHandler)
{
this.entries = entries;
Expand All @@ -164,11 +165,11 @@ public FixedTickableLoopItem(
public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
for (var i = 0; i < entries.Count; i++)
{
try
{
x.FixedTick();
entries[i].FixedTick();
}
catch (Exception ex)
{
Expand All @@ -185,12 +186,12 @@ public bool MoveNext()

sealed class PostFixedTickableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IPostFixedTickable> entries;
readonly IReadOnlyList<IPostFixedTickable> entries;
readonly EntryPointExceptionHandler exceptionHandler;
bool disposed;

public PostFixedTickableLoopItem(
IEnumerable<IPostFixedTickable> entries,
IReadOnlyList<IPostFixedTickable> entries,
EntryPointExceptionHandler exceptionHandler)
{
this.entries = entries;
Expand All @@ -200,11 +201,11 @@ public PostFixedTickableLoopItem(
public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
for (var i = 0; i < entries.Count; i++)
{
try
{
x.PostFixedTick();
entries[i].PostFixedTick();
}
catch (Exception ex)
{
Expand All @@ -221,12 +222,12 @@ public bool MoveNext()

sealed class TickableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<ITickable> entries;
readonly IReadOnlyList<ITickable> entries;
readonly EntryPointExceptionHandler exceptionHandler;
bool disposed;

public TickableLoopItem(
IEnumerable<ITickable> entries,
IReadOnlyList<ITickable> entries,
EntryPointExceptionHandler exceptionHandler)
{
this.entries = entries;
Expand All @@ -236,11 +237,11 @@ public TickableLoopItem(
public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
for (var i = 0; i < entries.Count; i++)
{
try
{
x.Tick();
entries[i].Tick();
}
catch (Exception ex)
{
Expand All @@ -257,12 +258,12 @@ public bool MoveNext()

sealed class PostTickableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IPostTickable> entries;
readonly IReadOnlyList<IPostTickable> entries;
readonly EntryPointExceptionHandler exceptionHandler;
bool disposed;

public PostTickableLoopItem(
IEnumerable<IPostTickable> entries,
IReadOnlyList<IPostTickable> entries,
EntryPointExceptionHandler exceptionHandler)
{
this.entries = entries;
Expand All @@ -272,11 +273,11 @@ public PostTickableLoopItem(
public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
for (var i = 0; i < entries.Count; i++)
{
try
{
x.PostTick();
entries[i].PostTick();
}
catch (Exception ex)
{
Expand All @@ -293,12 +294,12 @@ public bool MoveNext()

sealed class LateTickableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<ILateTickable> entries;
readonly IReadOnlyList<ILateTickable> entries;
readonly EntryPointExceptionHandler exceptionHandler;
bool disposed;

public LateTickableLoopItem(
IEnumerable<ILateTickable> entries,
IReadOnlyList<ILateTickable> entries,
EntryPointExceptionHandler exceptionHandler)
{
this.entries = entries;
Expand All @@ -308,11 +309,11 @@ public LateTickableLoopItem(
public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
for (var i = 0; i < entries.Count; i++)
{
try
{
x.LateTick();
entries[i].LateTick();
}
catch (Exception ex)
{
Expand All @@ -329,12 +330,12 @@ public bool MoveNext()

sealed class PostLateTickableLoopItem : IPlayerLoopItem, IDisposable
{
readonly IEnumerable<IPostLateTickable> entries;
readonly IReadOnlyList<IPostLateTickable> entries;
readonly EntryPointExceptionHandler exceptionHandler;
bool disposed;

public PostLateTickableLoopItem(
IEnumerable<IPostLateTickable> entries,
IReadOnlyList<IPostLateTickable> entries,
EntryPointExceptionHandler exceptionHandler)
{
this.entries = entries;
Expand All @@ -344,11 +345,11 @@ public PostLateTickableLoopItem(
public bool MoveNext()
{
if (disposed) return false;
foreach (var x in entries)
for (var i = 0; i < entries.Count; i++)
{
try
{
x.PostLateTick();
entries[i].PostLateTick();
}
catch (Exception ex)
{
Expand Down
100 changes: 100 additions & 0 deletions VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine.TestTools.Constraints;
using VContainer.Unity;
using Is = NUnit.Framework.Is;

namespace VContainer.Tests.Unity
{
public class PlayerLoopItemTest
{
private class Ticker : ITickable, IPostTickable, IPostLateTickable, IFixedTickable, IPostFixedTickable
{
public void Tick() { }
public void FixedTick() { }
public void PostTick() { }
public void PostLateTick() { }
public void PostFixedTick() { }
}

private class TickableLoopItemTest
{
[Test]
public void MoveNextWithoutAllocation()
{
var list = new List<ITickable> { new Ticker(), new Ticker() };
var exceptionHandler = new EntryPointExceptionHandler(exception => { });
var tickableLoopItem = new TickableLoopItem(list, exceptionHandler);

Assert.That(() =>
{
tickableLoopItem.MoveNext();
}, Is.Not.AllocatingGCMemory());
}
}

private class PostTickableLoopItemTest
{
[Test]
public void MoveNextWithoutAllocation()
{
var list = new List<IPostTickable> { new Ticker(), new Ticker() };
var exceptionHandler = new EntryPointExceptionHandler(exception => { });
var tickableLoopItem = new PostTickableLoopItem(list, exceptionHandler);

Assert.That(() =>
{
tickableLoopItem.MoveNext();
}, Is.Not.AllocatingGCMemory());
}
}

private class PostLateTickableLoopItemTest
{
[Test]
public void MoveNextWithoutAllocation()
{
var list = new List<IPostLateTickable> { new Ticker(), new Ticker() };
var exceptionHandler = new EntryPointExceptionHandler(exception => { });
var tickableLoopItem = new PostLateTickableLoopItem(list, exceptionHandler);

Assert.That(() =>
{
tickableLoopItem.MoveNext();
}, Is.Not.AllocatingGCMemory());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}

private class FixedTickableLoopItemTest
{
[Test]
public void MoveNextWithoutAllocation()
{
var list = new List<IFixedTickable> { new Ticker(), new Ticker() };
var exceptionHandler = new EntryPointExceptionHandler(exception => { });
var tickableLoopItem = new FixedTickableLoopItem(list, exceptionHandler);

Assert.That(() =>
{
tickableLoopItem.MoveNext();
}, Is.Not.AllocatingGCMemory());
}
}

private class PostFixedTickableLoopItemTest
{
[Test]
public void MoveNextWithoutAllocation()
{
var list = new List<IPostFixedTickable> { new Ticker(), new Ticker() };
var exceptionHandler = new EntryPointExceptionHandler(exception => { });
var tickableLoopItem = new PostFixedTickableLoopItem(list, exceptionHandler);

Assert.That(() =>
{
tickableLoopItem.MoveNext();
}, Is.Not.AllocatingGCMemory());
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.