diff --git a/Entitas/Entitas.csproj b/Entitas/Entitas.csproj index 60d50e8d7..e02b2bffa 100644 --- a/Entitas/Entitas.csproj +++ b/Entitas/Entitas.csproj @@ -69,7 +69,6 @@ <Compile Include="Entitas\Matcher\MatcherException.cs" /> <Compile Include="Entitas\Collector\Collector.cs" /> <Compile Include="Entitas\Collector\CollectorException.cs" /> - <Compile Include="Entitas\EntitasCache.cs" /> <Compile Include="Entitas\EntitasResources.cs" /> <Compile Include="Entitas\Extensions\CollectionExtension.cs" /> <Compile Include="Entitas\Extensions\PublicMemberInfoEntityExtension.cs" /> diff --git a/Entitas/Entitas/EntitasCache.cs b/Entitas/Entitas/EntitasCache.cs deleted file mode 100644 index b6e1bc3d1..000000000 --- a/Entitas/Entitas/EntitasCache.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Collections.Generic; -using DesperateDevs.Utils; - -namespace Entitas { - - public static class EntitasCache { - - static readonly ObjectCache _cache = new ObjectCache(); - - public static List<IComponent> GetIComponentList() { return _cache.Get<List<IComponent>>(); } - public static void PushIComponentList(List<IComponent> list) { list.Clear(); _cache.Push(list); } - - public static List<int> GetIntList() { return _cache.Get<List<int>>(); } - public static void PushIntList(List<int> list) { list.Clear(); _cache.Push(list); } - - public static HashSet<int> GetIntHashSet() { return _cache.Get<HashSet<int>>(); } - public static void PushIntHashSet(HashSet<int> hashSet) { hashSet.Clear(); _cache.Push(hashSet); } - - public static void Reset() { - _cache.Reset(); - } - } -} diff --git a/Entitas/Entitas/Entity/Entity.cs b/Entitas/Entitas/Entity/Entity.cs index cbe66af1d..b548c0260 100644 --- a/Entitas/Entitas/Entity/Entity.cs +++ b/Entitas/Entitas/Entity/Entity.cs @@ -65,6 +65,9 @@ public class Entity : IEntity { /// release it manually at some point. public IAERC aerc { get { return _aerc; } } + readonly List<IComponent> _componentBuffer; + readonly List<int> _indexBuffer; + int _creationIndex; bool _isEnabled; @@ -79,6 +82,11 @@ public class Entity : IEntity { string _toStringCache; StringBuilder _toStringBuilder; + public Entity() { + _componentBuffer = new List<IComponent>(); + _indexBuffer = new List<int>(); + } + public void Initialize(int creationIndex, int totalComponents, Stack<IComponent>[] componentPools, ContextInfo contextInfo = null, IAERC aerc = null) { Reactivate(creationIndex); @@ -234,18 +242,15 @@ public IComponent GetComponent(int index) { /// Returns all added components. public IComponent[] GetComponents() { if (_componentsCache == null) { - var components = EntitasCache.GetIComponentList(); - for (int i = 0; i < _components.Length; i++) { var component = _components[i]; if (component != null) { - components.Add(component); + _componentBuffer.Add(component); } } - _componentsCache = components.ToArray(); - - EntitasCache.PushIComponentList(components); + _componentsCache = _componentBuffer.ToArray(); + _componentBuffer.Clear(); } return _componentsCache; @@ -254,17 +259,14 @@ public IComponent[] GetComponents() { /// Returns all indices of added components. public int[] GetComponentIndices() { if (_componentIndicesCache == null) { - var indices = EntitasCache.GetIntList(); - for (int i = 0; i < _components.Length; i++) { if (_components[i] != null) { - indices.Add(i); + _indexBuffer.Add(i); } } - _componentIndicesCache = indices.ToArray(); - - EntitasCache.PushIntList(indices); + _componentIndicesCache = _indexBuffer.ToArray(); + _indexBuffer.Clear(); } return _componentIndicesCache; diff --git a/Entitas/Entitas/Matcher/MatcherStatic.cs b/Entitas/Entitas/Matcher/MatcherStatic.cs index 04ef61a9d..b2b80a63c 100644 --- a/Entitas/Entitas/Matcher/MatcherStatic.cs +++ b/Entitas/Entitas/Matcher/MatcherStatic.cs @@ -5,6 +5,9 @@ namespace Entitas { public partial class Matcher<TEntity> { + static readonly List<int> _indexBuffer = new List<int>(); + static readonly HashSet<int> _indexSetBuffer = new HashSet<int>(); + public static IAllOfMatcher<TEntity> AllOf(params int[] indices) { var matcher = new Matcher<TEntity>(); matcher._allOfIndices = distinctIndices(indices); @@ -30,21 +33,19 @@ public static IAnyOfMatcher<TEntity> AnyOf(params IMatcher<TEntity>[] matchers) } static int[] mergeIndices(int[] allOfIndices, int[] anyOfIndices, int[] noneOfIndices) { - var indicesList = EntitasCache.GetIntList(); - if (allOfIndices != null) { - indicesList.AddRange(allOfIndices); + _indexBuffer.AddRange(allOfIndices); } if (anyOfIndices != null) { - indicesList.AddRange(anyOfIndices); + _indexBuffer.AddRange(anyOfIndices); } if (noneOfIndices != null) { - indicesList.AddRange(noneOfIndices); + _indexBuffer.AddRange(noneOfIndices); } - var mergedIndices = distinctIndices(indicesList); + var mergedIndices = distinctIndices(_indexBuffer); - EntitasCache.PushIntList(indicesList); + _indexBuffer.Clear(); return mergedIndices; } @@ -81,16 +82,15 @@ static void setComponentNames(Matcher<TEntity> matcher, IMatcher<TEntity>[] matc } static int[] distinctIndices(IList<int> indices) { - var indicesSet = EntitasCache.GetIntHashSet(); - foreach (var index in indices) { - indicesSet.Add(index); + _indexSetBuffer.Add(index); } - var uniqueIndices = new int[indicesSet.Count]; - indicesSet.CopyTo(uniqueIndices); + + var uniqueIndices = new int[_indexSetBuffer.Count]; + _indexSetBuffer.CopyTo(uniqueIndices); Array.Sort(uniqueIndices); - EntitasCache.PushIntHashSet(indicesSet); + _indexSetBuffer.Clear(); return uniqueIndices; } diff --git a/Tests/Tests/Tests.csproj b/Tests/Tests/Tests.csproj index 2e06119f7..c037db8be 100644 --- a/Tests/Tests/Tests.csproj +++ b/Tests/Tests/Tests.csproj @@ -74,7 +74,6 @@ <Compile Include="Fixtures\MyTestContext.cs" /> <Compile Include="Tests\Entitas\describe_Collector.cs" /> <Compile Include="Tests\Entitas\describe_Context.cs" /> - <Compile Include="Tests\Entitas\describe_EntitasCache.cs" /> <Compile Include="Tests\Entitas\describe_EntitasErrorMessages.cs" /> <Compile Include="Tests\Entitas\describe_Entity.cs" /> <Compile Include="Tests\Entitas\describe_EntityIndex.cs" /> diff --git a/Tests/Tests/Tests/Entitas/describe_EntitasCache.cs b/Tests/Tests/Tests/Entitas/describe_EntitasCache.cs deleted file mode 100644 index fe6e52436..000000000 --- a/Tests/Tests/Tests/Entitas/describe_EntitasCache.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Entitas; -using NSpec; - -class describe_EntitasCache : nspec { - - void when_caching() { - - before = () => { - EntitasCache.Reset(); - }; - - it["clears IComponent list"] = () => { - var list = EntitasCache.GetIComponentList(); - list.Add(Component.A); - EntitasCache.PushIComponentList(list); - - EntitasCache.GetIComponentList().Count.should_be(0); - }; - - it["clears int list"] = () => { - var list = EntitasCache.GetIntList(); - list.Add(42); - EntitasCache.PushIntList(list); - - EntitasCache.GetIntList().Count.should_be(0); - }; - - it["clears int hashSet"] = () => { - var hashSet = EntitasCache.GetIntHashSet(); - hashSet.Add(42); - EntitasCache.PushIntHashSet(hashSet); - - EntitasCache.GetIntHashSet().Count.should_be(0); - }; - } -}