Skip to content

Commit 2e573e6

Browse files
[Core] Refactor to reduce GC pressure;
[Math] Add comparison operators to Rect/RectFloat;
1 parent e42a65a commit 2e573e6

File tree

19 files changed

+207
-163
lines changed

19 files changed

+207
-163
lines changed

Engine/Core/Audio/AudioSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public void Update()
233233

234234
Transform listenerTransform = null;
235235

236-
foreach((Entity _, Transform transform, AudioListener listener) in audioListeners)
236+
foreach((Entity _, Transform transform, AudioListener listener) in audioListeners.Contents)
237237
{
238238
listenerTransform ??= transform;
239239

Engine/Core/Entities/EntityQuery.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using Staple.Internal;
22
using System;
3-
using System.Collections;
4-
using System.Collections.Generic;
53

64
namespace Staple;
75

@@ -33,7 +31,7 @@ public enum EntityQueryMode
3331
/// It automatically updates as the world changes.
3432
/// </summary>
3533
/// <typeparam name="T">A type of component to get</typeparam>
36-
public sealed class EntityQuery<T> : ISceneQuery, IEnumerable<T>
34+
public sealed class EntityQuery<T> : ISceneQuery
3735
where T : IComponent
3836
{
3937
private T[] contents = [];
@@ -52,11 +50,21 @@ public sealed class EntityQuery<T> : ISceneQuery, IEnumerable<T>
5250
/// </summary>
5351
public T Content => content;
5452

53+
/// <summary>
54+
/// Contained content. Only valid if we have a single element.
55+
/// </summary>
56+
public T[] Contents => contents;
57+
5558
/// <summary>
5659
/// The content with its entity, if available.
5760
/// </summary>
5861
public (Entity, T) ContentEntity => contentEntity;
5962

63+
/// <summary>
64+
/// The content with its entity, if available.
65+
/// </summary>
66+
public (Entity, T)[] ContentEntities => contentEntities;
67+
6068
public T this[int index] => contents[index];
6169

6270
/// <summary>
@@ -168,20 +176,4 @@ public void WorldChanged()
168176
}
169177
}
170178
}
171-
172-
public IEnumerator<T> GetEnumerator()
173-
{
174-
foreach (var result in contents)
175-
{
176-
yield return result;
177-
}
178-
}
179-
180-
IEnumerator IEnumerable.GetEnumerator()
181-
{
182-
foreach (var result in contents)
183-
{
184-
yield return result;
185-
}
186-
}
187179
}

Engine/Core/Math/Rect.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using MessagePack;
2+
using System;
3+
using System.Diagnostics.CodeAnalysis;
24
using System.Numerics;
35

46
namespace Staple;
@@ -84,6 +86,32 @@ public readonly bool Contains(Vector2 v)
8486
v.Y >= top && v.Y <= bottom;
8587
}
8688

89+
public static bool operator==(Rect lhs, Rect rhs)
90+
{
91+
return lhs.left == rhs.left &&
92+
lhs.right == rhs.right &&
93+
lhs.top == rhs.top &&
94+
lhs.bottom == rhs.bottom;
95+
}
96+
97+
public static bool operator !=(Rect lhs, Rect rhs)
98+
{
99+
return lhs.left != rhs.left ||
100+
lhs.right != rhs.right ||
101+
lhs.top != rhs.top ||
102+
lhs.bottom != rhs.bottom;
103+
}
104+
105+
public override bool Equals([NotNullWhen(true)] object obj)
106+
{
107+
if(obj is Rect rhs)
108+
{
109+
return this == rhs;
110+
}
111+
112+
return false;
113+
}
114+
87115
public readonly bool ShouldSerializeIsEmpty() => false;
88116

89117
public readonly bool ShouldSerializeMin() => false;
@@ -93,4 +121,9 @@ public readonly bool Contains(Vector2 v)
93121
public readonly bool ShouldSerializeWidth() => false;
94122

95123
public readonly bool ShouldSerializeHeight() => false;
124+
125+
public override readonly int GetHashCode()
126+
{
127+
return HashCode.Combine(left, right, top, bottom);
128+
}
96129
}

Engine/Core/Math/RectFloat.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using MessagePack;
2+
using System;
3+
using System.Diagnostics.CodeAnalysis;
24
using System.Numerics;
35

46
namespace Staple;
@@ -69,11 +71,42 @@ public float Height
6971
}
7072
}
7173

74+
public static bool operator ==(RectFloat lhs, RectFloat rhs)
75+
{
76+
return lhs.left == rhs.left &&
77+
lhs.right == rhs.right &&
78+
lhs.top == rhs.top &&
79+
lhs.bottom == rhs.bottom;
80+
}
81+
82+
public static bool operator !=(RectFloat lhs, RectFloat rhs)
83+
{
84+
return lhs.left != rhs.left ||
85+
lhs.right != rhs.right ||
86+
lhs.top != rhs.top ||
87+
lhs.bottom != rhs.bottom;
88+
}
89+
90+
public override bool Equals([NotNullWhen(true)] object obj)
91+
{
92+
if (obj is RectFloat rhs)
93+
{
94+
return this == rhs;
95+
}
96+
97+
return false;
98+
}
99+
72100
public readonly bool ShouldSerializeMin() => false;
73101

74102
public readonly bool ShouldSerializeMax() => false;
75103

76104
public readonly bool ShouldSerializeWidth() => false;
77105

78106
public readonly bool ShouldSerializeHeight() => false;
107+
108+
public override readonly int GetHashCode()
109+
{
110+
return HashCode.Combine(left, right, top, bottom);
111+
}
79112
}

Engine/Core/Performance/PerformanceProfiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Staple;
66

7-
public sealed class PerformanceProfiler(PerformanceProfilerType type) : IDisposable
7+
public struct PerformanceProfiler(PerformanceProfilerType type) : IDisposable
88
{
99
private readonly PerformanceProfilerType type = type;
1010
private readonly DateTime startTime = DateTime.UtcNow;

Engine/Core/Rendering/Animation/SkinnedMeshAnimatorSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public sealed class SkinnedMeshAnimatorSystem : IRenderSystem
1010
{
1111
private static void ResetRenderers(SkinnedMeshAnimator animator)
1212
{
13-
foreach(var renderer in animator.renderers)
13+
foreach(var renderer in animator.renderers.Contents)
1414
{
1515
renderer.ResetAnimationState();
1616
}

Engine/Core/Rendering/Lighting/LightSystem.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,15 @@ public void ApplyLightProperties(Vector3 position, Matrix4x4 transform, Material
141141

142142
if (targets.Length > MaxLights)
143143
{
144-
targets = targets
145-
.OrderBy(x => Vector3.DistanceSquared(x.Item2.Position, position))
146-
.Take(MaxLights)
147-
.ToArray();
144+
static (Entity, Transform, Light)[] Trim((Entity, Transform, Light)[] targets, Vector3 position)
145+
{
146+
return targets
147+
.OrderBy(x => Vector3.DistanceSquared(x.Item2.Position, position))
148+
.Take(MaxLights)
149+
.ToArray();
150+
}
151+
152+
targets = Trim(targets, position);
148153
}
149154

150155
Matrix4x4.Invert(transform, out var invTransform);

Engine/Core/Rendering/Material.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,11 +633,11 @@ internal void ApplyProperties(ApplyMode applyMode)
633633
return;
634634
}
635635

636-
if(hasMainTexture)
636+
if(hasMainTexture && applyMode != ApplyMode.IgnoreTextures)
637637
{
638638
var t = mainTexture;
639639

640-
if (t.Disposed)
640+
if (t?.Disposed ?? true)
641641
{
642642
MainTexture = WhiteTexture;
643643
}

Engine/Core/Rendering/Mesh/MeshRenderSystem.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ public static void RenderMesh(Mesh mesh, Vector3 position, Quaternion rotation,
6464
mesh.UploadMeshData();
6565
}
6666

67-
var matrix = new Transform()
68-
{
69-
Position = position,
70-
LocalRotation = rotation,
71-
LocalScale = scale,
72-
}.Matrix;
67+
var matrix = Math.TransformationMatrix(position, scale, rotation);
7368

7469
bgfx.StateFlags state = bgfx.StateFlags.WriteRgb |
7570
bgfx.StateFlags.WriteA |

Engine/Core/Rendering/RenderSystem/RenderSystem+Internal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public void WorldChanged()
212212
{
213213
var collected = new Dictionary<IRenderSystem, List<(Entity, Transform, IComponent)>>();
214214

215-
foreach (var entityInfo in entityQuery)
215+
foreach (var entityInfo in entityQuery.Contents)
216216
{
217217
var layer = entityInfo.Item1.Layer;
218218

0 commit comments

Comments
 (0)