optimized away some LINQ

pull/5/head
evan 2019-12-16 20:40:15 -08:00
parent 84876ebc87
commit 664f08c446
9 changed files with 65 additions and 43 deletions

View File

@ -85,7 +85,7 @@ namespace Encompass
}
}
public IEnumerable<(Entity, TComponent)> All<TComponent>() where TComponent : struct, IComponent
public IEnumerable<(TComponent, Entity)> All<TComponent>() where TComponent : struct, IComponent
{
return Lookup<TComponent>().All();
}

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Encompass
{
@ -52,20 +51,21 @@ 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<T> All<T>()
// {
// return store.Values.Cast<T>();
// }
public override void Remove(Entity entity)
{

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
namespace Encompass
{
@ -44,12 +43,15 @@ namespace Encompass
internal IEnumerable<(TComponent, Entity)> GetComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
return componentStore.All<TComponent>().Select(pair => (pair.Item2, pair.Item1));
return componentStore.All<TComponent>();
}
internal IEnumerable<TComponent> GetComponentsByType<TComponent>() where TComponent : struct, IComponent
{
return componentStore.All<TComponent>().Select(pair => pair.Item2);
foreach (var pair in componentStore.All<TComponent>())
{
yield return pair.Item1;
}
}
internal TComponent GetComponentByEntityAndType<TComponent>(Entity entity) where TComponent : struct, IComponent

View File

@ -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<TComponent>() where TComponent : struct, IComponent
internal IEnumerable<(TComponent, Entity)> ReadExistingAndPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
{
return existingAndPendingComponentStore.All<TComponent>();
}
internal IEnumerable<(Entity, TComponent)> ReadExistingComponentsByType<TComponent>() where TComponent : struct, IComponent
internal IEnumerable<(TComponent, Entity)> ReadExistingComponentsByType<TComponent>() where TComponent : struct, IComponent
{
return existingComponentStore.All<TComponent>();
}
internal IEnumerable<(Entity, TComponent)> ReadPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
internal IEnumerable<(TComponent, Entity)> ReadPendingComponentsByType<TComponent>() where TComponent : struct, IComponent
{
return pendingComponentStore.All<TComponent>();
}
// singular component reads by type
internal (Entity, TComponent) ReadFirstExistingOrPendingComponentByType<TComponent>() where TComponent : struct, IComponent
internal (TComponent, Entity) ReadFirstExistingOrPendingComponentByType<TComponent>() where TComponent : struct, IComponent
{
if (!SomeExistingOrPendingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
return ReadExistingAndPendingComponentsByType<TComponent>().First();
var enumerator = ReadExistingAndPendingComponentsByType<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
}
internal (Entity, TComponent) ReadFirstExistingComponentByType<TComponent>() where TComponent : struct, IComponent
internal (TComponent, Entity) ReadFirstExistingComponentByType<TComponent>() where TComponent : struct, IComponent
{
if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
return ReadExistingComponentsByType<TComponent>().First();
var enumerator = ReadExistingComponentsByType<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
}
internal (Entity, TComponent) ReadFirstPendingComponentByType<TComponent>() where TComponent : struct, IComponent
internal (TComponent, Entity) ReadFirstPendingComponentByType<TComponent>() where TComponent : struct, IComponent
{
if (!SomePendingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); }
return ReadPendingComponentsByType<TComponent>().First();
var enumerator = ReadPendingComponentsByType<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
}
// check if some component of type exists in the world

View File

@ -156,7 +156,7 @@ namespace Encompass
/// </summary>
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentHelper<TComponent>().Item1;
return ReadComponentHelper<TComponent>().Item2;
}
/// <summary>
@ -164,7 +164,10 @@ namespace Encompass
/// </summary>
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentsHelper<TComponent>().Select(pair => pair.Item1);
foreach (var pair in ReadComponentsHelper<TComponent>())
{
yield return pair.Item2;
}
}
// these next two are for the ComponentMessageEmitter only
@ -174,7 +177,7 @@ namespace Encompass
return componentManager.GetComponentsByType<TComponent>();
}
private IEnumerable<(Entity, TComponent)> ReadComponentsHelper<TComponent>() where TComponent : struct, IComponent
private IEnumerable<(TComponent, Entity)> ReadComponentsHelper<TComponent>() where TComponent : struct, IComponent
{
var pendingRead = readPendingTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
@ -201,7 +204,10 @@ namespace Encompass
/// </summary>
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentsHelper<TComponent>().Select(tuple => tuple.Item2);
foreach (var pair in ReadComponentsHelper<TComponent>())
{
yield return pair.Item1;
}
}
/// <summary>
@ -209,7 +215,7 @@ namespace Encompass
/// </summary>
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentsHelper<TComponent>().Select((tuple) => (tuple.Item2, tuple.Item1));
return ReadComponentsHelper<TComponent>();
}
internal IEnumerable<(TComponent, Entity)> InternalRead<TComponent>() where TComponent : struct, IComponent
@ -217,7 +223,7 @@ namespace Encompass
return componentManager.GetComponentsIncludingEntity<TComponent>();
}
private (Entity, TComponent) ReadComponentHelper<TComponent>() where TComponent : struct, IComponent
private (TComponent, Entity) ReadComponentHelper<TComponent>() where TComponent : struct, IComponent
{
var pendingRead = readPendingTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
@ -244,7 +250,7 @@ namespace Encompass
/// </summary>
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentHelper<TComponent>().Item2;
return ReadComponentHelper<TComponent>().Item1;
}
/// <summary>
@ -252,8 +258,7 @@ namespace Encompass
/// </summary>
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
var (entity, component) = ReadComponentHelper<TComponent>();
return (component, entity);
return ReadComponentHelper<TComponent>();
}
/// <summary>

View File

@ -1,7 +1,5 @@
using System.Linq;
using System;
using System;
using System.Collections.Generic;
using Encompass.Exceptions;
namespace Encompass
{

View File

@ -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<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentsIncludingEntity<TComponent>().Select(tuple => tuple.Item2);
foreach (var pair in ReadComponentsIncludingEntity<TComponent>())
{
yield return pair.Item2;
}
}
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
@ -41,12 +42,16 @@ namespace Encompass
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{
return ReadComponents<TComponent>().First();
var enumerator = ReadComponents<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
}
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentsIncludingEntity<TComponent>().First();
var enumerator = ReadComponentsIncludingEntity<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
}
protected TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent

View File

@ -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;
}
}

View File

@ -387,6 +387,8 @@ namespace Encompass
/// <summary>
/// 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.
/// </summary>
private void PreloadJIT(IEnumerable<Type> componentTypes, IEnumerable<Type> messageTypes)
{