From b46a5e953133bc6d66a57a937368bd99b24a4502 Mon Sep 17 00:00:00 2001 From: mischa Date: Mon, 24 Jul 2023 20:35:55 +0800 Subject: [PATCH] historyBounds.total --- .../Core/LagCompensation/HistoryBounds.cs | 11 +++--- .../LagCompensation/HistoryBoundsTests.cs | 38 ++++++++++--------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Assets/Mirror/Core/LagCompensation/HistoryBounds.cs b/Assets/Mirror/Core/LagCompensation/HistoryBounds.cs index 70e65156b..be85cd1c2 100644 --- a/Assets/Mirror/Core/LagCompensation/HistoryBounds.cs +++ b/Assets/Mirror/Core/LagCompensation/HistoryBounds.cs @@ -15,6 +15,8 @@ public class HistoryBounds public readonly int limit; + public Bounds total; + public HistoryBounds(int limit) { // initialize queue with maximum capacity to avoid runtime resizing @@ -22,9 +24,9 @@ public HistoryBounds(int limit) history = new Queue(limit); } - // insert new bounds into history. returns new total bounds. + // insert new bounds into history. calculates new total bounds. // Queue.Dequeue() always has the oldest bounds. - public Bounds Insert(Bounds bounds) + public void Insert(Bounds bounds) { // remove oldest if limit reached if (history.Count >= limit) @@ -37,16 +39,15 @@ public Bounds Insert(Bounds bounds) // starting at latest bounds, not at 'new Bounds' because that would // encapsulate (0,0) too. // TODO make this not be O(N) - Bounds total = bounds; + total = bounds; foreach (Bounds b in history) total.Encapsulate(b); - - return total; } public void Reset() { history.Clear(); + total = new Bounds(); } } } diff --git a/Assets/Mirror/Tests/Editor/LagCompensation/HistoryBoundsTests.cs b/Assets/Mirror/Tests/Editor/LagCompensation/HistoryBoundsTests.cs index 31fe92e20..eb59d6a6a 100644 --- a/Assets/Mirror/Tests/Editor/LagCompensation/HistoryBoundsTests.cs +++ b/Assets/Mirror/Tests/Editor/LagCompensation/HistoryBoundsTests.cs @@ -44,7 +44,11 @@ public void Benchmark(int iterations, int insertions, int limit) float min = Random.Range(-1, 1); float max = Random.Range(min, 1); Bounds bounds = MinMax(min, max); - Bounds total = history.Insert(bounds); + history.Insert(bounds); + + // always call .total to include any getter calculations in + // the benchmark here. + Bounds total = history.total; } } } @@ -58,29 +62,29 @@ public void Insert_Basic() // insert initial [-1, 1]. // should calculate new bounds == initial. - Bounds total = history.Insert(MinMax(-1, 1)); + history.Insert(MinMax(-1, 1)); Assert.That(history.Count, Is.EqualTo(1)); - Assert.That(total, Is.EqualTo(MinMax(-1, 1))); + Assert.That(history.total, Is.EqualTo(MinMax(-1, 1))); // insert [0, 2] // should calculate new bounds == [-1, 2]. - total = history.Insert(MinMax(0, 2)); + history.Insert(MinMax(0, 2)); Assert.That(history.Count, Is.EqualTo(2)); - Assert.That(total, Is.EqualTo(MinMax(-1, 2))); + Assert.That(history.total, Is.EqualTo(MinMax(-1, 2))); // insert one that's smaller than current bounds [-.5, 0] // history needs to contain it even if smaller, because once the oldest // largest one gets removed, this one matters too. - total = history.Insert(MinMax(-0.5f, 0)); + history.Insert(MinMax(-0.5f, 0)); Assert.That(history.Count, Is.EqualTo(3)); - Assert.That(total, Is.EqualTo(MinMax(-1, 2))); + Assert.That(history.total, Is.EqualTo(MinMax(-1, 2))); // insert more than 'limit': [0, 0] // the oldest one [-1, 1] should be discarded. // new bounds should be [-0.5, 2] - total = history.Insert(MinMax(0, 0)); + history.Insert(MinMax(0, 0)); Assert.That(history.Count, Is.EqualTo(3)); - Assert.That(total, Is.EqualTo(MinMax(-0.5f, 2))); + Assert.That(history.total, Is.EqualTo(MinMax(-0.5f, 2))); } // player runs in a circles and visits the same areas again. @@ -93,27 +97,27 @@ public void Insert_Revisit() // insert initial [-1, 1]. // should calculate new bounds == initial. - Bounds total = history.Insert(MinMax(-1, 1)); + history.Insert(MinMax(-1, 1)); Assert.That(history.Count, Is.EqualTo(1)); - Assert.That(total, Is.EqualTo(MinMax(-1, 1))); + Assert.That(history.total, Is.EqualTo(MinMax(-1, 1))); // insert [0, 2] // should calculate new bounds == [-1, 2]. - total = history.Insert(MinMax(0, 2)); + history.Insert(MinMax(0, 2)); Assert.That(history.Count, Is.EqualTo(2)); - Assert.That(total, Is.EqualTo(MinMax(-1, 2))); + Assert.That(history.total, Is.EqualTo(MinMax(-1, 2))); // visit [-1, 1] again - total = history.Insert(MinMax(-1, 1)); + history.Insert(MinMax(-1, 1)); Assert.That(history.Count, Is.EqualTo(3)); - Assert.That(total, Is.EqualTo(MinMax(-1, 2))); + Assert.That(history.total, Is.EqualTo(MinMax(-1, 2))); // insert beyond limit. // oldest one [-1, 1] should be removed. // total should still include it because we revisited [1, 1]. - total = history.Insert(MinMax(0, 0)); + history.Insert(MinMax(0, 0)); Assert.That(history.Count, Is.EqualTo(3)); - Assert.That(total, Is.EqualTo(MinMax(-1, 2))); + Assert.That(history.total, Is.EqualTo(MinMax(-1, 2))); } [Test]