diff --git a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs index 5c46875..e659df5 100644 --- a/Svelto.ECS/DataStructures/TypeSafeDictionary.cs +++ b/Svelto.ECS/DataStructures/TypeSafeDictionary.cs @@ -32,7 +32,7 @@ namespace Svelto.ECS.Internal int Count { get; } } - class TypeSafeDictionary : FasterDictionary, ITypeSafeDictionary where TValue : IEntityData + class TypeSafeDictionary : FasterDictionary, ITypeSafeDictionary where TValue : IEntityStruct { public TypeSafeDictionary(int size):base(size) {} @@ -109,11 +109,6 @@ namespace Svelto.ECS.Internal } } - public void AddCapacity(int size) - { - throw new NotImplementedException(); - } - public ITypeSafeDictionary Create() { return new TypeSafeDictionary(); diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 647c43a..8c02da9 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -61,44 +61,48 @@ namespace Svelto.ECS ///-------------------------------------------- - /// - /// This function is experimental and untested. I never used it in production - /// it may not be necessary. - /// TODO: understand if this method is useful in a performance critical - /// scenario - /// void Preallocate(int groupID, int size) where T : class, IEntityDescriptor, new() { var entityViewsToBuild = EntityDescriptorTemplate.Info.entityViewsToBuild; var count = entityViewsToBuild.Length; + //reserve space in the database + Dictionary @group; + if (_groupEntityDB.TryGetValue(groupID, out group) == false) + group = _groupEntityDB[groupID] = new Dictionary(); + + //reserve space in building buffer + Dictionary @groupBuffer; + if (_groupedEntityToAdd.current.TryGetValue(groupID, out @groupBuffer) == false) + @groupBuffer = _groupedEntityToAdd.current[groupID] = new Dictionary(); + + ITypeSafeDictionary dbList; + for (var index = 0; index < count; index++) { var entityViewBuilder = entityViewsToBuild[index]; var entityViewType = entityViewBuilder.GetEntityType(); - //reserve space for the global pool - ITypeSafeDictionary dbList; - - //reserve space for the single group - Dictionary @group; - if (_groupEntityDB.TryGetValue(groupID, out group) == false) - group = _groupEntityDB[groupID] = new Dictionary(); - if (group.TryGetValue(entityViewType, out dbList) == false) group[entityViewType] = entityViewBuilder.Preallocate(ref dbList, size); else dbList.AddCapacity(size); - if (_groupedEntityToAdd.current.TryGetValue(groupID, out group) == false) - group = _groupEntityDB[groupID] = new Dictionary(); - - //reserve space to the temporary buffer - if (group.TryGetValue(entityViewType, out dbList) == false) - group[entityViewType] = entityViewBuilder.Preallocate(ref dbList, size); + if (@groupBuffer.TryGetValue(entityViewType, out dbList) == false) + @groupBuffer[entityViewType] = entityViewBuilder.Preallocate(ref dbList, size); else dbList.AddCapacity(size); } + + if (group.TryGetValue(_typeEntityInfoView, out dbList) == false) + group[_typeEntityInfoView] = EntityViewBuilder.Preallocate(ref dbList, size); + else + dbList.AddCapacity(size); + + if (@groupBuffer.TryGetValue(_typeEntityInfoView, out dbList) == false) + @groupBuffer[_typeEntityInfoView] = EntityViewBuilder.Preallocate(ref dbList, size); + else + dbList.AddCapacity(size); } ///-------------------------------------------- @@ -161,26 +165,24 @@ namespace Svelto.ECS _groupEntityDB.Add(toGroupID, groupedEntityViewsTyped); } - ITypeSafeDictionary toSafeList; + ITypeSafeDictionary toSafeDic; for (var i = 0; i < entityViewBuildersCount; i++) { var entityViewBuilder = entityViewBuilders[i]; var entityViewType = entityViewBuilder.GetEntityType(); - var fromSafeList = groupedEntities[entityViewType]; - if (groupedEntityViewsTyped.TryGetValue(entityViewType, out toSafeList) == false) - groupedEntityViewsTyped[entityViewType] = toSafeList = fromSafeList.Create(); + var fromSafeDic = groupedEntities[entityViewType]; + if (groupedEntityViewsTyped.TryGetValue(entityViewType, out toSafeDic) == false) + groupedEntityViewsTyped[entityViewType] = toSafeDic = fromSafeDic.Create(); - entityViewBuilder.MoveEntityView(entityegid, toGroupID, fromSafeList, toSafeList); - fromSafeList.Remove(entityegid.entityID); + entityViewBuilder.MoveEntityView(entityegid, toGroupID, fromSafeDic, toSafeDic); } - if (groupedEntityViewsTyped.TryGetValue(_typeEntityInfoView, out toSafeList) == false) - groupedEntityViewsTyped[_typeEntityInfoView] = toSafeList = entityInfoViewDictionary.Create(); + if (groupedEntityViewsTyped.TryGetValue(_typeEntityInfoView, out toSafeDic) == false) + groupedEntityViewsTyped[_typeEntityInfoView] = toSafeDic = entityInfoViewDictionary.Create(); - EntityViewBuilder.MoveEntityView(entityegid, toGroupID, entityInfoViewDictionary, toSafeList); - entityInfoViewDictionary.Remove(entityegid.entityID); + EntityViewBuilder.MoveEntityView(entityegid, toGroupID, entityInfoViewDictionary, toSafeDic); } EGID SwapFirstEntityGroup(int fromGroupID, int toGroupId) @@ -208,7 +210,7 @@ namespace Svelto.ECS _id = id; } - public void Init(ref T initializer) where T: struct, IEntityData + public void Init(ref T initializer) where T: struct, IEntityStruct { var typeSafeDictionary = (TypeSafeDictionary) _current[typeof(T)]; diff --git a/Svelto.ECS/EntityViewBuilder.cs b/Svelto.ECS/EntityViewBuilder.cs index 3608d2c..4fba8b9 100644 --- a/Svelto.ECS/EntityViewBuilder.cs +++ b/Svelto.ECS/EntityViewBuilder.cs @@ -9,13 +9,13 @@ using Svelto.Utilities; namespace Svelto.ECS { - public class EntityViewBuilder : IEntityViewBuilder where T : IEntityData, new() + public class EntityViewBuilder : IEntityViewBuilder where T : IEntityStruct, new() { public EntityViewBuilder() { _initializer = default(T); - -#if DEBUG && !PROFILER + +#if DEBUG && !PROFILER if (needsReflection == false && typeof(T) != typeof(EntityInfoView)) { var type = typeof(T); @@ -30,10 +30,10 @@ namespace Svelto.ECS if (field.FieldType.IsPrimitive == true || field.FieldType.IsValueType == true) continue; - throw new EntityStructException(); + throw new EntityStructException(field.FieldType); } } -#endif +#endif if (needsReflection == true) { EntityView.InitCache(); @@ -69,7 +69,12 @@ namespace Svelto.ECS } } - public ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary dictionary, int size) + ITypeSafeDictionary IEntityViewBuilder.Preallocate(ref ITypeSafeDictionary dictionary, int size) + { + return Preallocate(ref dictionary, size); + } + + public static ITypeSafeDictionary Preallocate(ref ITypeSafeDictionary dictionary, int size) { if (dictionary == null) dictionary = new TypeSafeDictionary(size); @@ -95,9 +100,9 @@ namespace Svelto.ECS var toCastedDic = toSafeDic as TypeSafeDictionary; var entity = fromCastedDic[entityID.entityID]; + fromCastedDic.Remove(entityID.entityID); entity.ID = new EGID(entityID.entityID, toGroupID); toCastedDic.Add(entityID.entityID, entity); - fromCastedDic.Remove(entityID.entityID); } static FasterList>> entityViewBlazingFastReflection @@ -114,7 +119,7 @@ namespace Svelto.ECS public class EntityStructException : Exception { - public EntityStructException():base("EntityStruct must contains only value types!") + public EntityStructException(Type fieldType):base("EntityStruct must contains only value types! " + fieldType.ToString()) {} } } \ No newline at end of file diff --git a/Svelto.ECS/EntityViewsDB.cs b/Svelto.ECS/EntityViewsDB.cs index 725d3d3..c994e43 100644 --- a/Svelto.ECS/EntityViewsDB.cs +++ b/Svelto.ECS/EntityViewsDB.cs @@ -12,12 +12,12 @@ namespace Svelto.ECS.Internal _groupEntityViewsDB = groupEntityViewsDB; } - public ReadOnlyCollectionStruct QueryEntityViews() where T:class, IEntityData + public ReadOnlyCollectionStruct QueryEntityViews() where T:class, IEntityStruct { return QueryEntityViews(ExclusiveGroups.StandardEntity); } - public ReadOnlyCollectionStruct QueryEntityViews(int @group) where T:class, IEntityData + public ReadOnlyCollectionStruct QueryEntityViews(int @group) where T:class, IEntityStruct { Dictionary entitiesInGroupPerType; @@ -31,12 +31,12 @@ namespace Svelto.ECS.Internal return (outList as TypeSafeDictionary).FasterValues; } - public T[] QueryEntities(out int count) where T : IEntityData + public T[] QueryEntities(out int count) where T : IEntityStruct { return QueryEntities(ExclusiveGroups.StandardEntity, out count); } - public T[] QueryEntities(int @group, out int count) where T : IEntityData + public T[] QueryEntities(int @group, out int count) where T : IEntityStruct { count = 0; @@ -52,7 +52,7 @@ namespace Svelto.ECS.Internal return ((TypeSafeDictionary)typeSafeDictionary).GetFasterValuesBuffer(out count); } - public T[] QueryEntities(EGID entityGID, out uint index) where T : IEntityData + public T[] QueryEntities(EGID entityGID, out uint index) where T : IEntityStruct { TypeSafeDictionary casted; if (!FindSafeDictionary(entityGID, out casted)) @@ -74,7 +74,7 @@ namespace Svelto.ECS.Internal return QueryEntities(out count); } - public T QueryEntityView(EGID entityGID) where T : class, IEntityData + public T QueryEntityView(EGID entityGID) where T : class, IEntityStruct { T entityView; @@ -83,7 +83,7 @@ namespace Svelto.ECS.Internal return entityView; } - public void ExecuteOnEntity(EGID entityGID, ref W value, ActionRef action) where T : IEntityData + public void ExecuteOnEntity(EGID entityGID, ref W value, ActionRef action) where T : IEntityStruct { TypeSafeDictionary casted; if (!FindSafeDictionary(entityGID, out casted)) return; @@ -92,7 +92,7 @@ namespace Svelto.ECS.Internal casted.ExecuteOnEntityView(entityGID.entityID, ref value, action); } - public void ExecuteOnEntity(EGID entityGID, ActionRef action) where T : IEntityData + public void ExecuteOnEntity(EGID entityGID, ActionRef action) where T : IEntityStruct { TypeSafeDictionary casted; if (!FindSafeDictionary(entityGID, out casted)) return; @@ -101,7 +101,7 @@ namespace Svelto.ECS.Internal casted.ExecuteOnEntityView(entityGID.entityID, action); } - public bool Exists(EGID entityGID) where T : IEntityData + public bool Exists(EGID entityGID) where T : IEntityStruct { TypeSafeDictionary casted; if (!FindSafeDictionary(entityGID, out casted)) return false; @@ -115,7 +115,7 @@ namespace Svelto.ECS.Internal return false; } - bool FindSafeDictionary(EGID entityGID, out TypeSafeDictionary casted) where T : IEntityData + bool FindSafeDictionary(EGID entityGID, out TypeSafeDictionary casted) where T : IEntityStruct { var type = typeof(T); @@ -133,26 +133,26 @@ namespace Svelto.ECS.Internal return true; } - public bool HasAny() where T : IEntityData + public bool HasAny() where T : IEntityStruct { int count; QueryEntities(out count); return count > 0; } - public bool HasAny(int @group) where T : IEntityData + public bool HasAny(int @group) where T : IEntityStruct { int count; QueryEntities(group, out count); return count > 0; } - public bool TryQueryEntityView(EGID entityegid, out T entityView) where T : class, IEntityData + public bool TryQueryEntityView(EGID entityegid, out T entityView) where T : class, IEntityStruct { return TryQueryEntityViewInGroup(entityegid, out entityView); } - bool TryQueryEntityViewInGroup(EGID entityGID, out T entityView) where T:IEntityData + bool TryQueryEntityViewInGroup(EGID entityGID, out T entityView) where T:IEntityStruct { TypeSafeDictionary casted; if (!FindSafeDictionary(entityGID, out casted)) diff --git a/Svelto.ECS/GenericEntityDescriptor.cs b/Svelto.ECS/GenericEntityDescriptor.cs index 5a0620b..e0e60e6 100644 --- a/Svelto.ECS/GenericEntityDescriptor.cs +++ b/Svelto.ECS/GenericEntityDescriptor.cs @@ -1,6 +1,6 @@ namespace Svelto.ECS { - public abstract class GenericEntityDescriptor:IEntityDescriptor where T : IEntityData, new() + public abstract class GenericEntityDescriptor:IEntityDescriptor where T : IEntityStruct, new() { static GenericEntityDescriptor() { @@ -15,8 +15,8 @@ static readonly IEntityViewBuilder[] entityViewBuilders; } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() - where U : IEntityData, new() + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityStruct, new() + where U : IEntityStruct, new() { static GenericEntityDescriptor() { @@ -31,9 +31,9 @@ static readonly IEntityViewBuilder[] entityViewBuilders; } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() - where U : IEntityData, new() - where V : IEntityData, new() + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityStruct, new() + where U : IEntityStruct, new() + where V : IEntityStruct, new() { static GenericEntityDescriptor() { @@ -48,10 +48,10 @@ static readonly IEntityViewBuilder[] entityViewBuilders; } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() - where U : IEntityData, new() - where V : IEntityData, new() - where W : IEntityData, new() + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityStruct, new() + where U : IEntityStruct, new() + where V : IEntityStruct, new() + where W : IEntityStruct, new() { static GenericEntityDescriptor() { @@ -66,11 +66,11 @@ static readonly IEntityViewBuilder[] entityViewBuilders; } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() - where U : IEntityData, new() - where V : IEntityData, new() - where W : IEntityData, new() - where X : IEntityData, new() + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityStruct, new() + where U : IEntityStruct, new() + where V : IEntityStruct, new() + where W : IEntityStruct, new() + where X : IEntityStruct, new() { static GenericEntityDescriptor() { @@ -85,12 +85,12 @@ static readonly IEntityViewBuilder[] entityViewBuilders; } - public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityData, new() - where U : IEntityData, new() - where V : IEntityData, new() - where W : IEntityData, new() - where X : IEntityData, new() - where Y : IEntityData, new() + public abstract class GenericEntityDescriptor : IEntityDescriptor where T : IEntityStruct, new() + where U : IEntityStruct, new() + where V : IEntityStruct, new() + where W : IEntityStruct, new() + where X : IEntityStruct, new() + where Y : IEntityStruct, new() { static GenericEntityDescriptor() { diff --git a/Svelto.ECS/IEntityView.cs b/Svelto.ECS/IEntityView.cs index 87ed0f5..7707b7b 100644 --- a/Svelto.ECS/IEntityView.cs +++ b/Svelto.ECS/IEntityView.cs @@ -5,13 +5,15 @@ using Svelto.DataStructures; using Svelto.Utilities; namespace Svelto.ECS -{ - public interface IEntityData +{ + ///EntityStruct MUST implement IEntiyStruct + public interface IEntityStruct { EGID ID { get; set; } } - - public interface IEntityView:IEntityData + + ///EntityViews and EntityViewStructs MUST implement IEntityView + public interface IEntityView:IEntityStruct {} public class EntityView : IEntityView @@ -25,14 +27,14 @@ namespace Svelto.ECS EGID _ID; } - public struct EntityInfoView : IEntityData + public struct EntityInfoView : IEntityStruct { public EGID ID { get; set; } public IEntityViewBuilder[] entityToBuild; } - public static class EntityView where T: IEntityData, new() + public static class EntityView where T: IEntityStruct, new() { internal static readonly FasterList>> cachedFields; diff --git a/Svelto.ECS/IEntityViewsDB.cs b/Svelto.ECS/IEntityViewsDB.cs index e6f38db..ac7973b 100644 --- a/Svelto.ECS/IEntityViewsDB.cs +++ b/Svelto.ECS/IEntityViewsDB.cs @@ -7,25 +7,25 @@ namespace Svelto.ECS public interface IEntityViewsDB { //to use with EntityViews - ReadOnlyCollectionStruct QueryEntityViews() where T : class, IEntityData; - ReadOnlyCollectionStruct QueryEntityViews(int group) where T : class, IEntityData; + ReadOnlyCollectionStruct QueryEntityViews() where T : class, IEntityStruct; + ReadOnlyCollectionStruct QueryEntityViews(int group) where T : class, IEntityStruct; //to use with EntityViews, EntityStructs and EntityViewStructs - T[] QueryEntities(out int count) where T : IEntityData; - T[] QueryEntities(int group, out int count) where T : IEntityData; - T[] QueryEntities(EGID entityGID, out uint index) where T : IEntityData; + T[] QueryEntities(out int count) where T : IEntityStruct; + T[] QueryEntities(int group, out int count) where T : IEntityStruct; + T[] QueryEntities(EGID entityGID, out uint index) where T : IEntityStruct; //to use with EntityViews - bool TryQueryEntityView(EGID egid, out T entityView) where T : class, IEntityData; - T QueryEntityView(EGID egid) where T : class, IEntityData; + bool TryQueryEntityView(EGID egid, out T entityView) where T : class, IEntityStruct; + T QueryEntityView(EGID egid) where T : class, IEntityStruct; //to use with EntityViews, EntityStructs and EntityViewStructs - void ExecuteOnEntity(EGID egid, ref W value, ActionRef action) where T : IEntityData; - void ExecuteOnEntity(EGID egid, ActionRef action) where T : IEntityData; + void ExecuteOnEntity(EGID egid, ref W value, ActionRef action) where T : IEntityStruct; + void ExecuteOnEntity(EGID egid, ActionRef action) where T : IEntityStruct; - bool Exists(EGID egid) where T : IEntityData; + bool Exists(EGID egid) where T : IEntityStruct; - bool HasAny() where T:IEntityData; - bool HasAny(int group) where T:IEntityData; + bool HasAny() where T:IEntityStruct; + bool HasAny(int group) where T:IEntityStruct; } } \ No newline at end of file diff --git a/Svelto.ECS/MultiEntitiesEngine.cs b/Svelto.ECS/MultiEntitiesEngine.cs index 709bb8c..c0fb410 100644 --- a/Svelto.ECS/MultiEntitiesEngine.cs +++ b/Svelto.ECS/MultiEntitiesEngine.cs @@ -1,7 +1,7 @@ namespace Svelto.ECS { public abstract class MultiEntitiesEngine : SingleEntityEngine, IHandleEntityStructEngine - where U : IEntityData where T : IEntityData + where U : IEntityStruct where T : IEntityStruct { public void AddInternal(ref U entityView) { Add(ref entityView); } @@ -13,7 +13,7 @@ namespace Svelto.ECS } public abstract class MultiEntitiesEngine : MultiEntitiesEngine, IHandleEntityStructEngine - where V : IEntityData where U : IEntityData where T : IEntityData + where V : IEntityStruct where U : IEntityStruct where T : IEntityStruct { public void AddInternal(ref V entityView) { Add(ref entityView); } @@ -30,7 +30,7 @@ namespace Svelto.ECS /// already too many responsabilities. /// public abstract class MultiEntitiesEngine : MultiEntitiesEngine, IHandleEntityStructEngine - where W : IEntityData where V : IEntityData where U : IEntityData where T : IEntityData + where W : IEntityStruct where V : IEntityStruct where U : IEntityStruct where T : IEntityStruct { public void AddInternal(ref W entityView) { Add(ref entityView); } diff --git a/Svelto.ECS/MultiEntityViewsEngine.cs b/Svelto.ECS/MultiEntityViewsEngine.cs index 2b1b95b..0d82fcc 100644 --- a/Svelto.ECS/MultiEntityViewsEngine.cs +++ b/Svelto.ECS/MultiEntityViewsEngine.cs @@ -1,7 +1,7 @@ namespace Svelto.ECS { public abstract class MultiEntityViewsEngine : SingleEntityViewEngine, IHandleEntityStructEngine - where U : class, IEntityData where T : class, IEntityData + where U : class, IEntityStruct where T : class, IEntityStruct { public void AddInternal(ref U entityView) { Add(entityView); } @@ -13,7 +13,7 @@ namespace Svelto.ECS } public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine, IHandleEntityStructEngine - where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData + where V : class, IEntityStruct where U : class, IEntityStruct where T : class, IEntityStruct { public void AddInternal(ref V entityView) { Add(entityView); } @@ -30,7 +30,7 @@ namespace Svelto.ECS /// already too many responsabilities. /// public abstract class MultiEntityViewsEngine : MultiEntityViewsEngine, IHandleEntityStructEngine - where W : class, IEntityData where V : class, IEntityData where U : class, IEntityData where T : class, IEntityData + where W : class, IEntityStruct where V : class, IEntityStruct where U : class, IEntityStruct where T : class, IEntityStruct { public void AddInternal(ref W entityView) { Add(entityView); } diff --git a/Svelto.ECS/SingleEntityEngine.cs b/Svelto.ECS/SingleEntityEngine.cs index dbf0757..260ea3c 100644 --- a/Svelto.ECS/SingleEntityEngine.cs +++ b/Svelto.ECS/SingleEntityEngine.cs @@ -1,6 +1,6 @@ namespace Svelto.ECS { - public abstract class SingleEntityEngine : IHandleEntityStructEngine where T : IEntityData + public abstract class SingleEntityEngine : IHandleEntityStructEngine where T : IEntityStruct { public void AddInternal(ref T entityView) { Add(ref entityView); } diff --git a/Svelto.ECS/SingleEntityViewEngine.cs b/Svelto.ECS/SingleEntityViewEngine.cs index e3f1994..d023748 100644 --- a/Svelto.ECS/SingleEntityViewEngine.cs +++ b/Svelto.ECS/SingleEntityViewEngine.cs @@ -1,6 +1,6 @@ namespace Svelto.ECS { - public abstract class SingleEntityViewEngine : IHandleEntityStructEngine where T : class, IEntityData + public abstract class SingleEntityViewEngine : IHandleEntityStructEngine where T : class, IEntityStruct { public void AddInternal(ref T entityView) { Add(entityView); }