From 664f08c44660b7c2933629901d1d8412f33b9574 Mon Sep 17 00:00:00 2001 From: evan Date: Mon, 16 Dec 2019 20:40:15 -0800 Subject: [PATCH] optimized away some LINQ --- encompass-cs/Collections/ComponentStore.cs | 2 +- .../Collections/TypedComponentStore.cs | 18 ++++++------- encompass-cs/ComponentManager.cs | 8 +++--- encompass-cs/ComponentUpdateManager.cs | 25 +++++++++++-------- encompass-cs/Engine.cs | 23 ++++++++++------- encompass-cs/EntityManager.cs | 4 +-- encompass-cs/Renderer.cs | 17 ++++++++----- encompass-cs/TimeManager.cs | 9 +++++-- encompass-cs/WorldBuilder.cs | 2 ++ 9 files changed, 65 insertions(+), 43 deletions(-) diff --git a/encompass-cs/Collections/ComponentStore.cs b/encompass-cs/Collections/ComponentStore.cs index c04845c..3283baf 100644 --- a/encompass-cs/Collections/ComponentStore.cs +++ b/encompass-cs/Collections/ComponentStore.cs @@ -85,7 +85,7 @@ namespace Encompass } } - public IEnumerable<(Entity, TComponent)> All() where TComponent : struct, IComponent + public IEnumerable<(TComponent, Entity)> All() where TComponent : struct, IComponent { return Lookup().All(); } diff --git a/encompass-cs/Collections/TypedComponentStore.cs b/encompass-cs/Collections/TypedComponentStore.cs index 2b47f63..63409a2 100644 --- a/encompass-cs/Collections/TypedComponentStore.cs +++ b/encompass-cs/Collections/TypedComponentStore.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; namespace Encompass { @@ -52,21 +51,22 @@ namespace Encompass priorities.Clear(); } - public IEnumerable<(Entity, TComponent)> All() + public IEnumerable<(TComponent, Entity)> All() { - return store.Select(kvp => (kvp.Key, kvp.Value)); + foreach (var kvp in store) + { + yield return (kvp.Value, kvp.Key); + } } public override IEnumerable<(Entity, Type, IComponent)> AllInterfaceTyped() { - return store.Select(kvp => (kvp.Key, typeof(TComponent), (IComponent)kvp.Value)); + foreach (var kvp in store) + { + yield return (kvp.Key, typeof(TComponent), (IComponent)kvp.Value); + } } - // public override IEnumerable All() - // { - // return store.Values.Cast(); - // } - public override void Remove(Entity entity) { store.Remove(entity); diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 611526d..cf105c5 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; namespace Encompass { @@ -44,12 +43,15 @@ namespace Encompass internal IEnumerable<(TComponent, Entity)> GetComponentsIncludingEntity() where TComponent : struct, IComponent { - return componentStore.All().Select(pair => (pair.Item2, pair.Item1)); + return componentStore.All(); } internal IEnumerable GetComponentsByType() where TComponent : struct, IComponent { - return componentStore.All().Select(pair => pair.Item2); + foreach (var pair in componentStore.All()) + { + yield return pair.Item1; + } } internal TComponent GetComponentByEntityAndType(Entity entity) where TComponent : struct, IComponent diff --git a/encompass-cs/ComponentUpdateManager.cs b/encompass-cs/ComponentUpdateManager.cs index 654ed99..5897b13 100644 --- a/encompass-cs/ComponentUpdateManager.cs +++ b/encompass-cs/ComponentUpdateManager.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; namespace Encompass { @@ -70,39 +69,45 @@ namespace Encompass // general component reads by type - internal IEnumerable<(Entity, TComponent)> ReadExistingAndPendingComponentsByType() where TComponent : struct, IComponent + internal IEnumerable<(TComponent, Entity)> ReadExistingAndPendingComponentsByType() where TComponent : struct, IComponent { return existingAndPendingComponentStore.All(); } - internal IEnumerable<(Entity, TComponent)> ReadExistingComponentsByType() where TComponent : struct, IComponent + internal IEnumerable<(TComponent, Entity)> ReadExistingComponentsByType() where TComponent : struct, IComponent { return existingComponentStore.All(); } - internal IEnumerable<(Entity, TComponent)> ReadPendingComponentsByType() where TComponent : struct, IComponent + internal IEnumerable<(TComponent, Entity)> ReadPendingComponentsByType() where TComponent : struct, IComponent { return pendingComponentStore.All(); } // singular component reads by type - internal (Entity, TComponent) ReadFirstExistingOrPendingComponentByType() where TComponent : struct, IComponent + internal (TComponent, Entity) ReadFirstExistingOrPendingComponentByType() where TComponent : struct, IComponent { if (!SomeExistingOrPendingComponent()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } - return ReadExistingAndPendingComponentsByType().First(); + var enumerator = ReadExistingAndPendingComponentsByType().GetEnumerator(); + enumerator.MoveNext(); + return enumerator.Current; } - internal (Entity, TComponent) ReadFirstExistingComponentByType() where TComponent : struct, IComponent + internal (TComponent, Entity) ReadFirstExistingComponentByType() where TComponent : struct, IComponent { if (!SomeExistingComponent()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } - return ReadExistingComponentsByType().First(); + var enumerator = ReadExistingComponentsByType().GetEnumerator(); + enumerator.MoveNext(); + return enumerator.Current; } - internal (Entity, TComponent) ReadFirstPendingComponentByType() where TComponent : struct, IComponent + internal (TComponent, Entity) ReadFirstPendingComponentByType() where TComponent : struct, IComponent { if (!SomePendingComponent()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } - return ReadPendingComponentsByType().First(); + var enumerator = ReadPendingComponentsByType().GetEnumerator(); + enumerator.MoveNext(); + return enumerator.Current; } // check if some component of type exists in the world diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 6d1fc6b..56a5c1a 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -156,7 +156,7 @@ namespace Encompass /// protected Entity ReadEntity() where TComponent : struct, IComponent { - return ReadComponentHelper().Item1; + return ReadComponentHelper().Item2; } /// @@ -164,7 +164,10 @@ namespace Encompass /// protected IEnumerable ReadEntities() where TComponent : struct, IComponent { - return ReadComponentsHelper().Select(pair => pair.Item1); + foreach (var pair in ReadComponentsHelper()) + { + yield return pair.Item2; + } } // these next two are for the ComponentMessageEmitter only @@ -174,7 +177,7 @@ namespace Encompass return componentManager.GetComponentsByType(); } - private IEnumerable<(Entity, TComponent)> ReadComponentsHelper() where TComponent : struct, IComponent + private IEnumerable<(TComponent, Entity)> ReadComponentsHelper() where TComponent : struct, IComponent { var pendingRead = readPendingTypes.Contains(typeof(TComponent)); var existingRead = readTypes.Contains(typeof(TComponent)); @@ -201,7 +204,10 @@ namespace Encompass /// protected IEnumerable ReadComponents() where TComponent : struct, IComponent { - return ReadComponentsHelper().Select(tuple => tuple.Item2); + foreach (var pair in ReadComponentsHelper()) + { + yield return pair.Item1; + } } /// @@ -209,7 +215,7 @@ namespace Encompass /// protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity() where TComponent : struct, IComponent { - return ReadComponentsHelper().Select((tuple) => (tuple.Item2, tuple.Item1)); + return ReadComponentsHelper(); } internal IEnumerable<(TComponent, Entity)> InternalRead() where TComponent : struct, IComponent @@ -217,7 +223,7 @@ namespace Encompass return componentManager.GetComponentsIncludingEntity(); } - private (Entity, TComponent) ReadComponentHelper() where TComponent : struct, IComponent + private (TComponent, Entity) ReadComponentHelper() where TComponent : struct, IComponent { var pendingRead = readPendingTypes.Contains(typeof(TComponent)); var existingRead = readTypes.Contains(typeof(TComponent)); @@ -244,7 +250,7 @@ namespace Encompass /// protected TComponent ReadComponent() where TComponent : struct, IComponent { - return ReadComponentHelper().Item2; + return ReadComponentHelper().Item1; } /// @@ -252,8 +258,7 @@ namespace Encompass /// protected (TComponent, Entity) ReadComponentIncludingEntity() where TComponent : struct, IComponent { - var (entity, component) = ReadComponentHelper(); - return (component, entity); + return ReadComponentHelper(); } /// diff --git a/encompass-cs/EntityManager.cs b/encompass-cs/EntityManager.cs index b3c700b..919d4c1 100644 --- a/encompass-cs/EntityManager.cs +++ b/encompass-cs/EntityManager.cs @@ -1,7 +1,5 @@ -using System.Linq; -using System; +using System; using System.Collections.Generic; -using Encompass.Exceptions; namespace Encompass { diff --git a/encompass-cs/Renderer.cs b/encompass-cs/Renderer.cs index b58dd39..c4a1dab 100644 --- a/encompass-cs/Renderer.cs +++ b/encompass-cs/Renderer.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; namespace Encompass { @@ -21,7 +19,10 @@ namespace Encompass protected IEnumerable ReadEntities() where TComponent : struct, IComponent { - return ReadComponentsIncludingEntity().Select(tuple => tuple.Item2); + foreach (var pair in ReadComponentsIncludingEntity()) + { + yield return pair.Item2; + } } protected Entity ReadEntity() where TComponent : struct, IComponent @@ -41,12 +42,16 @@ namespace Encompass protected TComponent ReadComponent() where TComponent : struct, IComponent { - return ReadComponents().First(); + var enumerator = ReadComponents().GetEnumerator(); + enumerator.MoveNext(); + return enumerator.Current; } protected (TComponent, Entity) ReadComponentIncludingEntity() where TComponent : struct, IComponent { - return ReadComponentsIncludingEntity().First(); + var enumerator = ReadComponentsIncludingEntity().GetEnumerator(); + enumerator.MoveNext(); + return enumerator.Current; } protected TComponent GetComponent(Entity entity) where TComponent : struct, IComponent diff --git a/encompass-cs/TimeManager.cs b/encompass-cs/TimeManager.cs index d29ac10..ee05939 100644 --- a/encompass-cs/TimeManager.cs +++ b/encompass-cs/TimeManager.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; namespace Encompass { @@ -16,7 +15,13 @@ namespace Encompass { get { - return timeDilationDatas.Count == 0 ? 1 : timeDilationDatas.Select(data => data.Factor).Average(); + if (timeDilationDatas.Count == 0) { return 1; } + var average = 0.0; + foreach (var data in timeDilationDatas) + { + average += data.Factor; + } + return average / timeDilationDatas.Count; } } diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index 0d50a6c..90b1eea 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -387,6 +387,8 @@ namespace Encompass /// /// This is necessary because Encompass heavily uses generic methods with value types, /// so the first time any code path runs the JIT gets smashed. This method warms up the runtime. + /// It does so by grabbing all component and message types known to the WorldBuilder and + /// executing every possible generic method that could be executed with those types. /// private void PreloadJIT(IEnumerable componentTypes, IEnumerable messageTypes) {