Merge pull request #8 from thatcosmonaut/preload_jit

Preload jit
pull/5/head
thatcosmonaut 2019-12-20 11:36:04 -08:00 committed by GitHub
commit 16d1d29fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 121 additions and 44 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

@ -41,6 +41,8 @@ namespace Encompass
CallGenericMethod(type, "ReadComponentIncludingEntity", null);
CallGenericMethod(type, "ReadComponents", null);
CallGenericMethod(type, "ReadComponentsIncludingEntity", null);
CallGenericMethod(type, "ReadEntity", null);
CallGenericMethod(type, "ReadEntities", null);
CallGenericMethod(type, "GetComponent", new Type[] { typeof(Entity) }, new object[] { _entity });
CallGenericMethod(type, "HasComponent", new Type[] { typeof(Entity) }, new object[] { _entity });
CallGenericMethod(type, "SomeComponent", null);

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Encompass
{
class UberRenderer : Renderer
{
private Entity _entity;
private IEnumerable<Type> _componentTypes;
public UberRenderer(Entity entity, IEnumerable<Type> componentTypes)
{
_entity = entity;
_componentTypes = componentTypes;
}
public void Render()
{
foreach (var type in _componentTypes)
{
CallGenericMethod(type, "ReadEntities", null);
CallGenericMethod(type, "ReadEntity", null);
CallGenericMethod(type, "ReadComponent", null);
CallGenericMethod(type, "ReadComponentIncludingEntity", null);
CallGenericMethod(type, "ReadComponents", null);
CallGenericMethod(type, "ReadComponentsIncludingEntity", null);
CallGenericMethod(type, "GetComponent", new object[] { _entity });
CallGenericMethod(type, "HasComponent", new object[] { _entity });
CallGenericMethod(type, "SomeComponent", null);
}
}
private void CallGenericMethod(Type type, string methodName, object[] parameters)
{
var readComponentMethod = typeof(Renderer).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters);
}
}
}

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)
{
@ -399,7 +401,9 @@ namespace Encompass
var dummyRenderManager = new RenderManager(dummyDrawLayerManager);
var prepEngineOrder = new List<Engine>();
var uberEngine = new UberEngine(dummyEntityManager.CreateEntity(), componentTypes, messageTypes);
var entity = dummyEntityManager.CreateEntity();
var uberEngine = new UberEngine(entity, componentTypes, messageTypes);
uberEngine.AssignEntityManager(dummyEntityManager);
uberEngine.AssignComponentManager(dummyComponentManager);
@ -407,6 +411,10 @@ namespace Encompass
uberEngine.AssignComponentUpdateManager(dummyComponentUpdateManager);
uberEngine.AssignTimeManager(dummyTimeManager);
var uberRenderer = new UberRenderer(entity, componentTypes);
uberRenderer.AssignComponentManager(dummyComponentManager);
uberRenderer.AssignEntityManager(dummyEntityManager);
foreach (var type in componentTypes)
{
var componentManagerRegisterMethod = typeof(ComponentManager).GetMethod("RegisterComponentType");
@ -451,6 +459,12 @@ namespace Encompass
dummyComponentUpdateManager.Clear();
dummyWorld.Update(1);
uberEngine.Write();
dummyComponentManager.WriteComponents();
dummyComponentUpdateManager.Clear();
uberRenderer.Render();
}
}
}