Syntriax f5a7077570 perf: improved garbage created by tweens slightly
They still do generate a lot of garbage but with boxed value pools I made the boxes reusable, it still does generate garbage through the delegate creation, gotta find a solution for them later
2025-08-14 20:31:46 +03:00

42 lines
1.4 KiB
C#

using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenPrimitiveExtensions
{
private static readonly BoxedPool<float> boxedFloatPool = new(2);
private static readonly BoxedPool<int> boxedIntPool = new(2);
public static ITween TweenFloat(this float initialFloat, ITweenManager tweenManager, float duration, float targetFloat, System.Action<float> setMethod)
{
Boxed<float> boxedInitial = boxedFloatPool.Get(initialFloat);
Boxed<float> boxedTarget = boxedFloatPool.Get(targetFloat);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedFloatPool.Return(boxedInitial);
boxedFloatPool.Return(boxedTarget);
});
return tween;
}
public static ITween TweenInt(this int initialInt, ITweenManager tweenManager, float duration, int targetInt, System.Action<float> setMethod)
{
Boxed<int> boxedInitial = boxedIntPool.Get(initialInt);
Boxed<int> boxedTarget = boxedIntPool.Get(targetInt);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value + (boxedTarget.Value - boxedInitial.Value) * t));
tween.OnComplete(() =>
{
boxedIntPool.Return(boxedInitial);
boxedIntPool.Return(boxedTarget);
});
return tween;
}
}