Skip to content

Commit 1ba884b

Browse files
committed
Fixed bug that allocates garbage upon accessing entitydata.
1 parent 96146b0 commit 1ba884b

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

src/Arch/Core/EntityInfo.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Arch.Core;
1616
/// stores information about an <see cref="Entity"/> to quickly access its data and location.
1717
/// </summary>
1818
[SkipLocalsInit]
19-
public struct EntityData
19+
public struct EntityData : IEquatable<EntityData>
2020
{
2121
/// <summary>
2222
/// A reference to its <see cref="Archetype"/>.
@@ -31,7 +31,7 @@ public struct EntityData
3131
/// <summary>
3232
/// Its version.
3333
/// </summary>
34-
public int Version;
34+
public readonly int Version;
3535

3636
/// <summary>
3737
/// Initializes a new instance of the <see cref="EntityData"/> struct.
@@ -92,6 +92,41 @@ internal void Move(Archetype archetype, Slot slot)
9292
Archetype = archetype;
9393
Slot = slot;
9494
}
95+
96+
/// <summary>
97+
/// Returns true if its equal to the passed instance.
98+
/// </summary>
99+
/// <param name="other">The other instance.</param>
100+
/// <returns>True or false.</returns>
101+
public bool Equals(EntityData other)
102+
{
103+
return Version == other.Version && Archetype.Equals(other.Archetype) && Slot.Equals(other.Slot);
104+
}
105+
106+
/// <summary>
107+
/// Returns true if its equal to the passed instance.
108+
/// </summary>
109+
/// <param name="obj">The other instance.</param>
110+
/// <returns>True or false.</returns>
111+
public override bool Equals(object? obj)
112+
{
113+
return obj is EntityData other && Equals(other);
114+
}
115+
116+
/// <summary>
117+
/// Returns the hashcode of this instance.
118+
/// </summary>
119+
/// <returns>The hashcode.</returns>
120+
public override int GetHashCode()
121+
{
122+
unchecked
123+
{
124+
var hashCode = Archetype.GetHashCode();
125+
hashCode = (hashCode * 397) ^ Slot.GetHashCode();
126+
hashCode = (hashCode * 397) ^ Version;
127+
return hashCode;
128+
}
129+
}
95130
}
96131

97132
/// <summary>

src/Arch/Core/World.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,8 +1639,13 @@ public partial class World
16391639
[Pure]
16401640
public bool IsAlive(Entity entity)
16411641
{
1642+
if (entity.Version <= 0)
1643+
{
1644+
return false;
1645+
}
1646+
16421647
ref var entityData = ref EntityInfo.TryGetEntityData(entity.Id, out var entityDataExists);
1643-
return entity.Version > 0 && entityDataExists && entityData.Version == entity.Version;
1648+
return entityDataExists && entityData.Version == entity.Version;
16441649
}
16451650

16461651
/// <summary>
@@ -1652,8 +1657,14 @@ public bool IsAlive(Entity entity)
16521657
[Pure]
16531658
public ref EntityData IsAlive(Entity entity, out bool exists)
16541659
{
1660+
if (entity.Version <= 0)
1661+
{
1662+
exists = false;
1663+
return ref Unsafe.NullRef<EntityData>();
1664+
}
1665+
16551666
ref var entityData = ref EntityInfo.TryGetEntityData(entity.Id, out var entityDataExists);
1656-
exists = entity.Version > 0 && entityDataExists && entityData.Version == entity.Version;
1667+
exists = entityDataExists && entityData.Version == entity.Version;
16571668
return ref entityData;
16581669
}
16591670

0 commit comments

Comments
 (0)