Skip to content

Commit 3d247c1

Browse files
release v1.0.0
- [feat] easy way to implement an ECS pattern program - [feat] support concurrency/parallelism to boost performance - [feat] purely asynchronous interfaces to ensure nothing blocks the thread (unless deadlock from user code)
1 parent 866e4e3 commit 3d247c1

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

EasyEcs.Core/Context.cs

+29-13
Original file line numberDiff line numberDiff line change
@@ -196,23 +196,39 @@ public async ValueTask Update(bool parallel = true)
196196
if (_disposed)
197197
throw new InvalidOperationException("Context disposed.");
198198

199+
// if empty, return
200+
if (_executeSystems.Count == 0)
201+
return;
202+
199203
// clear the cache of the groups
200204
InvalidateGroupCache();
201-
// execute the systems
202-
_executeTasks.Clear();
203-
// sort by priority
204-
foreach (var system in _executeSystems.Values)
205+
// group by priority
206+
int remaining = _executeSystems.Count;
207+
int index = 0;
208+
while (remaining > 0)
205209
{
206-
_executeTasks.Add(parallel ? Task.Run(() => system.Update(this)) : system.Update(this));
207-
}
210+
// execute the systems with the same priority
211+
_executeTasks.Clear();
212+
ExecuteSystemWrapper system = _executeSystems.Values[index];
213+
var currentPriority = system.Priority;
214+
while (system.Priority == currentPriority && remaining > 0)
215+
{
216+
var systemWrapper = system;
217+
_executeTasks.Add(parallel ? Task.Run(() => systemWrapper.Update(this)) : systemWrapper.Update(this));
218+
index++;
219+
if (--remaining > 0)
220+
system = _executeSystems.Values[index];
221+
}
208222

209-
try
210-
{
211-
await Task.WhenAll(_executeTasks);
212-
}
213-
catch (Exception e)
214-
{
215-
OnError?.Invoke(e);
223+
// dispatch all tasks of the same priority
224+
try
225+
{
226+
await Task.WhenAll(_executeTasks);
227+
}
228+
catch (Exception e)
229+
{
230+
OnError?.Invoke(e);
231+
}
216232
}
217233

218234
while (_removeList.TryDequeue(out var entity))

EasyEcs.Core/ExecuteSystemWrapper.cs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ internal class ExecuteSystemWrapper
1111
private readonly IExecuteSystem _system;
1212
private int _counter;
1313

14+
internal int Priority => ((SystemBase)_system).Priority;
15+
1416
public ExecuteSystemWrapper(IExecuteSystem system)
1517
{
1618
_system = system;

EasyEcs.sln.DotSettings.user

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AReadOnlyCollection_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F4cfef46534ca7afeaf461953f17dc5b92a0173671794e94d93bbe487d37f4a_003FReadOnlyCollection_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
55
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASocket_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F611efef9cc4db44681420ed548e67f2174d4f12bb94a911f6f81a4ba7ca_003FSocket_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
66
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASortedList_00602_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fd08c4c7cd56e410ba2f228947c6bfcc349200_003F6d_003Fab004b16_003FSortedList_00602_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
7+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=ca59a3c6_002D1b78_002D4f89_002D9b6e_002Df9f35114ecb0/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="SimpleTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
8+
&lt;TestAncestor&gt;
9+
&lt;TestId&gt;NUnit3x::FDA284F0-DE3A-4C46-A8B9-90324F25FCB0::net6.0::EasyEcs.UnitTest.SimpleTest&lt;/TestId&gt;
10+
&lt;/TestAncestor&gt;
11+
&lt;/SessionState&gt;</s:String>
712

813

914
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=fabae239_002Dedef_002D47f4_002Da7cb_002D6b40f5828ca8/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Tests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;

0 commit comments

Comments
 (0)