From 2033a129760acbfda909acf270930425b47353cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=84=E3=82=82?= Date: Sat, 10 Jul 2021 17:08:37 +0900 Subject: [PATCH 1/6] fix allocation on tickable loop --- .../Runtime/Unity/PlayerLoopItem.cs | 6 ++-- .../Tests/Unity/PlayerLoopItemTest.cs | 32 +++++++++++++++++++ .../Tests/Unity/PlayerLoopItemTest.cs.meta | 3 ++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs create mode 100644 VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs.meta diff --git a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs index ee1b070f..93c5af56 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; #if VCONTAINER_UNITASK_INTEGRATION using System.Threading; using Cysharp.Threading.Tasks; @@ -221,7 +222,7 @@ public bool MoveNext() sealed class TickableLoopItem : IPlayerLoopItem, IDisposable { - readonly IEnumerable entries; + readonly List entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; @@ -229,7 +230,7 @@ public TickableLoopItem( IEnumerable entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries; + this.entries = entries.ToList(); this.exceptionHandler = exceptionHandler; } @@ -249,6 +250,7 @@ public bool MoveNext() return false; } } + return !disposed; } diff --git a/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs b/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs new file mode 100644 index 00000000..77246cd0 --- /dev/null +++ b/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs @@ -0,0 +1,32 @@ +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 + { + internal class TickableLoopItemTest + { + private class Ticker : ITickable + { + public void Tick() { } + } + + [Test] + public void MoveNextWithoutAllocation() + { + var list = new List { new Ticker(), new Ticker() }; + var exceptionHandler = new EntryPointExceptionHandler(exception => { }); + var tickableLoopItem = new TickableLoopItem(list, exceptionHandler); + + Assert.That(() => + { + tickableLoopItem.MoveNext(); + }, Is.Not.AllocatingGCMemory()); + } + } + } +} diff --git a/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs.meta b/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs.meta new file mode 100644 index 00000000..e501d888 --- /dev/null +++ b/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 78a772dee4fb4a8a9d55d1307959bfe7 +timeCreated: 1625903364 \ No newline at end of file From d24df815af3fa65b9d5986fc55d434f3f65ffcd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=84=E3=82=82?= Date: Sat, 10 Jul 2021 17:18:36 +0900 Subject: [PATCH 2/6] fix post and fixed --- .../Runtime/Unity/PlayerLoopItem.cs | 8 ++-- .../Tests/Unity/PlayerLoopItemTest.cs | 44 ++++++++++++++++--- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs index 93c5af56..661ecec0 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs @@ -150,7 +150,7 @@ public bool MoveNext() sealed class FixedTickableLoopItem : IPlayerLoopItem, IDisposable { - readonly IEnumerable entries; + readonly List entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; @@ -158,7 +158,7 @@ public FixedTickableLoopItem( IEnumerable entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries; + this.entries = entries.ToList(); this.exceptionHandler = exceptionHandler; } @@ -259,7 +259,7 @@ public bool MoveNext() sealed class PostTickableLoopItem : IPlayerLoopItem, IDisposable { - readonly IEnumerable entries; + readonly List entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; @@ -267,7 +267,7 @@ public PostTickableLoopItem( IEnumerable entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries; + this.entries = entries.ToList(); this.exceptionHandler = exceptionHandler; } diff --git a/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs b/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs index 77246cd0..79b29a7c 100644 --- a/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs +++ b/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs @@ -8,13 +8,15 @@ namespace VContainer.Tests.Unity { public class PlayerLoopItemTest { - internal class TickableLoopItemTest + private class Ticker : ITickable, IPostTickable, IFixedTickable { - private class Ticker : ITickable - { - public void Tick() { } - } + public void Tick() { } + public void FixedTick() { } + public void PostTick() { } + } + private class TickableLoopItemTest + { [Test] public void MoveNextWithoutAllocation() { @@ -28,5 +30,37 @@ public void MoveNextWithoutAllocation() }, Is.Not.AllocatingGCMemory()); } } + + private class PostTickableLoopItemTest + { + [Test] + public void MoveNextWithoutAllocation() + { + var list = new List { new Ticker(), new Ticker() }; + var exceptionHandler = new EntryPointExceptionHandler(exception => { }); + var tickableLoopItem = new PostTickableLoopItem(list, exceptionHandler); + + Assert.That(() => + { + tickableLoopItem.MoveNext(); + }, Is.Not.AllocatingGCMemory()); + } + } + + private class FixedTickableLoopItemTest + { + [Test] + public void MoveNextWithoutAllocation() + { + var list = new List { new Ticker(), new Ticker() }; + var exceptionHandler = new EntryPointExceptionHandler(exception => { }); + var tickableLoopItem = new FixedTickableLoopItem(list, exceptionHandler); + + Assert.That(() => + { + tickableLoopItem.MoveNext(); + }, Is.Not.AllocatingGCMemory()); + } + } } } From fe8e9075f5bd95652b365a3d376bdfc1645a36b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=84=E3=82=82?= Date: Sat, 10 Jul 2021 17:28:44 +0900 Subject: [PATCH 3/6] fix all tickables --- .../Runtime/Unity/PlayerLoopItem.cs | 8 ++--- .../Tests/Unity/PlayerLoopItemTest.cs | 36 ++++++++++++++++++- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs index 661ecec0..31e6cacb 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs @@ -186,7 +186,7 @@ public bool MoveNext() sealed class PostFixedTickableLoopItem : IPlayerLoopItem, IDisposable { - readonly IEnumerable entries; + readonly List entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; @@ -194,7 +194,7 @@ public PostFixedTickableLoopItem( IEnumerable entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries; + this.entries = entries.ToList(); this.exceptionHandler = exceptionHandler; } @@ -331,7 +331,7 @@ public bool MoveNext() sealed class PostLateTickableLoopItem : IPlayerLoopItem, IDisposable { - readonly IEnumerable entries; + readonly List entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; @@ -339,7 +339,7 @@ public PostLateTickableLoopItem( IEnumerable entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries; + this.entries = entries.ToList(); this.exceptionHandler = exceptionHandler; } diff --git a/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs b/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs index 79b29a7c..a4d37341 100644 --- a/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs +++ b/VContainer/Assets/VContainer/Tests/Unity/PlayerLoopItemTest.cs @@ -8,11 +8,13 @@ namespace VContainer.Tests.Unity { public class PlayerLoopItemTest { - private class Ticker : ITickable, IPostTickable, IFixedTickable + 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 @@ -47,6 +49,22 @@ public void MoveNextWithoutAllocation() } } + private class PostLateTickableLoopItemTest + { + [Test] + public void MoveNextWithoutAllocation() + { + var list = new List { 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] @@ -62,5 +80,21 @@ public void MoveNextWithoutAllocation() }, Is.Not.AllocatingGCMemory()); } } + + private class PostFixedTickableLoopItemTest + { + [Test] + public void MoveNextWithoutAllocation() + { + var list = new List { new Ticker(), new Ticker() }; + var exceptionHandler = new EntryPointExceptionHandler(exception => { }); + var tickableLoopItem = new PostFixedTickableLoopItem(list, exceptionHandler); + + Assert.That(() => + { + tickableLoopItem.MoveNext(); + }, Is.Not.AllocatingGCMemory()); + } + } } } From 37ad275298b776a36a1808cf5d7518e002fb7768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=84=E3=82=82?= Date: Sat, 10 Jul 2021 17:31:58 +0900 Subject: [PATCH 4/6] fix unnecessary spaces --- VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs index 31e6cacb..a7f62696 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs @@ -250,7 +250,6 @@ public bool MoveNext() return false; } } - return !disposed; } From 0a84e5043d7977dc78e5f5ad2ef69b56b8bd2a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=84=E3=82=82?= Date: Sat, 10 Jul 2021 18:20:37 +0900 Subject: [PATCH 5/6] change argument --- .../Runtime/Unity/PlayerLoopItem.cs | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs index a7f62696..f1089579 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs @@ -150,23 +150,24 @@ public bool MoveNext() sealed class FixedTickableLoopItem : IPlayerLoopItem, IDisposable { - readonly List entries; + readonly IReadOnlyList entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; public FixedTickableLoopItem( - IEnumerable entries, + IReadOnlyList entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries.ToList(); + this.entries = entries; this.exceptionHandler = exceptionHandler; } public bool MoveNext() { if (disposed) return false; - foreach (var x in entries) + for (var i = 0; i < entries.Count; i++) { + var x = entries[i]; try { x.FixedTick(); @@ -186,23 +187,24 @@ public bool MoveNext() sealed class PostFixedTickableLoopItem : IPlayerLoopItem, IDisposable { - readonly List entries; + readonly IReadOnlyList entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; public PostFixedTickableLoopItem( - IEnumerable entries, + IReadOnlyList entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries.ToList(); + this.entries = entries; this.exceptionHandler = exceptionHandler; } public bool MoveNext() { if (disposed) return false; - foreach (var x in entries) + for (var i = 0; i < entries.Count; i++) { + var x = entries[i]; try { x.PostFixedTick(); @@ -222,23 +224,24 @@ public bool MoveNext() sealed class TickableLoopItem : IPlayerLoopItem, IDisposable { - readonly List entries; + readonly IReadOnlyList entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; public TickableLoopItem( - IEnumerable entries, + IReadOnlyList entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries.ToList(); + this.entries = entries; this.exceptionHandler = exceptionHandler; } public bool MoveNext() { if (disposed) return false; - foreach (var x in entries) + for (var i = 0; i < entries.Count; i++) { + var x = entries[i]; try { x.Tick(); @@ -258,23 +261,24 @@ public bool MoveNext() sealed class PostTickableLoopItem : IPlayerLoopItem, IDisposable { - readonly List entries; + readonly IReadOnlyList entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; public PostTickableLoopItem( - IEnumerable entries, + IReadOnlyList entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries.ToList(); + this.entries = entries; this.exceptionHandler = exceptionHandler; } public bool MoveNext() { if (disposed) return false; - foreach (var x in entries) + for (var i = 0; i < entries.Count; i++) { + var x = entries[i]; try { x.PostTick(); @@ -294,12 +298,12 @@ public bool MoveNext() sealed class LateTickableLoopItem : IPlayerLoopItem, IDisposable { - readonly IEnumerable entries; + readonly IReadOnlyList entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; public LateTickableLoopItem( - IEnumerable entries, + IReadOnlyList entries, EntryPointExceptionHandler exceptionHandler) { this.entries = entries; @@ -309,8 +313,9 @@ public LateTickableLoopItem( public bool MoveNext() { if (disposed) return false; - foreach (var x in entries) + for (var i = 0; i < entries.Count; i++) { + var x = entries[i]; try { x.LateTick(); @@ -330,23 +335,24 @@ public bool MoveNext() sealed class PostLateTickableLoopItem : IPlayerLoopItem, IDisposable { - readonly List entries; + readonly IReadOnlyList entries; readonly EntryPointExceptionHandler exceptionHandler; bool disposed; public PostLateTickableLoopItem( - IEnumerable entries, + IReadOnlyList entries, EntryPointExceptionHandler exceptionHandler) { - this.entries = entries.ToList(); + this.entries = entries; this.exceptionHandler = exceptionHandler; } public bool MoveNext() { if (disposed) return false; - foreach (var x in entries) + for (var i = 0; i < entries.Count; i++) { + var x = entries[i]; try { x.PostLateTick(); From 559fb4a4d59197f4604e6860b4ae980276be1b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=84=E3=82=82?= Date: Sat, 10 Jul 2021 18:25:18 +0900 Subject: [PATCH 6/6] Remove unnecessary intermediate variables. --- .../VContainer/Runtime/Unity/PlayerLoopItem.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs index f1089579..84bad29a 100644 --- a/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs +++ b/VContainer/Assets/VContainer/Runtime/Unity/PlayerLoopItem.cs @@ -167,10 +167,9 @@ public bool MoveNext() if (disposed) return false; for (var i = 0; i < entries.Count; i++) { - var x = entries[i]; try { - x.FixedTick(); + entries[i].FixedTick(); } catch (Exception ex) { @@ -204,10 +203,9 @@ public bool MoveNext() if (disposed) return false; for (var i = 0; i < entries.Count; i++) { - var x = entries[i]; try { - x.PostFixedTick(); + entries[i].PostFixedTick(); } catch (Exception ex) { @@ -241,10 +239,9 @@ public bool MoveNext() if (disposed) return false; for (var i = 0; i < entries.Count; i++) { - var x = entries[i]; try { - x.Tick(); + entries[i].Tick(); } catch (Exception ex) { @@ -278,10 +275,9 @@ public bool MoveNext() if (disposed) return false; for (var i = 0; i < entries.Count; i++) { - var x = entries[i]; try { - x.PostTick(); + entries[i].PostTick(); } catch (Exception ex) { @@ -315,10 +311,9 @@ public bool MoveNext() if (disposed) return false; for (var i = 0; i < entries.Count; i++) { - var x = entries[i]; try { - x.LateTick(); + entries[i].LateTick(); } catch (Exception ex) { @@ -352,10 +347,9 @@ public bool MoveNext() if (disposed) return false; for (var i = 0; i < entries.Count; i++) { - var x = entries[i]; try { - x.PostLateTick(); + entries[i].PostLateTick(); } catch (Exception ex) {