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

View File

@ -1,10 +1,24 @@
using System;
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenAABBExtensions
{
public static ITween TweenAABB(this AABB initialAABB, ITweenManager tweenManager, float duration, AABB targetAABB, Action<AABB> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(new AABB(initialAABB.LowerBoundary.Lerp(targetAABB.LowerBoundary, t), initialAABB.UpperBoundary.Lerp(targetAABB.UpperBoundary, t))));
private static readonly BoxedPool<AABB> boxedAABBPool = new(2);
public static ITween TweenAABB(this AABB initialAABB, ITweenManager tweenManager, float duration, AABB targetAABB, System.Action<AABB> setMethod)
{
Boxed<AABB> boxedInitial = boxedAABBPool.Get(initialAABB);
Boxed<AABB> boxedTarget = boxedAABBPool.Get(targetAABB);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(new AABB(boxedInitial.Value.LowerBoundary.Lerp(boxedTarget.Value.LowerBoundary, t), boxedInitial.Value.UpperBoundary.Lerp(boxedTarget.Value.UpperBoundary, t))));
tween.OnComplete(() =>
{
boxedAABBPool.Return(boxedInitial);
boxedAABBPool.Return(boxedTarget);
});
return tween;
}
}

View File

@ -4,9 +4,21 @@ namespace Engine.Systems.Tween;
public static class TweenCamera2DExtensions
{
private static readonly BoxedPool<float> boxedFloatPool = new(2);
public static ITween TweenZoom(this ICamera2D camera2D, ITweenManager tweenManager, float duration, float targetZoom)
{
float initialZoom = camera2D.Zoom;
return tweenManager.StartTween(duration, t => camera2D.Zoom = initialZoom.Lerp(targetZoom, t));
Boxed<float> boxedInitial = boxedFloatPool.Get(camera2D.Zoom);
Boxed<float> boxedTarget = boxedFloatPool.Get(targetZoom);
ITween tween = tweenManager.StartTween(duration, t => camera2D.Zoom = boxedInitial.Value.Lerp(boxedTarget.Value, t));
tween.OnComplete(() =>
{
boxedFloatPool.Return(boxedInitial);
boxedFloatPool.Return(boxedTarget);
});
return tween;
}
}

View File

@ -4,13 +4,28 @@ namespace Engine.Systems.Tween;
public static class TweenCircleExtensions
{
private static readonly BoxedPool<Circle> boxedCirclePool = new(2);
public static ITween TweenCircle(this Circle initialCircle, ITweenManager tweenManager, float duration, Circle targetCircle, System.Action<Circle> setMethod)
=> tweenManager.StartTween(duration,
{
Boxed<Circle> boxedInitial = boxedCirclePool.Get(initialCircle);
Boxed<Circle> boxedTarget = boxedCirclePool.Get(targetCircle);
ITween tween = tweenManager.StartTween(duration,
t => setMethod?.Invoke(
new Circle(
initialCircle.Center.Lerp(targetCircle.Center, t),
initialCircle.Diameter.Lerp(targetCircle.Diameter, t)
boxedInitial.Value.Center.Lerp(boxedTarget.Value.Center, t),
boxedInitial.Value.Diameter.Lerp(boxedTarget.Value.Diameter, t)
)
)
);
tween.OnComplete(() =>
{
boxedCirclePool.Return(boxedInitial);
boxedCirclePool.Return(boxedTarget);
});
return tween;
}
}

View File

@ -4,6 +4,9 @@ namespace Engine.Systems.Tween;
public static class TweenColorExtensions
{
private static readonly BoxedPool<ColorHSV> boxedColorHSVPool = new(2);
private static readonly BoxedPool<ColorHSVA> boxedColorHSVAPool = new(2);
public static ITween TweenColor(this ColorRGB initialColorRGB, ITweenManager tweenManager, float duration, ColorRGB targetColorRGB, System.Action<ColorRGB> setMethod)
=> TweenColor((ColorHSV)initialColorRGB, tweenManager, duration, (ColorHSV)targetColorRGB, color => setMethod?.Invoke(color));
@ -11,8 +14,34 @@ public static class TweenColorExtensions
=> TweenColor((ColorHSVA)initialColorRGBA, tweenManager, duration, (ColorHSVA)targetColorRGBA, color => setMethod?.Invoke(color));
public static ITween TweenColor(this ColorHSV initialColorHSV, ITweenManager tweenManager, float duration, ColorHSV targetColorHSV, System.Action<ColorHSV> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialColorHSV.Lerp(targetColorHSV, t)));
{
Boxed<ColorHSV> boxedInitial = boxedColorHSVPool.Get(initialColorHSV);
Boxed<ColorHSV> boxedTarget = boxedColorHSVPool.Get(targetColorHSV);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedColorHSVPool.Return(boxedInitial);
boxedColorHSVPool.Return(boxedTarget);
});
return tween;
}
public static ITween TweenColor(this ColorHSVA initialColorHSVA, ITweenManager tweenManager, float duration, ColorHSVA targetColorHSVA, System.Action<ColorHSVA> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialColorHSVA.Lerp(targetColorHSVA, t)));
{
Boxed<ColorHSVA> boxedInitial = boxedColorHSVAPool.Get(initialColorHSVA);
Boxed<ColorHSVA> boxedTarget = boxedColorHSVAPool.Get(targetColorHSVA);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedColorHSVAPool.Return(boxedInitial);
boxedColorHSVAPool.Return(boxedTarget);
});
return tween;
}
}

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;
}
}

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;
}
}

View File

@ -0,0 +1,41 @@
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;
}
}

View File

@ -1,17 +1,31 @@
using System;
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenProjection1DExtensions
{
public static ITween TweenProjection1D(this Projection1D initialProjection1D, ITweenManager tweenManager, float duration, Projection1D targetProjection1D, Action<Projection1D> setMethod)
=> tweenManager.StartTween(duration,
private static readonly BoxedPool<Projection1D> boxedProjection1DPool = new(2);
public static ITween TweenProjection1D(this Projection1D initialProjection1D, ITweenManager tweenManager, float duration, Projection1D targetProjection1D, System.Action<Projection1D> setMethod)
{
Boxed<Projection1D> boxedInitial = boxedProjection1DPool.Get(initialProjection1D);
Boxed<Projection1D> boxedTarget = boxedProjection1DPool.Get(targetProjection1D);
ITween tween = tweenManager.StartTween(duration,
t => setMethod?.Invoke(
new Projection1D(
initialProjection1D.Min.Lerp(targetProjection1D.Min, t),
initialProjection1D.Max.Lerp(targetProjection1D.Max, t)
boxedInitial.Value.Min.Lerp(boxedTarget.Value.Min, t),
boxedInitial.Value.Max.Lerp(boxedTarget.Value.Max, t)
)
)
);
tween.OnComplete(() =>
{
boxedProjection1DPool.Return(boxedInitial);
boxedProjection1DPool.Return(boxedTarget);
});
return tween;
}
}

View File

@ -1,10 +1,24 @@
using System;
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenQuaternionExtensions
{
public static ITween TweenQuaternion(this Quaternion initialQuaternion, ITweenManager tweenManager, float duration, Quaternion targetQuaternion, Action<Quaternion> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialQuaternion.SLerp(targetQuaternion, t)));
private static readonly BoxedPool<Quaternion> boxedQuaternionPool = new(2);
public static ITween TweenQuaternion(this Quaternion initialQuaternion, ITweenManager tweenManager, float duration, Quaternion targetQuaternion, System.Action<Quaternion> setMethod)
{
Boxed<Quaternion> boxedInitial = boxedQuaternionPool.Get(initialQuaternion);
Boxed<Quaternion> boxedTarget = boxedQuaternionPool.Get(targetQuaternion);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.SLerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedQuaternionPool.Return(boxedInitial);
boxedQuaternionPool.Return(boxedTarget);
});
return tween;
}
}

View File

@ -5,40 +5,23 @@ namespace Engine.Systems.Tween;
public static class TweenTransform2DExtensions
{
public static ITween TweenPosition(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D targetPosition)
{
Vector2D initialPosition = transform2D.Position;
return tweenManager.StartTween(duration, t => transform2D.Position = initialPosition.Lerp(targetPosition, t));
}
=> transform2D.Position.TweenVector2D(tweenManager, duration, targetPosition, x => transform2D.Position = x);
public static ITween TweenScale(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D targetScale)
{
Vector2D initialScale = transform2D.Scale;
return tweenManager.StartTween(duration, t => transform2D.Scale = initialScale.Lerp(targetScale, t));
}
=> transform2D.Scale.TweenVector2D(tweenManager, duration, targetScale, x => transform2D.Scale = x);
public static ITween TweenRotation(this ITransform2D transform2D, ITweenManager tweenManager, float duration, float targetRotation)
{
float initialRotation = transform2D.Rotation;
return tweenManager.StartTween(duration, t => transform2D.Rotation = initialRotation.Lerp(targetRotation, t));
}
=> transform2D.Rotation.TweenFloat(tweenManager, duration, targetRotation, x => transform2D.Rotation = x);
public static ITween TweenLocalPosition(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D targetLocalPosition)
{
Vector2D initialLocalPosition = transform2D.LocalPosition;
return tweenManager.StartTween(duration, t => transform2D.LocalPosition = initialLocalPosition.Lerp(targetLocalPosition, t));
}
=> transform2D.LocalPosition.TweenVector2D(tweenManager, duration, targetLocalPosition, x => transform2D.LocalPosition = x);
public static ITween TweenLocalScale(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D targetLocalScale)
{
Vector2D initialLocalScale = transform2D.LocalScale;
return tweenManager.StartTween(duration, t => transform2D.LocalScale = initialLocalScale.Lerp(targetLocalScale, t));
}
=> transform2D.LocalScale.TweenVector2D(tweenManager, duration, targetLocalScale, x => transform2D.LocalScale = x);
public static ITween TweenLocalRotation(this ITransform2D transform2D, ITweenManager tweenManager, float duration, float targetLocalRotation)
{
float initialLocalRotation = transform2D.LocalRotation;
return tweenManager.StartTween(duration, t => transform2D.LocalRotation = initialLocalRotation.Lerp(targetLocalRotation, t));
}
=> transform2D.LocalRotation.TweenFloat(tweenManager, duration, targetLocalRotation, x => transform2D.LocalRotation = x);
public static ITween TweenPositionAdditive(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D additivePosition)
{
Vector2D progressedPosition = Vector2D.Zero;

View File

@ -1,18 +1,31 @@
using System;
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenTriangleExtensions
{
public static ITween TweenTriangle(this Triangle initialTriangle, ITweenManager tweenManager, float duration, Triangle targetTriangle, Action<Triangle> setMethod)
=> tweenManager.StartTween(duration,
t => setMethod?.Invoke(
private static readonly BoxedPool<Triangle> boxedTrianglePool = new(2);
public static ITween TweenTriangle(this Triangle initialTriangle, ITweenManager tweenManager, float duration, Triangle targetTriangle, System.Action<Triangle> setMethod)
{
Boxed<Triangle> boxedInitial = boxedTrianglePool.Get(initialTriangle);
Boxed<Triangle> boxedTarget = boxedTrianglePool.Get(targetTriangle);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(
new Triangle(
initialTriangle.A.Lerp(targetTriangle.A, t),
initialTriangle.B.Lerp(targetTriangle.B, t),
initialTriangle.C.Lerp(targetTriangle.C, t)
boxedInitial.Value.A.Lerp(boxedTarget.Value.A, t),
boxedInitial.Value.B.Lerp(boxedTarget.Value.B, t),
boxedInitial.Value.C.Lerp(boxedTarget.Value.C, t)
)
)
);
tween.OnComplete(() =>
{
boxedTrianglePool.Return(boxedInitial);
boxedTrianglePool.Return(boxedTarget);
});
return tween;
}
}

View File

@ -4,6 +4,21 @@ namespace Engine.Systems.Tween;
public static class TweenVector2DExtensions
{
private static readonly BoxedPool<Vector2D> boxedVector2DPool = new(2);
public static ITween TweenVector2D(this Vector2D initialVector2D, ITweenManager tweenManager, float duration, Vector2D targetVector2D, System.Action<Vector2D> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialVector2D.Lerp(targetVector2D, t)));
{
Boxed<Vector2D> boxedInitial = boxedVector2DPool.Get(initialVector2D);
Boxed<Vector2D> boxedTarget = boxedVector2DPool.Get(targetVector2D);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedVector2DPool.Return(boxedInitial);
boxedVector2DPool.Return(boxedTarget);
});
return tween;
}
}

View File

@ -4,6 +4,21 @@ namespace Engine.Systems.Tween;
public static class TweenVector3DExtensions
{
private static readonly BoxedPool<Vector3D> boxedVector3DPool = new(2);
public static ITween TweenVector3D(this Vector3D initialVector3D, ITweenManager tweenManager, float duration, Vector3D targetVector3D, System.Action<Vector3D> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialVector3D.Lerp(targetVector3D, t)));
{
Boxed<Vector3D> boxedInitial = boxedVector3DPool.Get(initialVector3D);
Boxed<Vector3D> boxedTarget = boxedVector3DPool.Get(targetVector3D);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedVector3DPool.Return(boxedInitial);
boxedVector3DPool.Return(boxedTarget);
});
return tween;
}
}

View File

@ -0,0 +1,12 @@
using Engine.Core;
namespace Engine.Systems.Tween;
public class Boxed<T> where T : struct
{
public Event<Boxed<T>, BoxedValueChangedArguments> OnValueChanged { get; } = new();
public T Value { get; set; } = default;
public readonly record struct BoxedValueChangedArguments(T PreviousValue, T CurrentValue);
}

View File

@ -0,0 +1,14 @@
using Engine.Core;
namespace Engine.Systems.Tween;
public class BoxedPool<T>(int initialCapacity = 1) : Pool<Boxed<T>>(() => new(), initialCapacity) where T : struct;
public static class BoxedPoolExtensions
{
public static Boxed<T> Get<T>(this BoxedPool<T> boxedPool, T value) where T : struct
{
Boxed<T> boxed = boxedPool.Get();
boxed.Value = value;
return boxed;
}
}