Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove managed function pointer support #150

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,27 @@ public unsafe struct ManagedMotionData
public object State0;
public object State1;
public object State2;
public void* UpdateActionPtr;
public object UpdateAction;
public Action OnCompleteAction;
public Action OnCancelAction;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UpdateUnsafe<TValue>(in TValue value) where TValue : unmanaged
{
if (UpdateActionPtr == null)
switch (StateCount)
{
switch (StateCount)
{
case 0:
UnsafeUtility.As<object, Action<TValue>>(ref UpdateAction)?.Invoke(value);
break;
case 1:
UnsafeUtility.As<object, Action<TValue, object>>(ref UpdateAction)?.Invoke(value, State0);
break;
case 2:
UnsafeUtility.As<object, Action<TValue, object, object>>(ref UpdateAction)?.Invoke(value, State0, State1);
break;
case 3:
UnsafeUtility.As<object, Action<TValue, object, object, object>>(ref UpdateAction)?.Invoke(value, State0, State1, State2);
break;
}
}
else
{
switch (StateCount)
{
case 0:
((delegate* managed<TValue, void>)UpdateActionPtr)(value);
break;
case 1:
((delegate* managed<TValue, object, void>)UpdateActionPtr)(value, State0);
break;
case 2:
((delegate* managed<TValue, object, object, void>)UpdateActionPtr)(value, State0, State1);
break;
case 3:
((delegate* managed<TValue, object, object, object, void>)UpdateActionPtr)(value, State0, State1, State2);
break;
}
case 0:
UnsafeUtility.As<object, Action<TValue>>(ref UpdateAction)?.Invoke(value);
break;
case 1:
UnsafeUtility.As<object, Action<TValue, object>>(ref UpdateAction)?.Invoke(value, State0);
break;
case 2:
UnsafeUtility.As<object, Action<TValue, object, object>>(ref UpdateAction)?.Invoke(value, State0, State1);
break;
case 3:
UnsafeUtility.As<object, Action<TValue, object, object, object>>(ref UpdateAction)?.Invoke(value, State0, State1, State2);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public unsafe MotionHandle Create(ref MotionBuilder<TValue, TOptions, TAdapter>
managedDataRef.CancelOnError = buffer.CancelOnError;
managedDataRef.SkipValuesDuringDelay = buffer.SkipValuesDuringDelay;
managedDataRef.UpdateAction = buffer.UpdateAction;
managedDataRef.UpdateActionPtr = buffer.UpdateActionPtr;
managedDataRef.OnCancelAction = buffer.OnCancelAction;
managedDataRef.OnCompleteAction = buffer.OnCompleteAction;
managedDataRef.StateCount = buffer.StateCount;
Expand Down
112 changes: 0 additions & 112 deletions src/LitMotion/Assets/LitMotion/Runtime/MotionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public static void Return(MotionBuilderBuffer<TValue, TOptions> buffer)
buffer.StateCount = default;

buffer.UpdateAction = default;
buffer.UpdateActionPtr = default;
buffer.OnCompleteAction = default;
buffer.OnCancelAction = default;

Expand Down Expand Up @@ -85,7 +84,6 @@ public static void Return(MotionBuilderBuffer<TValue, TOptions> buffer)
public object State1;
public object State2;
public byte StateCount;
public void* UpdateActionPtr;
public object UpdateAction;
public Action OnCompleteAction;
public Action OnCancelAction;
Expand Down Expand Up @@ -332,76 +330,6 @@ public MotionHandle Bind<TState0, TState1, TState2>(TState0 state0, TState1 stat
return ScheduleMotion();
}

/// <summary>
/// Create motion and bind it to a specific object, property, etc.
/// </summary>
/// <param name="ptr">Function pointer that handles binding</param>
/// <returns>Handle of the created motion data.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe MotionHandle Bind(delegate* managed<TValue, void> ptr)
{
CheckBuffer();
SetCallbackData(ptr);
return ScheduleMotion();
}

/// <summary>
/// Create motion and bind it to a specific object.
/// </summary>
/// <typeparam name="TState">Type of state</typeparam>
/// <param name="state">Motion state</param>
/// <param name="ptr">Function pointer that handles binding</param>
/// <returns>Handle of the created motion data.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe MotionHandle Bind<TState>(TState state, delegate* managed<TValue, TState, void> ptr)
where TState : class
{
CheckBuffer();
SetCallbackData(state, ptr);
return ScheduleMotion();
}

/// <summary>
/// Create motion and bind it to a specific object.
/// </summary>
/// <typeparam name="TState0">Type of state</typeparam>
/// <typeparam name="TState1">Type of state</typeparam>
/// <param name="state0">Motion state</param>
/// <param name="state1">Motion state</param>
/// <param name="ptr">Function pointer that handles binding</param>
/// <returns>Handle of the created motion data.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe MotionHandle Bind<TState0, TState1>(TState0 state0, TState1 state1, delegate* managed<TValue, TState0, TState1, void> ptr)
where TState0 : class
where TState1 : class
{
CheckBuffer();
SetCallbackData(state0, state1, ptr);
return ScheduleMotion();
}

/// <summary>
/// Create motion and bind it to a specific object.
/// </summary>
/// <typeparam name="TState0">Type of state</typeparam>
/// <typeparam name="TState1">Type of state</typeparam>
/// <typeparam name="TState2">Type of state</typeparam>
/// <param name="state0">Motion state</param>
/// <param name="state1">Motion state</param>
/// <param name="state2">Motion state</param>
/// <param name="ptr">Funciton pointer that handles binding</param>
/// <returns>Handle of the created motion data.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe MotionHandle Bind<TState0, TState1, TState2>(TState0 state0, TState1 state1, TState2 state2, delegate* managed<TValue, TState0, TState1, TState2, void> ptr)
where TState0 : class
where TState1 : class
where TState2 : class
{
CheckBuffer();
SetCallbackData(state0, state1, state2, ptr);
return ScheduleMotion();
}

/// <summary>
/// Creates a MotionSettings from the values ​​set in the builder.
/// </summary>
Expand Down Expand Up @@ -525,46 +453,6 @@ internal readonly void SetCallbackData<TState0, TState1, TState2>(TState0 state0
buffer.UpdateAction = action;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe readonly void SetCallbackData(delegate* managed<TValue, void> action)
{
buffer.StateCount = 0;
buffer.UpdateActionPtr = action;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe readonly void SetCallbackData<TState>(TState state, delegate* managed<TValue, TState, void> action)
where TState : class
{
buffer.StateCount = 1;
buffer.State0 = state;
buffer.UpdateActionPtr = action;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe readonly void SetCallbackData<TState0, TState1>(TState0 state0, TState1 state1, delegate* managed<TValue, TState0, TState1, void> action)
where TState0 : class
where TState1 : class
{
buffer.StateCount = 2;
buffer.State0 = state0;
buffer.State1 = state1;
buffer.UpdateActionPtr = action;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe readonly void SetCallbackData<TState0, TState1, TState2>(TState0 state0, TState1 state1, TState2 state2, delegate* managed<TValue, TState0, TState1, TState2, void> action)
where TState0 : class
where TState1 : class
where TState2 : class
{
buffer.StateCount = 3;
buffer.State0 = state0;
buffer.State1 = state1;
buffer.State2 = state2;
buffer.UpdateActionPtr = action;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly void CheckBuffer()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ public unsafe static MotionHandle Bind<TValue, TOptions, TAdapter, TState>(this
where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
where TState : struct
{
return builder.Bind(Box.Create(state), action, &BindAction);
}

static void BindAction<TValue, TState>(TValue value, Box<TState> state, Action<TValue, TState> action)
where TValue : unmanaged
where TState : struct
{
action(value, state.Value);
return builder.Bind(Box.Create(state), action, (value, state, action) => action(value, state.Value));
}

/// <summary>
Expand Down
47 changes: 1 addition & 46 deletions src/LitMotion/Assets/LitMotion/Tests/Benchmark/BindBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ public void TearDown()
handles.Cancel();
}

[Test]
[Performance]
public unsafe void Benchmark_Bind_Pointer_Startup()
{
BenchmarkHelper.MeasureStartUp(() =>
{
int state = 0;
LMotion.Create(0f, 1f, 3f)
.Bind(Box.Create(state), (Action<float, int>)(static (x, state) => DoNothing(x, state)), &BindAction)
.AddTo(handles);
}, () => handles.Cancel());
}

[Test]
[Performance]
public void Benchmark_Bind_Box_StartUp()
Expand Down Expand Up @@ -95,21 +82,6 @@ public void Benchmark_Bind_Closure_Startup()
}, () => handles.Cancel());
}

[UnityTest]
[Performance]
public unsafe IEnumerator Benchmark_Bind_Pointer_Update()
{
return BenchmarkHelper.MeasureUpdate(() =>
{
for (int i = 0; i < 10000; i++)
{
LMotion.Create(0f, 1f, 3f)
.Bind(Box.Create(i), (Action<float, int>)(static (x, state) => DoNothing(x, state)), &BindAction)
.AddTo(handles);
}
});
}

[UnityTest]
[Performance]
public IEnumerator Benchmark_Bind_Box_Update()
Expand Down Expand Up @@ -175,24 +147,7 @@ public IEnumerator Benchmark_Bind_Closure_Update()
}
});
}

[Test]
[Performance]
public unsafe void Benchmark_Bind_Pointer_GC()
{
BenchmarkHelper.MeasureGC(() =>
{
for (int i = 0; i < 10000; i++)
{
LMotion.Create(0f, 1f, 3f)
.Bind(Box.Create(i), (Action<float, int>)(static (x, state) => DoNothing(x, state)), &BindAction)
.AddTo(handles);
}
});

handles.Cancel();
}


[Test]
[Performance]
public void Benchmark_Bind_Box_GC()
Expand Down

This file was deleted.

This file was deleted.

Loading