From 002cff1c2e206a5b15c05fb0908ec8283af32ff8 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Sun, 22 Mar 2020 12:12:26 -0700 Subject: [PATCH] rework typed message store --- encompass-cs/Collections/TypedMessageStore.cs | 43 ++++++++++++------- test/ComponentTest.cs | 2 +- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/encompass-cs/Collections/TypedMessageStore.cs b/encompass-cs/Collections/TypedMessageStore.cs index 0a7a1e8..1a2c60c 100644 --- a/encompass-cs/Collections/TypedMessageStore.cs +++ b/encompass-cs/Collections/TypedMessageStore.cs @@ -10,10 +10,12 @@ namespace Encompass internal class TypedMessageStore : TypedMessageStore where TMessage : struct, IMessage { - private readonly List _store = new List(128); + private readonly List _indices = new List(); + 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> _entityToMessage = new Dictionary>(); + private readonly Dictionary> _entityToIndices = new Dictionary>(); + private readonly IDManager _idManager = new IDManager(); public override void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta) { @@ -52,56 +54,67 @@ namespace Encompass } } - public void Add(TMessage message) + public void Add(in TMessage message) { - _store.Add(message); + var index = _idManager.NextID(); + _indices.Add(index); + _store[index] = message; if (message is IHasEntity entityMessage) { var entityID = entityMessage.Entity.ID; - if (!_entityToMessage.ContainsKey(entityID)) { _entityToMessage.Add(entityID, new List()); } - _entityToMessage[entityID].Add(message); + if (!_entityToIndices.ContainsKey(entityID)) { _entityToIndices.Add(entityID, new HashSet()); } + _entityToIndices[entityID].Add(index); } } - public void Add(TMessage message, double time) + public void Add(in TMessage message, double time) { _delayedStore.Add((message, time)); } - public void AddIgnoringTimeDilation(TMessage message, double time) + public void AddIgnoringTimeDilation(in TMessage message, double time) { _delayedStoreIgnoringTimeDilation.Add((message, time)); } public TMessage First() { - return _store[0]; + return _store[_indices[0]]; } public bool Any() { - return _store.Count > 0; + return _indices.Count > 0; } public IEnumerable All() { - return _store; + foreach (var index in _indices) + { + yield return _store[index]; + } } public IEnumerable WithEntity(int entityID) { - return _entityToMessage.ContainsKey(entityID) ? _entityToMessage[entityID] : System.Linq.Enumerable.Empty(); + if (_entityToIndices.ContainsKey(entityID)) + { + foreach (var index in _entityToIndices[entityID]) + { + yield return _store[index]; + } + } } public bool SomeWithEntity(int entityID) { - return _entityToMessage.ContainsKey(entityID) && _entityToMessage[entityID].Count > 0; + return _entityToIndices.ContainsKey(entityID) && _entityToIndices[entityID].Count > 0; } public override void Clear() { - _store.Clear(); - foreach (var set in _entityToMessage.Values) + _indices.Clear(); + foreach (var set in _entityToIndices.Values) { set.Clear(); } diff --git a/test/ComponentTest.cs b/test/ComponentTest.cs index 95c8f68..b8826b2 100644 --- a/test/ComponentTest.cs +++ b/test/ComponentTest.cs @@ -48,7 +48,7 @@ namespace Tests foreach (var addComponentTestMessage in ReadMessages()) { Assert.IsTrue(HasComponent(addComponentTestMessage.entity)); - var gottenComponent = GetComponent(addComponentTestMessage.entity); + ref readonly var gottenComponent = ref GetComponent(addComponentTestMessage.entity); gottenComponent.Should().BeEquivalentTo(addComponentTestMessage.mockComponent); } }