Skip to content

Commit

Permalink
Engine - Index: optimize - removed callvirt using StoreIndex.GetIndex()
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Jun 21, 2024
1 parent 0e71b07 commit f7424e9
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Engine/src/ECS/Archetype/StructHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal void RemoveIndex<TComponent>(EntityStoreBase store, int id)

private static ComponentIndex CreateIndex(EntityStoreBase store, int structIndex) {
var entityStore = (EntityStore)store;
return entityStore.extension.indexes[structIndex].GetIndex(entityStore);
return StoreIndex.GetIndex(entityStore, structIndex);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Engine/src/ECS/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public void EmitSignal<TEvent> (in TEvent ev) where TEvent : struct {
/// Executes in O(1) with default index. O(log n) when using <see cref="ValueInRangeIndex{TValue}"/>.
/// </summary>
internal Entities GetForeignEntities<TComponent>() where TComponent: struct, IIndexedComponent<Entity> {
var index = (ComponentIndex<Entity>)store.GetIndex(StructInfo<TComponent>.Index);
var index = (ComponentIndex<Entity>)StoreIndex.GetIndex(store, StructInfo<TComponent>.Index);
return index.GetHasValueEntities(this);
}
#endregion
Expand Down
6 changes: 1 addition & 5 deletions Engine/src/ECS/EntityStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,7 @@ public bool TryGetEntityByPid(long pid, out Entity value)
/// Executes in O(1) with default index. O(log n) when using <see cref="ValueInRangeIndex{TValue}"/>.
/// </summary>
internal Entities GetEntitiesWithComponentValue<TComponent, TValue>(TValue value) where TComponent: struct, IIndexedComponent<TValue> {
var index = (ComponentIndex<TValue>)GetIndex(StructInfo<TComponent>.Index);
var index = (ComponentIndex<TValue>)StoreIndex.GetIndex(this, StructInfo<TComponent>.Index);
return index.GetHasValueEntities(value);
}

internal ComponentIndex GetIndex(int index) {
return extension.indexes[index].GetIndex(this);
}
}
2 changes: 1 addition & 1 deletion Engine/src/ECS/Lab/Conditions/HasValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal HasValueCondition(TValue value) {

internal override void AddMatchingEntities(EntityStore store, HashSet<int> idSet)
{
var index = (ComponentIndex<TValue>)store.GetIndex(StructInfo<TComponent>.Index);
var index = (ComponentIndex<TValue>)StoreIndex.GetIndex(store, StructInfo<TComponent>.Index);
var entities = index.GetHasValueEntities(value);
foreach (var id in entities.Ids) {
idSet.Add(id);
Expand Down
2 changes: 1 addition & 1 deletion Engine/src/ECS/Lab/Conditions/ValueInRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal ValueInRangeCondition(TValue min, TValue max) {

internal override void AddMatchingEntities(EntityStore store, HashSet<int> idSet)
{
var index = (ComponentIndex<TValue>)store.GetIndex(StructInfo<TComponent>.Index);
var index = (ComponentIndex<TValue>)StoreIndex.GetIndex(store, StructInfo<TComponent>.Index);
index.AddValueInRangeEntities(min, max, idSet);
}
}
7 changes: 5 additions & 2 deletions Engine/src/ECS/Lab/Indexes/ComponentIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ internal StoreIndex(IndexedComponentType type) {
this.type = type;
}

internal ComponentIndex GetIndex(EntityStore store) {
internal static ComponentIndex GetIndex(EntityStore store, int structIndex)
{
var index = store.extension.indexes[structIndex].index;
if (index != null) {
return index;
}
return index = type.CreateComponentIndex(store);
ref var storeIndex = ref store.extension.indexes[structIndex];
return storeIndex.index = storeIndex.type.CreateComponentIndex(store);
}
}
2 changes: 1 addition & 1 deletion Engine/src/Tests-internal/ECS/Test_Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static void Test_Index_already_added()

entity.AddComponent(new IndexedName { name = "added" });

var index = (HasValueIndex<string>)world.GetIndex(StructInfo<IndexedName>.Index);
var index = (HasValueIndex<string>)StoreIndex.GetIndex(world, StructInfo<IndexedName>.Index);
index.Add(1, new IndexedName { name = "added" });
AreEqual(1, index.Count);
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/src/Tests-internal/ECS/Test_Index_Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static void Test_Index_Range_already_added()

entity.AddComponent(new IndexedIntRange { value = 456 });

var index = (ValueInRangeIndex<int>)world.GetIndex(StructInfo<IndexedIntRange>.Index);
var index = (ValueInRangeIndex<int>)StoreIndex.GetIndex(world, StructInfo<IndexedIntRange>.Index);
index.Add(1, new IndexedIntRange { value = 456 });
AreEqual(1, index.Count);
}
Expand Down

0 comments on commit f7424e9

Please sign in to comment.