Skip to content

Commit

Permalink
Merge pull request #279 from adarapata/fix_allocation_on_tickables
Browse files Browse the repository at this point in the history
Fix allocation on tickables
  • Loading branch information
hadashiA authored Jul 10, 2021
2 parents dec3038 + 559fb4a commit 36089e6
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 24 deletions.
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());
}
}

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.

1 comment on commit 36089e6

@vercel
Copy link

@vercel vercel bot commented on 36089e6 Jul 10, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.