Skip to content

Commit

Permalink
Merge pull request #107 from AnnulusGames/fix-fastlistcore
Browse files Browse the repository at this point in the history
Change: FastListCore initialization to be lazy
  • Loading branch information
AnnulusGames committed Mar 17, 2024
2 parents b222616 + b75c131 commit cf2aa69
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
28 changes: 19 additions & 9 deletions src/LitMotion/Assets/LitMotion/Runtime/Collections/FastListCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ namespace LitMotion.Collections
[StructLayout(LayoutKind.Auto)]
public struct FastListCore<T>
{
public FastListCore(int initialCapacity)
{
array = new T[initialCapacity];
tailIndex = 0;
}
const int InitialCapacity = 8;

public static readonly FastListCore<T> Empty = new(0);
public static readonly FastListCore<T> Empty = default;

T[] array;
int tailIndex;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(T element)
{
if (array.Length == tailIndex)
if (array == null)
{
array = new T[InitialCapacity];
}
else if (array.Length == tailIndex)
{
Array.Resize(ref array, tailIndex * 2);
}
Expand All @@ -37,22 +37,32 @@ public void Add(T element)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemoveAtSwapback(int index)
{
Error.IsNull(array);
CheckIndex(index);

array[index] = array[tailIndex - 1];
array[tailIndex - 1] = default;
tailIndex--;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
public void Clear(bool removeArray = false)
{
if (array == null) return;

array.AsSpan().Clear();
tailIndex = 0;
if (removeArray) array = null;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EnsureCapacity(int capacity)
{
if (array == null)
{
array = new T[InitialCapacity];
}

while (array.Length < capacity)
{
Array.Resize(ref array, array.Length * 2);
Expand All @@ -73,7 +83,7 @@ public readonly int Length
get => tailIndex;
}

public readonly Span<T> AsSpan() => array.AsSpan(0, tailIndex);
public readonly Span<T> AsSpan() => array == null ? Span<T>.Empty : array.AsSpan(0, tailIndex);
public readonly T[] AsArray() => array;

readonly void CheckIndex(int index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public TextMeshProMotionAnimator()

TMP_Text target;
internal CharInfo[] charInfoArray;
internal FastListCore<MotionHandle> motionHandleList = new(16);
internal FastListCore<MotionHandle> motionHandleList;

TextMeshProMotionAnimator nextNode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace LitMotion
[AddComponentMenu("")]
internal sealed class MotionHandleLinker : MonoBehaviour
{
FastListCore<MotionHandle> cancelOnDestroyList = new(8);
FastListCore<MotionHandle> cancelOnDisableList = new(8);
FastListCore<MotionHandle> completeOnDisableList = new(8);
FastListCore<MotionHandle> cancelOnDestroyList;
FastListCore<MotionHandle> cancelOnDisableList;
FastListCore<MotionHandle> completeOnDisableList;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Register(MotionHandle handle, LinkBehaviour linkBehaviour)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace LitMotion
{
internal static class MotionStorageManager
{
static FastListCore<IMotionStorage> storageList = new(16);
static FastListCore<IMotionStorage> storageList;

public static int CurrentStorageId { get; private set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static MotionStorage<TValue, TOptions, TAdapter> GetOrCreate()
}
}

static FastListCore<IUpdateRunner> updateRunners = new(16);
static FastListCore<IUpdateRunner> updateRunners;

/// <summary>
/// ManualMotionDispatcher time. It increases every time Update is called.
Expand Down
20 changes: 10 additions & 10 deletions src/LitMotion/Assets/LitMotion/Runtime/MotionDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,16 @@ public static (UpdateRunner<TValue, TOptions, TAdapter> runner, bool isCreated)
}
}

static FastListCore<IUpdateRunner> initializationRunners = new(16);
static FastListCore<IUpdateRunner> earlyUpdateRunners = new(16);
static FastListCore<IUpdateRunner> fixedUpdateRunners = new(16);
static FastListCore<IUpdateRunner> preUpdateRunners = new(16);
static FastListCore<IUpdateRunner> updateRunners = new(16);
static FastListCore<IUpdateRunner> preLateUpdateRunners = new(16);
static FastListCore<IUpdateRunner> postLateUpdateRunners = new(16);
static FastListCore<IUpdateRunner> timeUpdateRunners = new(16);
static FastListCore<IUpdateRunner> initializationRunners;
static FastListCore<IUpdateRunner> earlyUpdateRunners;
static FastListCore<IUpdateRunner> fixedUpdateRunners;
static FastListCore<IUpdateRunner> preUpdateRunners;
static FastListCore<IUpdateRunner> updateRunners;
static FastListCore<IUpdateRunner> preLateUpdateRunners;
static FastListCore<IUpdateRunner> postLateUpdateRunners;
static FastListCore<IUpdateRunner> timeUpdateRunners;

internal static FastListCore<IUpdateRunner> EmptyList = new(0);
internal static FastListCore<IUpdateRunner> EmptyList = FastListCore<IUpdateRunner>.Empty;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static ref FastListCore<IUpdateRunner> GetRunnerList(PlayerLoopTiming playerLoopTiming)
Expand Down Expand Up @@ -273,7 +273,7 @@ public static void InitUpdateRunner()
}
}

static FastListCore<IUpdateRunner> updateRunners = new(16);
static FastListCore<IUpdateRunner> updateRunners;

public static MotionHandle Schedule<TValue, TOptions, TAdapter>(in MotionData<TValue, TOptions> data, in MotionCallbackData callbackData)
where TValue : unmanaged
Expand Down

0 comments on commit cf2aa69

Please sign in to comment.