Skip to content

Commit

Permalink
add UnsafeSetId for entity hosts & storages
Browse files Browse the repository at this point in the history
  • Loading branch information
sicusa committed Feb 21, 2024
1 parent e895662 commit e3f7650
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 38 deletions.
8 changes: 6 additions & 2 deletions Sia/Entities/DynEntityRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ private class EntityMover<TEntity>(IEntityCreator creator) : IEntityMover
var bundle = Bundle.Create(
current.UnsafeCast<TEntity>().AsRef(), newComponent);

var id = current.Slot.Id;
current.Dispose();
return (creator.CreateEntity(bundle),
new EntityMover<Bundle<TEntity, TComponent>>(creator));

var newEntity = creator.CreateEntity(bundle);
newEntity.Host.UnsafeSetId(newEntity.Slot, id);

return (newEntity, new EntityMover<Bundle<TEntity, TComponent>>(creator));
}
}

Expand Down
20 changes: 2 additions & 18 deletions Sia/Entities/Hosts/StorageEntityHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,68 +26,52 @@ public class StorageEntityHost<[DynamicallyAccessedMembers(DynamicallyAccessedMe

public TStorage Storage { get; } = managedStorage;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ContainsCommon<TComponent>()
=> Descriptor.GetOffset<TComponent>() != -1;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool ContainsCommon(Type componentType)
=> Descriptor.GetOffset(componentType) != -1;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
EntityRef IEntityHost.Create() => Create();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual EntityRef<T> Create()
=> new(Storage.AllocateSlot(), this);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual EntityRef<T> Create(in T initial)
=> new(Storage.AllocateSlot(initial), this);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Release(scoped in StorageSlot slot)
=> Storage.Release(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsValid(scoped in StorageSlot slot)
=> Storage.IsValid(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EntityDescriptor GetDescriptor(scoped in StorageSlot slot)
=> Descriptor;
public void UnsafeSetId(scoped in StorageSlot slot, int id)
=> Storage.UnsafeSetId(slot, id);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe ref byte GetByteRef(scoped in StorageSlot slot)
=> ref Unsafe.AsRef<byte>(Unsafe.AsPointer(ref Storage.GetRef(slot)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe ref byte UnsafeGetByteRef(scoped in StorageSlot slot)
=> ref Unsafe.AsRef<byte>(Unsafe.AsPointer(ref Storage.UnsafeGetRef(slot)));

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T GetRef(scoped in StorageSlot slot)
=> ref Storage.GetRef(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T UnsafeGetRef(scoped in StorageSlot slot)
=> ref Storage.UnsafeGetRef(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Box(scoped in StorageSlot slot)
=> Storage.GetRef(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IEnumerator<EntityRef> GetEnumerator()
{
foreach (var slot in Storage) {
yield return new(slot, this);
}
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
Storage.Dispose();
Expand Down
4 changes: 1 addition & 3 deletions Sia/Entities/Interfaces/IEntityHost.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance.Buffers;

namespace Sia;

public interface IEntityHost : IEnumerable<EntityRef>, IDisposable
Expand All @@ -20,6 +17,7 @@ public interface IEntityHost : IEnumerable<EntityRef>, IDisposable
ref byte GetByteRef(scoped in StorageSlot slot);
ref byte UnsafeGetByteRef(scoped in StorageSlot slot);

void UnsafeSetId(scoped in StorageSlot slot, int id);
object Box(scoped in StorageSlot slot);
}

Expand Down
5 changes: 4 additions & 1 deletion Sia/Storages/Common/StorageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Sia;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance;
using CommunityToolkit.HighPerformance.Buffers;

public abstract class StorageBase<T> : IStorage<T>
where T : struct
Expand Down Expand Up @@ -86,6 +85,10 @@ public ref T GetRef(scoped in StorageSlot slot)
public ref T UnsafeGetRef(scoped in StorageSlot slot)
=> ref GetRef(slot.Index);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UnsafeSetId(scoped in StorageSlot slot, int id)
=> _allocatedSlots.ValueSpan[slot.Index].Id = id;

protected abstract void Allocate(int slot);
protected abstract void Release(int slot);
protected abstract ref T GetRef(int slot);
Expand Down
6 changes: 3 additions & 3 deletions Sia/Storages/Common/UnversionedStorageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace Sia;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance.Buffers;

public abstract class UnversionedStorageBase<T> : IStorage<T>
where T : struct
Expand Down Expand Up @@ -50,11 +49,12 @@ public void Release(scoped in StorageSlot slot)
public bool IsValid(scoped in StorageSlot slot)
=> true;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UnsafeSetId(scoped in StorageSlot slot, int id)
=> _allocatedSlots.ValueSpan[slot.Index].Id = id;

public ref T GetRef(scoped in StorageSlot slot)
=> ref GetRef(slot.Index);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T UnsafeGetRef(scoped in StorageSlot slot)
=> ref GetRef(slot.Index);

Expand Down
3 changes: 2 additions & 1 deletion Sia/Storages/Interfaces/IStorage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Sia;

using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance.Buffers;

public interface IStorage : IEnumerable<StorageSlot>, IDisposable
{
Expand All @@ -12,6 +11,8 @@ public interface IStorage : IEnumerable<StorageSlot>, IDisposable
StorageSlot AllocateSlot();
void Release(scoped in StorageSlot slot);
bool IsValid(scoped in StorageSlot slot);

void UnsafeSetId(scoped in StorageSlot slot, int id);
}

public interface IStorage<T> : IStorage
Expand Down
3 changes: 3 additions & 0 deletions Sia/Systems/SystemLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void Release(in StorageSlot slot)
public bool IsValid(in StorageSlot slot)
=> Host.IsValid(slot);

public void UnsafeSetId(scoped in StorageSlot slot, int id)
=> Host.UnsafeSetId(slot, id);

public ref byte GetByteRef(scoped in StorageSlot slot)
=> ref Host.GetByteRef(slot);

Expand Down
13 changes: 3 additions & 10 deletions Sia/Worlds/WrappedWorldEntityHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Sia;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance.Buffers;

public sealed record WrappedWorldEntityHost<T, TEntityHost> : IEntityHost<T>, IReactiveEntityHost
where T : struct
Expand Down Expand Up @@ -72,34 +71,28 @@ public void Release(scoped in StorageSlot slot)
OnEntityReleased?.Invoke(entity);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsValid(scoped in StorageSlot slot)
=> _host.IsValid(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UnsafeSetId(scoped in StorageSlot slot, int id)
=> _host.UnsafeSetId(slot, id);

public unsafe ref byte GetByteRef(scoped in StorageSlot slot)
=> ref _host.GetByteRef(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe ref byte UnsafeGetByteRef(scoped in StorageSlot slot)
=> ref _host.UnsafeGetByteRef(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T GetRef(scoped in StorageSlot slot)
=> ref _host.GetRef(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref T UnsafeGetRef(scoped in StorageSlot slot)
=> ref _host.UnsafeGetRef(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Box(scoped in StorageSlot slot)
=> _host.Box(slot);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IEnumerator<EntityRef> GetEnumerator() => _host.GetEnumerator();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
IEnumerator IEnumerable.GetEnumerator() => _host.GetEnumerator();

public void Dispose() => _host.Dispose();
Expand Down

0 comments on commit e3f7650

Please sign in to comment.