Skip to content

Commit

Permalink
Merge pull request #6 from Akeit0/main
Browse files Browse the repository at this point in the history
Remove unnecessary code. Avoid memory leak.
  • Loading branch information
AnnulusGames authored Dec 27, 2023
2 parents d2f666d + f2b32e4 commit 3e19d78
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ internal sealed class MotionStorage<TValue, TOptions, TAdapter> : IMotionStorage
// Data
public int?[] toEntryIndex = new int?[InitialCapacity];
public MotionData<TValue, TOptions>[] dataArray = new MotionData<TValue, TOptions>[InitialCapacity];
public Span<MotionData<TValue, TOptions>> DataSpan => dataArray.AsSpan(0, tail);

public MotionCallbackData[] callbacksArray = new MotionCallbackData[InitialCapacity];
public Span<MotionCallbackData> CallbacksSpan => callbacksArray.AsSpan(0, tail);

int tail;

const int InitialCapacity = 8;
Expand Down Expand Up @@ -195,6 +199,7 @@ void RemoveAt(int denseIndex)
public void RemoveAll(NativeList<int> indexes)
{
var entryIndexes = new NativeArray<int>(indexes.Length, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
var lastCallbacksSpan=CallbacksSpan;
for (int i = 0; i < entryIndexes.Length; i++)
{
entryIndexes[i] = (int)toEntryIndex[indexes[i]];
Expand All @@ -204,7 +209,8 @@ public void RemoveAll(NativeList<int> indexes)
{
RemoveAt(entries[entryIndexes[i]].DenseIndex);
}

//Avoid Memory leak
lastCallbacksSpan[tail..].Clear();
entryIndexes.Dispose();
}

Expand Down Expand Up @@ -243,7 +249,7 @@ public void Complete(MotionHandle handle)
{
var entry = entries[handle.Index];
var denseIndex = entry.DenseIndex;
if (denseIndex < 0 || denseIndex >= dataArray.Length)
if (denseIndex < 0 || denseIndex >= tail)
{
throw new ArgumentException("Motion has been destroyed or no longer exists.");
}
Expand Down Expand Up @@ -325,9 +331,10 @@ void CheckIndex(MotionHandle handle)
public void Reset()
{
entries.Reset();
for (int i = 0; i < toEntryIndex.Length; i++) toEntryIndex[i] = default;
for (int i = 0; i < dataArray.Length; i++) dataArray[i] = default;
for (int i = 0; i < callbacksArray.Length; i++) callbacksArray[i] = default;

toEntryIndex.AsSpan().Clear();
dataArray.AsSpan().Clear();
callbacksArray.AsSpan().Clear();
tail = 0;
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/LitMotion/Assets/LitMotion/Runtime/Internal/UpdateRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public unsafe void Update(float deltaTime, float unscaledDeltaTime)
using var output = new NativeArray<TValue>(count, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
using var completedIndexList = new NativeList<int>(count, Allocator.TempJob);

fixed (MotionData<TValue, TOptions>* dataPtr = storage.dataArray)
fixed (MotionData<TValue, TOptions>* dataPtr = storage.DataSpan)
{
// update data
var job = new MotionUpdateJob<TValue, TOptions, TAdapter>()
Expand All @@ -44,15 +44,14 @@ public unsafe void Update(float deltaTime, float unscaledDeltaTime)
job.Schedule(count, 16).Complete();

// invoke delegates
var span = storage.callbacksArray.AsSpan(0, storage.Count);
var callbackSpan = storage.CallbacksSpan;
var outputPtr = (TValue*)output.GetUnsafePtr();
var outputLength = output.Length;
for (int i = 0; i < span.Length; i++)
for (int i = 0; i < callbackSpan.Length; i++)
{
var status = (dataPtr + i)->Status;
if (status == MotionStatus.Playing)
{
ref var callbacks = ref span[i];
ref var callbacks = ref callbackSpan[i];
try
{
callbacks.InvokeUnsafe(outputPtr[i]);
Expand All @@ -64,7 +63,7 @@ public unsafe void Update(float deltaTime, float unscaledDeltaTime)
}
else if (status == MotionStatus.Completed)
{
ref var callbacks = ref span[i];
ref var callbacks = ref callbackSpan[i];
try
{
callbacks.InvokeUnsafe(outputPtr[i]);
Expand Down

0 comments on commit 3e19d78

Please sign in to comment.