optimize singular entity message read
continuous-integration/drone/push Build is passing Details

pull/3/head
Evan Hemsley 2020-03-22 13:53:23 -07:00
parent 971993f605
commit 5a536e6123
3 changed files with 14 additions and 6 deletions

View File

@ -53,6 +53,11 @@ namespace Encompass
return Lookup<TMessage>().WithEntity(entityID);
}
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage
{
return ref Lookup<TMessage>().FirstWithEntity(entityID);
}
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{
return Lookup<TMessage>().SomeWithEntity(entityID);

View File

@ -17,7 +17,7 @@ namespace Encompass
private readonly TMessage[] _store = new TMessage[128];
private readonly List<(TMessage, double)> _delayedStore = new List<(TMessage, double)>(128);
private readonly List<(TMessage, double)> _delayedStoreIgnoringTimeDilation = new List<(TMessage, double)>(128);
private readonly Dictionary<int, HashSet<int>> _entityToIndices = new Dictionary<int, HashSet<int>>();
private readonly Dictionary<int, List<int>> _entityToIndices = new Dictionary<int, List<int>>();
public override void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
{
@ -63,7 +63,7 @@ namespace Encompass
if (message is IHasEntity entityMessage)
{
var entityID = entityMessage.Entity.ID;
if (!_entityToIndices.ContainsKey(entityID)) { _entityToIndices.Add(entityID, new HashSet<int>()); }
if (!_entityToIndices.ContainsKey(entityID)) { _entityToIndices.Add(entityID, new List<int>()); }
_entityToIndices[entityID].Add(index);
}
}
@ -104,6 +104,11 @@ namespace Encompass
}
}
public ref readonly TMessage FirstWithEntity(int entityID)
{
return ref _store[_entityToIndices[entityID][0]];
}
public bool SomeWithEntity(int entityID)
{
return _entityToIndices.ContainsKey(entityID) && _entityToIndices[entityID].Count > 0;

View File

@ -58,11 +58,9 @@ namespace Encompass
return _messageStore.WithEntity<TMessage>(entityID);
}
internal TMessage WithEntitySingular<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
internal ref readonly TMessage WithEntitySingular<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{
var enumerator = _messageStore.WithEntity<TMessage>(entityID).GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
return ref _messageStore.FirstWithEntity<TMessage>(entityID);
}
internal bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity