perf: DelegateExtensions.InvokeSafe marked obsolete for memory allocation reasons, soon to be removed

This commit is contained in:
2025-05-29 21:48:08 +03:00
parent 1b0f25e854
commit bf8fbebae3
35 changed files with 116 additions and 115 deletions

View File

@@ -61,12 +61,12 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
{
activeBehaviours.Add(behaviour);
OnBehaviourAdd(behaviour);
OnCollected?.InvokeSafe(this, behaviour);
OnCollected?.Invoke(this, behaviour);
}
else if (activeBehaviours.Remove(behaviour))
{
OnBehaviourRemove(behaviour);
OnRemoved?.InvokeSafe(this, behaviour);
OnRemoved?.Invoke(this, behaviour);
}
}
@@ -83,7 +83,7 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
if (activeBehaviours.Remove(tBehaviour))
{
OnBehaviourRemove(tBehaviour);
OnRemoved?.InvokeSafe(this, tBehaviour);
OnRemoved?.Invoke(this, tBehaviour);
}
}
@@ -99,7 +99,7 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
universe.OnUniverseObjectUnRegistered += OnUniverseObjectUnregistered;
Universe = universe;
OnUniverseAssigned?.InvokeSafe(this);
OnUniverseAssigned?.Invoke(this);
return true;
}
@@ -116,7 +116,7 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
Universe.OnUniverseObjectUnRegistered -= OnUniverseObjectUnregistered;
Universe = null!;
OnUnassigned?.InvokeSafe(this);
OnUnassigned?.Invoke(this);
return true;
}

View File

@@ -33,7 +33,7 @@ public abstract class BaseEntity : IEntity
string previousId = _id;
_id = value;
OnIdChanged?.InvokeSafe(this, previousId);
OnIdChanged?.Invoke(this, previousId);
}
}
@@ -47,9 +47,9 @@ public abstract class BaseEntity : IEntity
_initialized = value;
if (value)
OnInitialized?.InvokeSafe(this);
OnInitialized?.Invoke(this);
else
OnFinalized?.InvokeSafe(this);
OnFinalized?.Invoke(this);
}
}
@@ -62,7 +62,7 @@ public abstract class BaseEntity : IEntity
_stateEnable = stateEnable;
_stateEnable.Assign(this);
OnAssign(stateEnable);
OnStateEnableAssigned?.InvokeSafe(this);
OnStateEnableAssigned?.Invoke(this);
return true;
}
@@ -76,7 +76,7 @@ public abstract class BaseEntity : IEntity
_stateEnable = null!;
_stateEnable.Unassign();
OnUnassigned?.InvokeSafe(this);
OnUnassigned?.Invoke(this);
return true;
}

View File

@@ -21,7 +21,7 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour
int previousPriority = _priority;
_priority = value;
OnPriorityChanged?.InvokeSafe(this, previousPriority);
OnPriorityChanged?.Invoke(this, previousPriority);
}
}
@@ -39,7 +39,7 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour
behaviourController.OnUniverseObjectAssigned += OnUniverseObjectAssigned;
if (behaviourController.UniverseObject is not null)
OnUniverseObjectAssigned(behaviourController);
OnBehaviourControllerAssigned?.InvokeSafe(this);
OnBehaviourControllerAssigned?.Invoke(this);
return true;
}
@@ -79,6 +79,6 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour
_isActive = StateEnable.Enabled && _behaviourController.UniverseObject.IsActive;
if (previousActive != IsActive)
OnActiveChanged?.InvokeSafe(this, previousActive);
OnActiveChanged?.Invoke(this, previousActive);
}
}

View File

@@ -48,7 +48,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
behaviours.Add(tBehaviour);
OnBehaviourAdd(behaviour);
OnCollected?.InvokeSafe(this, tBehaviour);
OnCollected?.Invoke(this, tBehaviour);
}
protected virtual void OnBehaviourRemove(IBehaviour behaviour) { }
@@ -61,7 +61,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
return;
OnBehaviourRemove(behaviour);
OnRemoved?.InvokeSafe(this, tBehaviour);
OnRemoved?.Invoke(this, tBehaviour);
}
protected virtual void OnAssign(IUniverse universe) { }
@@ -78,7 +78,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
Universe = universe;
OnAssign(universe);
OnUniverseAssigned?.InvokeSafe(this);
OnUniverseAssigned?.Invoke(this);
return true;
}
@@ -95,7 +95,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
Universe.OnUniverseObjectUnRegistered -= OnUniverseObjectUnregistered;
Universe = null!;
OnUnassigned?.InvokeSafe(this);
OnUnassigned?.Invoke(this);
return true;
}

View File

@@ -27,7 +27,7 @@ public class BehaviourController : BaseEntity, IBehaviourController
if (IsInitialized)
behaviour.Initialize();
behaviour.OnPriorityChanged += OnPriorityChange;
OnBehaviourAdded?.InvokeSafe(this, behaviour);
OnBehaviourAdded?.Invoke(this, behaviour);
return behaviour;
}
@@ -95,7 +95,7 @@ public class BehaviourController : BaseEntity, IBehaviourController
behaviour.OnPriorityChanged -= OnPriorityChange;
behaviour.Finalize();
behaviours.Remove(behaviour);
OnBehaviourRemoved?.InvokeSafe(this, behaviour);
OnBehaviourRemoved?.Invoke(this, behaviour);
}
protected virtual void OnAssign(IUniverseObject universeObject) { }
@@ -106,7 +106,7 @@ public class BehaviourController : BaseEntity, IBehaviourController
_universeObject = universeObject;
OnAssign(universeObject);
OnUniverseObjectAssigned?.InvokeSafe(this);
OnUniverseObjectAssigned?.Invoke(this);
return true;
}

View File

@@ -4,6 +4,7 @@ namespace Syntriax.Engine.Core;
public static class DelegateExtensions
{
[Obsolete($"{nameof(InvokeSafe)} causes memory allocation, please use Invoke() instead.")]
public static void InvokeSafe(this Delegate @delegate, params object?[] args)
{
foreach (Delegate invocation in Delegate.EnumerateInvocationList(@delegate))

View File

@@ -19,10 +19,10 @@ public class ProgressionTracker : IProgressionTracker
Progression = progression.Clamp(Progression, 1f);
Status = status;
OnUpdated?.InvokeSafe(this, previousProgression, previousStatus);
OnUpdated?.Invoke(this, previousProgression, previousStatus);
if (progression >= 1f)
OnEnded?.InvokeSafe(this);
OnEnded?.Invoke(this);
}
void IProgressionTracker.Reset()

View File

@@ -35,7 +35,7 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
foreach (Vector2D vertex in value)
_vertices.Add(vertex);
OnShapeUpdated?.InvokeSafe(this);
OnShapeUpdated?.Invoke(this);
}
}
@@ -229,7 +229,7 @@ public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
for (int i = 0; i < count; i++)
to._vertices.Add(transform.Transform(from[i]));
to.OnShapeUpdated?.InvokeSafe(to);
to.OnShapeUpdated?.Invoke(to);
}
/// <summary>

View File

@@ -14,7 +14,7 @@ public class EntityRegistry
public void Add(IEntity entity)
{
if (registeredEntities.TryAdd(entity.Id, entity))
OnEntityRegistered?.InvokeSafe(this, entity);
OnEntityRegistered?.Invoke(this, entity);
}
public void QueueAssign(string id, Action<IEntity> setMethod)
@@ -26,7 +26,7 @@ public class EntityRegistry
public void AssignAll()
{
foreach ((string id, Action<IEntity>? action) in assignCallbacks)
action?.InvokeSafe(registeredEntities[id]);
action?.Invoke(registeredEntities[id]);
}
public void Reset()

View File

@@ -21,7 +21,7 @@ public class StateEnable : IStateEnable
bool previousState = _enabled;
_enabled = value;
OnEnabledChanged?.InvokeSafe(this, previousState);
OnEnabledChanged?.Invoke(this, previousState);
}
}
@@ -33,7 +33,7 @@ public class StateEnable : IStateEnable
_entity = entity;
OnAssign(entity);
OnEntityAssigned?.InvokeSafe(this);
OnEntityAssigned?.Invoke(this);
return true;
}
@@ -43,7 +43,7 @@ public class StateEnable : IStateEnable
return false;
_entity = null!;
OnUnassigned?.InvokeSafe(this);
OnUnassigned?.Invoke(this);
return true;
}
}

View File

@@ -31,7 +31,7 @@ public class Transform2D : Behaviour, ITransform2D
_position = value;
UpdateLocalPosition();
OnPositionChanged?.InvokeSafe(this, previousPosition);
OnPositionChanged?.Invoke(this, previousPosition);
}
}
@@ -47,7 +47,7 @@ public class Transform2D : Behaviour, ITransform2D
_scale = value;
UpdateLocalScale();
OnScaleChanged?.InvokeSafe(this, previousScale);
OnScaleChanged?.Invoke(this, previousScale);
}
}
@@ -63,7 +63,7 @@ public class Transform2D : Behaviour, ITransform2D
_rotation = value;
UpdateLocalRotation();
OnRotationChanged?.InvokeSafe(this, previousRotation);
OnRotationChanged?.Invoke(this, previousRotation);
}
}
@@ -79,7 +79,7 @@ public class Transform2D : Behaviour, ITransform2D
_localPosition = value;
UpdatePosition();
OnPositionChanged?.InvokeSafe(this, previousPosition);
OnPositionChanged?.Invoke(this, previousPosition);
}
}
@@ -97,8 +97,8 @@ public class Transform2D : Behaviour, ITransform2D
UpdateScale();
UpdatePosition();
OnScaleChanged?.InvokeSafe(this, previousScale);
OnPositionChanged?.InvokeSafe(this, previousPosition);
OnScaleChanged?.Invoke(this, previousScale);
OnPositionChanged?.Invoke(this, previousPosition);
}
}
@@ -114,7 +114,7 @@ public class Transform2D : Behaviour, ITransform2D
_localRotation = value;
UpdateRotation();
OnRotationChanged?.InvokeSafe(this, previousRotation);
OnRotationChanged?.Invoke(this, previousRotation);
}
}
@@ -125,7 +125,7 @@ public class Transform2D : Behaviour, ITransform2D
UpdatePosition();
OnPositionChanged?.InvokeSafe(this, previousPosition);
OnPositionChanged?.Invoke(this, previousPosition);
}
private void RecalculateScale(ITransform2D _, Vector2D previousScale)
@@ -138,8 +138,8 @@ public class Transform2D : Behaviour, ITransform2D
UpdateScale();
UpdatePosition();
OnScaleChanged?.InvokeSafe(this, previousScale);
OnPositionChanged?.InvokeSafe(this, previousPosition);
OnScaleChanged?.Invoke(this, previousScale);
OnPositionChanged?.Invoke(this, previousPosition);
}
private void RecalculateRotation(ITransform2D _, float previousRotation)
@@ -152,8 +152,8 @@ public class Transform2D : Behaviour, ITransform2D
UpdateRotation();
UpdatePosition();
OnRotationChanged?.InvokeSafe(this, previousRotation);
OnPositionChanged?.InvokeSafe(this, previousPosition);
OnRotationChanged?.Invoke(this, previousRotation);
OnPositionChanged?.Invoke(this, previousPosition);
}
private void UpdateLocalPosition()
@@ -247,9 +247,9 @@ public class Transform2D : Behaviour, ITransform2D
UpdateLocalScale();
UpdateLocalRotation();
OnPositionChanged?.InvokeSafe(this, Position);
OnScaleChanged?.InvokeSafe(this, Scale);
OnRotationChanged?.InvokeSafe(this, Rotation);
OnPositionChanged?.Invoke(this, Position);
OnScaleChanged?.Invoke(this, Scale);
OnRotationChanged?.Invoke(this, Rotation);
}
private void LookForTransform2D(IBehaviourController sender, IBehaviour behaviourAdded)

View File

@@ -37,7 +37,7 @@ public class Universe : BaseEntity, IUniverse
float previousTimeScale = _timeScale;
_timeScale = value;
OnTimeScaleChanged?.InvokeSafe(this, previousTimeScale);
OnTimeScaleChanged?.Invoke(this, previousTimeScale);
}
}
@@ -60,7 +60,7 @@ public class Universe : BaseEntity, IUniverse
if (!universeObject.EnterUniverse(this))
throw new Exception($"{universeObject.Name} can't enter the universe");
OnUniverseObjectRegistered?.InvokeSafe(this, universeObject);
OnUniverseObjectRegistered?.Invoke(this, universeObject);
}
public T InstantiateUniverseObject<T>(params object?[]? args) where T : class, IUniverseObject
@@ -95,7 +95,7 @@ public class Universe : BaseEntity, IUniverse
if (!universeObject.Finalize())
throw new Exception($"{universeObject.Name} can't be finalized");
OnUniverseObjectUnRegistered?.InvokeSafe(this, universeObject);
OnUniverseObjectUnRegistered?.Invoke(this, universeObject);
}
protected override void InitializeInternal()
@@ -118,18 +118,18 @@ public class Universe : BaseEntity, IUniverse
UnscaledTime = engineTime;
Time = new(TimeSpan.FromTicks((long)(Time.TimeSinceStart.Ticks + engineTime.DeltaSpan.Ticks * TimeScale)), TimeSpan.FromTicks((long)(engineTime.DeltaSpan.Ticks * TimeScale)));
OnPreUpdate?.InvokeSafe(this, Time);
OnUpdate?.InvokeSafe(this, Time);
OnPostUpdate?.InvokeSafe(this, Time);
OnPreUpdate?.Invoke(this, Time);
OnUpdate?.Invoke(this, Time);
OnPostUpdate?.Invoke(this, Time);
}
public void Draw()
{
Debug.Assert.AssertInitialized(this);
OnPreDraw?.InvokeSafe(this);
OnDraw?.InvokeSafe(this);
OnPostDraw?.InvokeSafe(this);
OnPreDraw?.Invoke(this);
OnDraw?.Invoke(this);
OnPostDraw?.Invoke(this);
}
private void OnUniverseObjectFinalize(IInitializable initializable)

View File

@@ -37,7 +37,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
string previousName = _name;
_name = value;
OnNameChanged?.InvokeSafe(this, previousName);
OnNameChanged?.Invoke(this, previousName);
}
}
@@ -50,7 +50,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
_universe = universe;
UpdateActive();
OnEnteringUniverse(universe);
OnEnteredUniverse?.InvokeSafe(this, universe);
OnEnteredUniverse?.Invoke(this, universe);
return true;
}
@@ -62,7 +62,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
OnExitingUniverse(universe);
_universe = null!;
OnExitedUniverse?.InvokeSafe(this, universe);
OnExitedUniverse?.Invoke(this, universe);
return true;
}
@@ -93,7 +93,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
}
UpdateActive();
OnParentChanged?.InvokeSafe(this, previousParent, parent);
OnParentChanged?.Invoke(this, previousParent, parent);
}
public void AddChild(IUniverseObject parent)
@@ -103,7 +103,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
_children.Add(parent);
parent.SetParent(this);
OnChildrenAdded?.InvokeSafe(this, parent);
OnChildrenAdded?.Invoke(this, parent);
}
public void RemoveChild(IUniverseObject child)
@@ -112,7 +112,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
return;
child.SetParent(null);
OnChildrenRemoved?.InvokeSafe(this, child);
OnChildrenRemoved?.Invoke(this, child);
}
protected virtual void OnAssign(IBehaviourController behaviourController) { }
@@ -123,7 +123,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
_behaviourController = behaviourController;
OnAssign(behaviourController);
OnBehaviourControllerAssigned?.InvokeSafe(this);
OnBehaviourControllerAssigned?.Invoke(this);
return true;
}
@@ -143,7 +143,7 @@ public class UniverseObject : BaseEntity, IUniverseObject
_isActive = StateEnable.Enabled && (Parent?.IsActive ?? true);
if (previousActive != IsActive)
OnActiveChanged?.InvokeSafe(this, previousActive);
OnActiveChanged?.Invoke(this, previousActive);
}
protected override void UnassignInternal()