feat: added list pools
This commit is contained in:
parent
3f914fe46f
commit
152b0e93db
40
Engine.Core/Helpers/ListPool.cs
Normal file
40
Engine.Core/Helpers/ListPool.cs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Syntriax.Engine.Core;
|
||||||
|
|
||||||
|
public class ListPool<T> : IPool<List<T>>
|
||||||
|
{
|
||||||
|
public Event<IPool<List<T>>, List<T>> OnReturned { get; } = new();
|
||||||
|
public Event<IPool<List<T>>, List<T>> OnRemoved { get; } = new();
|
||||||
|
|
||||||
|
private readonly Func<List<T>> generator = null!;
|
||||||
|
private readonly Queue<List<T>> queue = new();
|
||||||
|
|
||||||
|
public List<T> Get()
|
||||||
|
{
|
||||||
|
if (!queue.TryDequeue(out List<T>? result))
|
||||||
|
result = generator();
|
||||||
|
|
||||||
|
result.Clear();
|
||||||
|
OnRemoved?.Invoke(this, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Return(List<T> list)
|
||||||
|
{
|
||||||
|
if (queue.Contains(list))
|
||||||
|
return;
|
||||||
|
|
||||||
|
list.Clear();
|
||||||
|
queue.Enqueue(list);
|
||||||
|
OnReturned?.Invoke(this, list);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListPool(Func<List<T>> generator, int initialCapacity = 1)
|
||||||
|
{
|
||||||
|
this.generator = generator;
|
||||||
|
for (int i = 0; i < initialCapacity; i++)
|
||||||
|
queue.Enqueue(generator());
|
||||||
|
}
|
||||||
|
}
|
@ -25,6 +25,12 @@ public class PhysicsEngine2D : Behaviour, IPreUpdate, IPhysicsEngine2D
|
|||||||
protected BehaviourCollector<IRigidBody2D> rigidBodyCollector = new();
|
protected BehaviourCollector<IRigidBody2D> rigidBodyCollector = new();
|
||||||
protected BehaviourCollector<ICollider2D> colliderCollector = new();
|
protected BehaviourCollector<ICollider2D> colliderCollector = new();
|
||||||
|
|
||||||
|
private readonly ListPool<ICollider2D> colliderPool = new(() => new(32));
|
||||||
|
private readonly ListPool<IPrePhysicsUpdate> prePhysicsUpdatePool = new(() => new(32));
|
||||||
|
private readonly ListPool<IPhysicsUpdate> physicsUpdatePool = new(() => new(32));
|
||||||
|
private readonly ListPool<IPhysicsIteration> physicsIterationPool = new(() => new(32));
|
||||||
|
private readonly ListPool<IPostPhysicsUpdate> postPhysicsUpdatePool = new(() => new(32));
|
||||||
|
|
||||||
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
|
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
|
||||||
public float IterationPeriod { get => _iterationPeriod; set => _iterationPeriod = value.Max(0.0001f); }
|
public float IterationPeriod { get => _iterationPeriod; set => _iterationPeriod = value.Max(0.0001f); }
|
||||||
|
|
||||||
@ -111,11 +117,11 @@ public class PhysicsEngine2D : Behaviour, IPreUpdate, IPhysicsEngine2D
|
|||||||
{
|
{
|
||||||
float intervalDeltaTime = deltaTime / IterationPerStep;
|
float intervalDeltaTime = deltaTime / IterationPerStep;
|
||||||
|
|
||||||
List<ICollider2D> childColliders = [];
|
List<ICollider2D> childColliders = colliderPool.Get();
|
||||||
List<IPrePhysicsUpdate> physicsPreUpdates = [];
|
List<IPrePhysicsUpdate> physicsPreUpdates = prePhysicsUpdatePool.Get();
|
||||||
List<IPhysicsUpdate> physicsUpdates = [];
|
List<IPhysicsUpdate> physicsUpdates = physicsUpdatePool.Get();
|
||||||
List<IPhysicsIteration> physicsIterations = [];
|
List<IPhysicsIteration> physicsIterations = physicsIterationPool.Get();
|
||||||
List<IPostPhysicsUpdate> physicsPostUpdates = [];
|
List<IPostPhysicsUpdate> physicsPostUpdates = postPhysicsUpdatePool.Get();
|
||||||
|
|
||||||
rigidBody.BehaviourController.GetBehavioursInChildren(childColliders);
|
rigidBody.BehaviourController.GetBehavioursInChildren(childColliders);
|
||||||
rigidBody.BehaviourController.GetBehavioursInChildren(physicsPreUpdates);
|
rigidBody.BehaviourController.GetBehavioursInChildren(physicsPreUpdates);
|
||||||
@ -159,6 +165,12 @@ public class PhysicsEngine2D : Behaviour, IPreUpdate, IPhysicsEngine2D
|
|||||||
|
|
||||||
for (int i = physicsPostUpdates.Count - 1; i >= 0; i--)
|
for (int i = physicsPostUpdates.Count - 1; i >= 0; i--)
|
||||||
physicsPostUpdates[i].PostPhysicsUpdate(deltaTime);
|
physicsPostUpdates[i].PostPhysicsUpdate(deltaTime);
|
||||||
|
|
||||||
|
colliderPool.Return(childColliders);
|
||||||
|
prePhysicsUpdatePool.Return(physicsPreUpdates);
|
||||||
|
physicsUpdatePool.Return(physicsUpdates);
|
||||||
|
physicsIterationPool.Return(physicsIterations);
|
||||||
|
postPhysicsUpdatePool.Return(physicsPostUpdates);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ResolveColliders(ICollider2D colliderX, ICollider2D colliderY)
|
private void ResolveColliders(ICollider2D colliderX, ICollider2D colliderY)
|
||||||
|
@ -6,7 +6,7 @@ namespace Syntriax.Engine.Physics2D;
|
|||||||
|
|
||||||
public class RaycastResolver2D : IRaycastResolver2D
|
public class RaycastResolver2D : IRaycastResolver2D
|
||||||
{
|
{
|
||||||
private readonly Pool<List<Line2D>> lineCacheQueue = new(() => new List<Line2D>(4));
|
private readonly ListPool<Line2D> lineCacheQueue = new(() => new(4));
|
||||||
|
|
||||||
RaycastResult? IRaycastResolver2D.RaycastAgainst<T>(T shape, Ray2D ray, float length)
|
RaycastResult? IRaycastResolver2D.RaycastAgainst<T>(T shape, Ray2D ray, float length)
|
||||||
{
|
{
|
||||||
@ -21,7 +21,6 @@ public class RaycastResolver2D : IRaycastResolver2D
|
|||||||
public RaycastResult? RaycastAgainstShape(IShapeCollider2D shapeCollider, Ray2D ray, float length)
|
public RaycastResult? RaycastAgainstShape(IShapeCollider2D shapeCollider, Ray2D ray, float length)
|
||||||
{
|
{
|
||||||
List<Line2D> line2Ds = lineCacheQueue.Get();
|
List<Line2D> line2Ds = lineCacheQueue.Get();
|
||||||
line2Ds.Clear();
|
|
||||||
|
|
||||||
RaycastResult? raycastResult = null;
|
RaycastResult? raycastResult = null;
|
||||||
float closestRaycastResultSquared = float.MaxValue;
|
float closestRaycastResultSquared = float.MaxValue;
|
||||||
@ -60,7 +59,6 @@ public class RaycastResolver2D : IRaycastResolver2D
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
line2Ds.Clear();
|
|
||||||
lineCacheQueue.Return(line2Ds);
|
lineCacheQueue.Return(line2Ds);
|
||||||
|
|
||||||
return raycastResult;
|
return raycastResult;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user