-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reuse group collections more efficiently + better parallel approach v…
…ia Task.WaitAll
- Loading branch information
1 parent
e0cdabe
commit 04d823b
Showing
10 changed files
with
181 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace EasyEcs.Core; | ||
|
||
/// <summary> | ||
/// A wrapper for an <see cref="IExecuteSystem"/> that will only execute the system at certain intervals. | ||
/// Used by Context. | ||
/// </summary> | ||
internal class ExecuteSystemWrapper | ||
{ | ||
private readonly IExecuteSystem _system; | ||
private int _counter; | ||
|
||
public ExecuteSystemWrapper(IExecuteSystem system) | ||
{ | ||
_system = system; | ||
} | ||
|
||
internal Task Update(Context context) | ||
{ | ||
if (_system.ExecuteFrequency == 1 || (_counter++ > 0 && _counter % _system.ExecuteFrequency == 0)) | ||
{ | ||
return _system.OnExecute(context); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace EasyEcs.Core; | ||
|
||
/// <summary> | ||
/// Represents a pooled collection. Use this for iterating (i.e. foreach) over a collection. Remember to dispose it. | ||
/// </summary> | ||
/// <typeparam name="TCollection"></typeparam> | ||
/// <typeparam name="TElement"></typeparam> | ||
public class PooledCollection<TCollection, TElement> : ICollection<TElement>, IDisposable | ||
where TCollection : ICollection<TElement>, new() | ||
{ | ||
internal TCollection Collection; | ||
private static readonly ConcurrentQueue<PooledCollection<TCollection, TElement>> Pool = new(); | ||
|
||
/// <summary> | ||
/// Create a pooled collection. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static PooledCollection<TCollection, TElement> Create() | ||
{ | ||
if (Pool.TryDequeue(out var pooled)) | ||
{ | ||
return pooled; | ||
} | ||
|
||
return new PooledCollection<TCollection, TElement>(); | ||
} | ||
|
||
public TElement this[int index] => Collection.ElementAt(index); | ||
|
||
private PooledCollection() | ||
{ | ||
Collection = new TCollection(); | ||
} | ||
|
||
public IEnumerator<TElement> GetEnumerator() | ||
{ | ||
return Collection.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Pool.Enqueue(this); | ||
} | ||
|
||
public void Add(TElement item) | ||
{ | ||
Collection.Add(item); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
Collection.Clear(); | ||
} | ||
|
||
public bool Contains(TElement item) | ||
{ | ||
return Collection.Contains(item); | ||
} | ||
|
||
public void CopyTo(TElement[] array, int arrayIndex) | ||
{ | ||
Collection.CopyTo(array, arrayIndex); | ||
} | ||
|
||
public bool Remove(TElement item) | ||
{ | ||
return Collection.Remove(item); | ||
} | ||
|
||
public int Count => Collection.Count; | ||
public bool IsReadOnly => Collection.IsReadOnly; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.