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
This commit is contained in:
2025-08-14 20:31:46 +03:00
parent 746d29fb7a
commit f5a7077570
15 changed files with 278 additions and 57 deletions

View File

@@ -4,13 +4,28 @@ namespace Engine.Systems.Tween;
public static class TweenLine2DExtensions
{
private static readonly BoxedPool<Line2D> boxedLine2DPool = new(2);
public static ITween TweenLine2D(this Line2D initialLine2D, ITweenManager tweenManager, float duration, Line2D targetLine2D, System.Action<Line2D> setMethod)
=> tweenManager.StartTween(duration,
{
Boxed<Line2D> boxedInitial = boxedLine2DPool.Get(initialLine2D);
Boxed<Line2D> boxedTarget = boxedLine2DPool.Get(targetLine2D);
ITween tween = tweenManager.StartTween(duration,
t => setMethod?.Invoke(
new Line2D(
initialLine2D.From.Lerp(targetLine2D.From, t),
initialLine2D.To.Lerp(targetLine2D.To, t)
boxedInitial.Value.From.Lerp(boxedTarget.Value.From, t),
boxedInitial.Value.To.Lerp(boxedTarget.Value.To, t)
)
)
);
tween.OnComplete(() =>
{
boxedLine2DPool.Return(boxedInitial);
boxedLine2DPool.Return(boxedTarget);
});
return tween;
}
}