From c908daa8c3774f5cc9fb2b3cd5fb8625ab753523 Mon Sep 17 00:00:00 2001 From: ikpil Date: Tue, 20 Feb 2024 01:53:50 +0900 Subject: [PATCH] RcCyclicBuffer optimizations @wrenge --- CHANGELOG.md | 1 + src/DotRecast.Core/Buffers/RcCyclicBuffer.cs | 10 +++++----- test/DotRecast.Core.Test/RcCyclicBufferTest.cs | 13 ++++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18e0d06..42ba3d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed - Changed DtPathCorridor.Init(int maxPath) function to allow setting the maximum path [@ikpil](https://github.com/ikpil) - Changed from List to RcCyclicBuffer in DtCrowdTelemetry execution timing sampling [@wrenge](https://github.com/wrenge) +- RcCyclicBuffer optimizations [@wrenge](https://github.com/wrenge) ### Removed diff --git a/src/DotRecast.Core/Buffers/RcCyclicBuffer.cs b/src/DotRecast.Core/Buffers/RcCyclicBuffer.cs index 65f9a91..a3671c3 100644 --- a/src/DotRecast.Core/Buffers/RcCyclicBuffer.cs +++ b/src/DotRecast.Core/Buffers/RcCyclicBuffer.cs @@ -10,14 +10,14 @@ namespace DotRecast.Core.Buffers { public struct Enumerator : IEnumerator { - private readonly RcCyclicBuffer _buffer; + private readonly RcCyclicBuffer _cb; private int _index; private readonly int _size; - internal Enumerator(RcCyclicBuffer buffer) + internal Enumerator(RcCyclicBuffer cb) { - _buffer = buffer; - _size = _buffer._size; + _cb = cb; + _size = _cb._size; _index = default; Reset(); } @@ -32,7 +32,7 @@ namespace DotRecast.Core.Buffers _index = -1; } - public T Current => _buffer[_index]; + public T Current => _cb[_index]; object IEnumerator.Current => Current; diff --git a/test/DotRecast.Core.Test/RcCyclicBufferTest.cs b/test/DotRecast.Core.Test/RcCyclicBufferTest.cs index 9c72e51..82f02fe 100644 --- a/test/DotRecast.Core.Test/RcCyclicBufferTest.cs +++ b/test/DotRecast.Core.Test/RcCyclicBufferTest.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using DotRecast.Core.Buffers; using DotRecast.Core.Collections; using NUnit.Framework; @@ -291,7 +293,7 @@ public class RcCyclicBufferTests Assert.That(buffer[i], Is.EqualTo(i)); } } - + [Test] public void RcCyclicBuffer_RegularForEachWorks() { @@ -304,21 +306,22 @@ public class RcCyclicBufferTests Assert.That(element, Is.EqualTo(refValues[index++])); } } - + [Test] public void RcCyclicBuffer_EnumeratorWorks() { - var refValues = new[] { 4, 3, 2, 1, 0 }; + var refValues = new int[] { 4, 3, 2, 1, 0 }; var buffer = new RcCyclicBuffer(5, refValues); + var index = 0; - var enumerator = buffer.GetEnumerator(); + using var enumerator = buffer.GetEnumerator(); enumerator.Reset(); while (enumerator.MoveNext()) { Assert.That(enumerator.Current, Is.EqualTo(refValues[index++])); } - + // Ensure Reset works properly index = 0; enumerator.Reset();