From 9430a7a497827266f20a862a11d23d6578899513 Mon Sep 17 00:00:00 2001 From: sebas77 Date: Fri, 18 Jan 2019 14:46:52 +0000 Subject: [PATCH] use the new platform profiler and fix some code that would prevent the correct use of multiple enginesroot --- Svelto.Common | 2 +- Svelto.ECS/EnginesRoot.Entities.cs | 13 +- Svelto.ECS/EnginesRoot.Submission.cs | 4 +- Svelto.ECS/EntityBuilder.cs | 14 ++- Svelto.ECS/EntityViewUtility.cs | 180 ++++++++++++++------------- 5 files changed, 114 insertions(+), 99 deletions(-) diff --git a/Svelto.Common b/Svelto.Common index 500cff6..bc533fa 160000 --- a/Svelto.Common +++ b/Svelto.Common @@ -1 +1 @@ -Subproject commit 500cff60f786add412b745726d0c700364f93b59 +Subproject commit bc533fa00a89fc37dcd635909c138cf7953c3376 diff --git a/Svelto.ECS/EnginesRoot.Entities.cs b/Svelto.ECS/EnginesRoot.Entities.cs index 2358edb..15903fb 100644 --- a/Svelto.ECS/EnginesRoot.Entities.cs +++ b/Svelto.ECS/EnginesRoot.Entities.cs @@ -15,15 +15,15 @@ namespace Svelto.ECS /// public void Dispose() { - using (var profiler = new PlatformProfiler("Final Dispose")) + var profiler = new PlatformProfiler(); + using (profiler.StartNewSession("Final Dispose")) { foreach (var groups in _groupEntityDB) foreach (var entityList in groups.Value) { try { - var platformProfiler = profiler; - entityList.Value.RemoveEntitiesFromEngines(_entityEngines, ref platformProfiler); + entityList.Value.RemoveEntitiesFromEngines(_entityEngines, ref profiler); } catch (Exception e) { @@ -126,7 +126,8 @@ namespace Svelto.ECS void MoveEntity(IEntityBuilder[] entityBuilders, EGID entityGID, Type originalDescriptorType, EGID toEntityGID, Dictionary toGroup = null) { - using (var profiler = new PlatformProfiler("Move Entity")) + var profiler = new PlatformProfiler(); + using (profiler.StartNewSession("Move Entity")) { //for each entity view generated by the entity descriptor Dictionary fromGroup; @@ -137,7 +138,6 @@ namespace Svelto.ECS .FastConcat(entityGID.groupID)); ITypeSafeDictionary entityInfoViewDic; - EntityInfoView entityInfoView = default(EntityInfoView); //Check if there is an EntityInfoView linked to this entity, if so it's a DynamicEntityDescriptor! @@ -223,7 +223,8 @@ namespace Svelto.ECS void RemoveGroupAndEntitiesFromDB(int groupID) { - using (var profiler = new PlatformProfiler("Remove Group")) + var profiler = new PlatformProfiler(); + using (profiler.StartNewSession("Remove Group")) { var dictionariesOfEntities = _groupEntityDB[groupID]; foreach (var dictionaryOfEntities in dictionariesOfEntities) diff --git a/Svelto.ECS/EnginesRoot.Submission.cs b/Svelto.ECS/EnginesRoot.Submission.cs index 36866af..9209166 100644 --- a/Svelto.ECS/EnginesRoot.Submission.cs +++ b/Svelto.ECS/EnginesRoot.Submission.cs @@ -5,7 +5,6 @@ using Svelto.DataStructures; using Svelto.DataStructures.Experimental; using Svelto.ECS.Internal; using Svelto.ECS.Schedulers; -using Console = Svelto.Console; namespace Svelto.ECS { @@ -13,7 +12,8 @@ namespace Svelto.ECS { void SubmitEntityViews() { - using (var profiler = new PlatformProfiler("Svelto.ECS - Entities Submission")) + var profiler = new PlatformProfiler(); + using (profiler.StartNewSession("Svelto.ECS - Entities Submission")) { if (_entitiesOperations.Count > 0) { diff --git a/Svelto.ECS/EntityBuilder.cs b/Svelto.ECS/EntityBuilder.cs index 3bef0fa..696b98b 100644 --- a/Svelto.ECS/EntityBuilder.cs +++ b/Svelto.ECS/EntityBuilder.cs @@ -34,9 +34,8 @@ namespace Svelto.ECS T entityView; EntityView.BuildEntityView(entityID, out entityView); - this.FillEntityView(ref entityView - , entityViewBlazingFastReflection - , implementors); + this.FillEntityView(ref entityView, entityViewBlazingFastReflection, implementors, implementorsByType, + cachedTypes); castedDic.Add(entityID.entityID, ref entityView); } @@ -67,6 +66,15 @@ namespace Svelto.ECS { return ENTITY_VIEW_TYPE; } + +#if DEBUG && !PROFILER + readonly Dictionary> implementorsByType = new Dictionary>(); +#else + readonly Dictionary implementorsByType = new Dictionary(); +#endif + + //this is used to avoid newing a dictionary every time, but it's used locally only and it's cleared for each use + readonly Dictionary cachedTypes = new Dictionary(); static FasterList>> entityViewBlazingFastReflection { diff --git a/Svelto.ECS/EntityViewUtility.cs b/Svelto.ECS/EntityViewUtility.cs index 9d710e8..949be8f 100644 --- a/Svelto.ECS/EntityViewUtility.cs +++ b/Svelto.ECS/EntityViewUtility.cs @@ -1,129 +1,135 @@ using System; using System.Collections.Generic; using Svelto.DataStructures; -using Svelto.ECS; using Svelto.ECS.Internal; using Svelto.Utilities; -static class EntityViewUtility +namespace Svelto.ECS { - public static void FillEntityView(this IEntityBuilder entityBuilder - , ref T entityView - , FasterList>> entityViewBlazingFastReflection - , object[] implementors) + +#if DEBUG && !PROFILER + struct ECSTuple { - int count; + public readonly T1 implementorType; + public T2 numberOfImplementations; + + public ECSTuple(T1 implementor, T2 v) + { + implementorType = implementor; + numberOfImplementations = v; + } + } +#endif - //efficient way to collect the fields of every EntityViewType - var setters = - FasterList>> - .NoVirt.ToArrayFast(entityViewBlazingFastReflection, out count); - for (var index = 0; index < implementors.Length; index++) + static class EntityViewUtility + { + + public static void FillEntityView(this IEntityBuilder entityBuilder + , ref T entityView + , FasterList>> + entityViewBlazingFastReflection + , object[] implementors, +#if DEBUG && !PROFILER + Dictionary> implementorsByType +#else + Dictionary implementorsByType +#endif + , Dictionary cachedTypes + ) { - var implementor = implementors[index]; + int count; - if (implementor != null) - { - var type = implementor.GetType(); + //efficient way to collect the fields of every EntityViewType + var setters = + FasterList>> + .NoVirt.ToArrayFast(entityViewBlazingFastReflection, out count); - Type[] interfaces; - if (_cachedTypes.TryGetValue(type, out interfaces) == false) - interfaces = _cachedTypes[type] = type.GetInterfacesEx(); + for (var index = 0; index < implementors.Length; index++) + { + var implementor = implementors[index]; - for (var iindex = 0; iindex < interfaces.Length; iindex++) + if (implementor != null) { - var componentType = interfaces[iindex]; -#if DEBUG && !PROFILER - Tuple implementorData; + var type = implementor.GetType(); + + Type[] interfaces; + if (cachedTypes.TryGetValue(type, out interfaces) == false) + interfaces = cachedTypes[type] = type.GetInterfacesEx(); - if (implementorsByType.TryGetValue(componentType, out implementorData)) + for (var iindex = 0; iindex < interfaces.Length; iindex++) { - implementorData.numberOfImplementations++; - implementorsByType[componentType] = implementorData; - } - else - implementorsByType[componentType] = new Tuple(implementor, 1); + var componentType = interfaces[iindex]; +#if DEBUG && !PROFILER + ECSTuple implementorData; + + if (implementorsByType.TryGetValue(componentType, out implementorData)) + { + implementorData.numberOfImplementations++; + implementorsByType[componentType] = implementorData; + } + else + implementorsByType[componentType] = new ECSTuple(implementor, 1); #else - implementorsByType[componentType] = implementor; + implementorsByType[componentType] = implementor; #endif + } } - } #if DEBUG && !PROFILER - else - { - Svelto.Console.Log(NULL_IMPLEMENTOR_ERROR.FastConcat(" entityView ", - entityBuilder.GetEntityType().ToString())); - } + else + { + Svelto.Console.Log(NULL_IMPLEMENTOR_ERROR.FastConcat(" entityView ", + entityBuilder.GetEntityType().ToString())); + } #endif - } + } - for (var i = 0; i < count; i++) - { - var fieldSetter = setters[i]; - var fieldType = fieldSetter.Key; + for (var i = 0; i < count; i++) + { + var fieldSetter = setters[i]; + var fieldType = fieldSetter.Key; #if DEBUG && !PROFILER - Tuple component; + ECSTuple component; #else object component; #endif - if (implementorsByType.TryGetValue(fieldType, out component) == false) - { - var e = new ECSException(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + - " - EntityView: " + entityBuilder.GetEntityType().Name); + if (implementorsByType.TryGetValue(fieldType, out component) == false) + { + var e = new ECSException(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + + " - EntityView: " + entityBuilder.GetEntityType().Name); - throw e; - } + throw e; + } #if DEBUG && !PROFILER - if (component.numberOfImplementations > 1) - throw new ECSException(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat( - "Component Type: ", fieldType.Name, " implementor: ", - component.implementorType.ToString()) + " - EntityView: " + - entityBuilder.GetEntityType().Name); + if (component.numberOfImplementations > 1) + throw new ECSException(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat( + "Component Type: ", fieldType.Name, + " implementor: ", + component.implementorType + .ToString()) + + " - EntityView: " + + entityBuilder.GetEntityType().Name); #endif #if DEBUG && !PROFILER - fieldSetter.Value(ref entityView, component.implementorType); + fieldSetter.Value(ref entityView, component.implementorType); #else fieldSetter.Value(ref entityView, component); #endif + } + + implementorsByType.Clear(); } - implementorsByType.Clear(); - } - - - //this is used to avoid newing a dictionary every time, but it's used locally only and it's cleared for each use -#if DEBUG && !PROFILER - static readonly Dictionary> implementorsByType = - new Dictionary>(); -#else - static readonly Dictionary implementorsByType = new Dictionary(); -#endif + 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. "; -#if DEBUG && !PROFILER - struct Tuple - { - public readonly T1 implementorType; - public T2 numberOfImplementations; + const string NULL_IMPLEMENTOR_ERROR = + "Svelto.ECS Null implementor, please be careful about the implementors passed to avoid " + + "performance loss "; - public Tuple(T1 implementor, T2 v) - { - implementorType = implementor; - numberOfImplementations = v; - } + const string NOT_FOUND_EXCEPTION = "Svelto.ECS Implementor not found for an EntityView. "; } -#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. "; - - const string NULL_IMPLEMENTOR_ERROR = - "Svelto.ECS Null implementor, please be careful about the implementors passed to avoid " + - "performance loss "; - - const string NOT_FOUND_EXCEPTION = "Svelto.ECS Implementor not found for an EntityView. "; } \ No newline at end of file