Skip to content

Commit

Permalink
Refactored Edge creation and wrapped it into its own optimized method…
Browse files Browse the repository at this point in the history
… `World.GetOrCreateArchetypeEdge`.
  • Loading branch information
genaray committed May 16, 2023
1 parent b224328 commit 292ece0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/Arch/Core/Edges/Archetype.Edges.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Arch.Core.Utils;
using Arch.Core.Extensions.Internal;
using Arch.Core.Utils;

namespace Arch.Core;

Expand Down
35 changes: 35 additions & 0 deletions src/Arch/Core/Edges/World.Edges.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Arch.Core.Extensions.Internal;
using Arch.Core.Utils;

namespace Arch.Core;

public partial class World
{

/// <summary>
/// Creates or returns an <see cref="Archetype"/> based on the old one with one additional component.
/// Automatically creates a link between them for quick access.
/// </summary>
/// <param name="type">The new <see cref="ComponentType"/> that additionally forms a new <see cref="Archetype"/> with the old components of the old archetype.</param>
/// <param name="oldArchetype">The old <see cref="Archetype"/>.</param>
/// <returns>The cached or newly created <see cref="Archetype"/> with that additional component.</returns>
private Archetype GetOrCreateArchetypeByEdge(in ComponentType type, Archetype oldArchetype)
{
var edgeIndex = type.Id - 1;

#if NET5_0_OR_GREATER
var newArchetype = oldArchetype.CreateOrGetAddEdge(edgeIndex, out var exists);
if (!exists)
{
newArchetype = GetOrCreate(oldArchetype.Types.Add(type));
}
#else
if (!oldArchetype.TryGetAddEdge(edgeIndex, out var newArchetype))
{
newArchetype = GetOrCreate(oldArchetype.Types.Add(type));
oldArchetype.CreateAddEdge(edgeIndex, newArchetype);
}
#endif
return newArchetype;
}
}
6 changes: 2 additions & 4 deletions src/Arch/Core/Utils/ArrayDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ internal ArrayDictionary(int maxArraySize)
exists = !Equals(value, default(TValue));
return ref value;
}
else
{
return ref CollectionsMarshal.GetValueRefOrAddDefault(_dictionary, index, out exists)!;
}

return ref CollectionsMarshal.GetValueRefOrAddDefault(_dictionary, index, out exists)!;
}
#else
/// <summary>
Expand Down
32 changes: 2 additions & 30 deletions src/Arch/Core/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -909,21 +909,7 @@ internal void Add<T>(Entity entity, out Archetype newArchetype, out Slot slot)
{
var oldArchetype = EntityInfo.GetArchetype(entity.Id);
var type = Component<T>.ComponentType;
var edgeIndex = type.Id - 1;

#if NET5_0_OR_GREATER
newArchetype = oldArchetype.CreateOrGetAddEdge(edgeIndex, out var exists);
if (!exists)
{
newArchetype = GetOrCreate(oldArchetype.Types.Add(type));
}
#else
if (!oldArchetype.TryGetAddEdge(edgeIndex, out newArchetype))
{
newArchetype = GetOrCreate(oldArchetype.Types.Add(type));
oldArchetype.CreateAddEdge(edgeIndex, newArchetype);
}
#endif
newArchetype = GetOrCreateArchetypeByEdge(in type, oldArchetype);

Move(entity, oldArchetype, newArchetype, out slot);
OnComponentAdded<T>(entity);
Expand Down Expand Up @@ -1143,21 +1129,7 @@ public void Add(Entity entity, in object cmp)
{
var oldArchetype = EntityInfo.GetArchetype(entity.Id);
var type = (ComponentType) cmp.GetType();
var edgeIndex = type.Id - 1;

#if NET5_0_OR_GREATER
var newArchetype = oldArchetype.CreateOrGetAddEdge(edgeIndex, out var exists);
if (!exists)
{
newArchetype = GetOrCreate(oldArchetype.Types.Add(type));
}
#else
if (!oldArchetype.TryGetAddEdge(edgeIndex, out var newArchetype))
{
newArchetype = GetOrCreate(oldArchetype.Types.Add(type));
oldArchetype.CreateAddEdge(edgeIndex, newArchetype);
}
#endif
var newArchetype = GetOrCreateArchetypeByEdge(in type, oldArchetype);

Move(entity, oldArchetype, newArchetype, out var slot);
newArchetype.Set(ref slot, cmp);
Expand Down

0 comments on commit 292ece0

Please sign in to comment.