From 5179c885f7866fb8bafe05bd134ab2c0d906badc Mon Sep 17 00:00:00 2001 From: sebas77 Date: Sat, 31 Mar 2018 00:58:31 +0100 Subject: [PATCH] some optimizations --- Svelto.ECS/EnginesRootEngines.cs | 2 ++ Svelto.ECS/EnginesRootEntities.cs | 41 ++++++++++++++----------------- Svelto.ECS/EntityFactory.cs | 24 ++++++------------ Svelto.ECS/EntityInfoView.cs | 7 ------ 4 files changed, 27 insertions(+), 47 deletions(-) delete mode 100644 Svelto.ECS/EntityInfoView.cs diff --git a/Svelto.ECS/EnginesRootEngines.cs b/Svelto.ECS/EnginesRootEngines.cs index d16b3ac..97f7bc7 100644 --- a/Svelto.ECS/EnginesRootEngines.cs +++ b/Svelto.ECS/EnginesRootEngines.cs @@ -41,6 +41,8 @@ namespace Svelto.ECS _groupEntityViewsDB = new Dictionary>(); _globalEntityViewsDBDic = new Dictionary(); + _entityInfos = new Dictionary(); + _groupedEntityViewsToAdd = new DoubleBufferedEntityViews>>(); _DB = new EntityViewsDB(_globalEntityViewsDB, _globalEntityViewsDBDic, _groupEntityViewsDB); diff --git a/Svelto.ECS/EnginesRootEntities.cs b/Svelto.ECS/EnginesRootEntities.cs index ecac7b2..620a29c 100644 --- a/Svelto.ECS/EnginesRootEntities.cs +++ b/Svelto.ECS/EnginesRootEntities.cs @@ -61,6 +61,7 @@ namespace Svelto.ECS EntityFactory.BuildGroupedEntityViews(entityID, groupID, _groupedEntityViewsToAdd.current, EntityDescriptorTemplate.Default, + _entityInfos, implementors); } @@ -69,7 +70,9 @@ namespace Svelto.ECS { EntityFactory.BuildGroupedEntityViews(entityID, groupID, _groupedEntityViewsToAdd.current, - entityDescriptor, implementors); + entityDescriptor, + _entityInfos, + implementors); } ///-------------------------------------------- @@ -127,28 +130,23 @@ namespace Svelto.ECS void RemoveEntity(EGID entityGID) { - EntityInfoView entityInfoView; - - _DB.TryQueryEntityView(entityGID, out entityInfoView); - - var entityViewBuilders = entityInfoView.entityViews; - EGID id = entityInfoView._ID; + var entityViewBuilders = _entityInfos[entityGID.GID]; var entityViewBuildersCount = entityViewBuilders.Length; - var group = _groupEntityViewsDB[id.group]; + var group = _groupEntityViewsDB[entityGID.group]; //for each entity view generated by the entity descriptor for (var i = 0; i < entityViewBuildersCount; i++) { var entityViewType = entityViewBuilders[i].GetEntityViewType(); - InternalRemoveEntityViewFromDBDicAndEngines(entityViewType, id); + InternalRemoveEntityViewFromDBDicAndEngines(entityViewType, entityGID); - RemoveEntityViewFromDB(@group, entityViewType, id); - RemoveEntityViewFromDB(@_globalEntityViewsDB, entityViewType, id); + RemoveEntityViewFromDB(@group, entityViewType, entityGID); + RemoveEntityViewFromDB(@_globalEntityViewsDB, entityViewType, entityGID); } - InternalRemoveEntityViewFromDBDicAndEngines(_entityInfoViewType, id); + _entityInfos.Remove(entityGID.GID); } static void RemoveEntityViewFromDB(Dictionary @group, Type entityViewType, EGID id) @@ -233,11 +231,8 @@ namespace Svelto.ECS Check.Require(fromGroupID != toGroupID, "can't move an entity to the same group where it already belongs to"); - EntityInfoView entityInfoView; - - _DB.TryQueryEntityView(new EGID(fromGroupID, entityID), out entityInfoView); - - var entityViewBuilders = entityInfoView.entityViews; + var entityegid = new EGID(entityID, fromGroupID); + var entityViewBuilders = _entityInfos[entityegid.GID]; var entityViewBuildersCount = entityViewBuilders.Length; var groupedEntities = _groupEntityViewsDB[fromGroupID]; @@ -261,13 +256,13 @@ namespace Svelto.ECS if (groupedEntityViewsTyped.TryGetValue(entityViewType, out toSafeList) == false) groupedEntityViewsTyped[entityViewType] = toSafeList = fromSafeList.Create(); - entityViewBuilder.MoveEntityView(entityInfoView._ID, fromSafeList, toSafeList); - fromSafeList.MappedRemove(entityInfoView._ID); - entityInfoView._ID = new EGID(entityID, toGroupID); + entityViewBuilder.MoveEntityView(entityegid, fromSafeList, toSafeList); + fromSafeList.MappedRemove(entityegid); } - } - readonly Type _entityInfoViewType = typeof(EntityInfoView); + _entityInfos.Remove(entityegid.GID); + _entityInfos.Add(new EGID(entityID, toGroupID).GID, entityViewBuilders); + } readonly EntityViewsDB _DB; @@ -283,6 +278,6 @@ namespace Svelto.ECS //indexable entity views when the entity ID is known. Usually useful to handle //event based logic. readonly Dictionary _globalEntityViewsDBDic; - + Dictionary _entityInfos; } } \ No newline at end of file diff --git a/Svelto.ECS/EntityFactory.cs b/Svelto.ECS/EntityFactory.cs index e90d220..8fe024e 100644 --- a/Svelto.ECS/EntityFactory.cs +++ b/Svelto.ECS/EntityFactory.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Xml; using Svelto.DataStructures; using Svelto.Utilities; using Console = Utility.Console; @@ -11,31 +12,20 @@ namespace Svelto.ECS.Internal internal static void BuildGroupedEntityViews(int entityID, int groupID, Dictionary> groupEntityViewsByType, EntityDescriptorInfo entityViewsToBuildDescriptor, + Dictionary entityInfos, object[] implementors) { var @group = FetchGroup(groupID, groupEntityViewsByType); BuildEntityViewsAndAddToGroup(new EGID(entityID, groupID), group, entityViewsToBuildDescriptor, implementors); - AddEntityInfoView(new EGID(entityID, groupID), entityViewsToBuildDescriptor, @group); + AddEntityInfoView(new EGID(entityID, groupID), entityViewsToBuildDescriptor, entityInfos); } - static void AddEntityInfoView(EGID entityID, EntityDescriptorInfo entityViewsToBuildDescriptor, - Dictionary @group) + static void AddEntityInfoView(EGID entityID, EntityDescriptorInfo entityViewsToBuildDescriptor, + Dictionary entityInfos) { - //should be a struct? - var removeEntityView = new EntityInfoView(); - - removeEntityView._ID = entityID; - removeEntityView.entityViews = entityViewsToBuildDescriptor.entityViewsToBuild; - - ITypeSafeList list; - - if (group.TryGetValue(typeof(EntityInfoView), out list) == false) - list = group[typeof(EntityInfoView)] = - new TypeSafeFasterListForECSForClasses(); - - ((TypeSafeFasterListForECSForClasses) list).Add(removeEntityView); + entityInfos.Add(entityID.GID, entityViewsToBuildDescriptor.entityViewsToBuild); } static Dictionary FetchGroup(int groupID, Dictionary> groupEntityViewsByType) @@ -202,7 +192,7 @@ namespace Svelto.ECS.Internal } #endif static readonly Dictionary _cachedTypes = new Dictionary(); - + const string DUPLICATE_IMPLEMENTOR_ERROR = "Svelto.ECS the same component is implemented with more than one implementor. This is considered an error and MUST be fixed. "; diff --git a/Svelto.ECS/EntityInfoView.cs b/Svelto.ECS/EntityInfoView.cs deleted file mode 100644 index 14490bd..0000000 --- a/Svelto.ECS/EntityInfoView.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Svelto.ECS.Internal -{ - class EntityInfoView : EntityView - { - internal IEntityViewBuilder[] entityViews; - } -} \ No newline at end of file