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 TweenLine2DEquationExtensions
{
private static readonly BoxedPool<Line2DEquation> boxedLine2DEquationPool = new(2);
public static ITween TweenLine2DEquation(this Line2DEquation initialLine2DEquation, ITweenManager tweenManager, float duration, Line2DEquation targetLine2DEquation, System.Action<Line2DEquation> setMethod)
=> tweenManager.StartTween(duration,
{
Boxed<Line2DEquation> boxedInitial = boxedLine2DEquationPool.Get(initialLine2DEquation);
Boxed<Line2DEquation> boxedTarget = boxedLine2DEquationPool.Get(targetLine2DEquation);
ITween tween = tweenManager.StartTween(duration,
t => setMethod?.Invoke(
new Line2DEquation(
initialLine2DEquation.Slope.Lerp(targetLine2DEquation.Slope, t),
initialLine2DEquation.OffsetY.Lerp(targetLine2DEquation.OffsetY, t)
boxedInitial.Value.Slope.Lerp(boxedTarget.Value.Slope, t),
boxedInitial.Value.OffsetY.Lerp(boxedTarget.Value.OffsetY, t)
)
)
);
tween.OnComplete(() =>
{
boxedLine2DEquationPool.Return(boxedInitial);
boxedLine2DEquationPool.Return(boxedTarget);
});
return tween;
}
}