Skip to content

Commit 5123d52

Browse files
release v2.0.6
- [opt] remove allocation via using valuetask
1 parent b8f226d commit 5123d52

10 files changed

+21
-21
lines changed

EasyEcs.Core/Context.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private void AddSystem(SystemBase system)
323323
/// <param name="action"></param>
324324
/// <param name="catchError"></param>
325325
/// <typeparam name="T"></typeparam>
326-
private async Task QueryTasks<T>(List<T> list, Func<T, Task> action, bool catchError = true)
326+
private async ValueTask QueryTasks<T>(List<T> list, Func<T, ValueTask> action, bool catchError = true)
327327
{
328328
if (_options.Parallel)
329329
{
@@ -353,7 +353,7 @@ await Parallel.ForEachAsync(list, _parallelOptions, async (item, _) =>
353353
// collect tasks for the current priority
354354
foreach (var item in list)
355355
{
356-
_executeTasks.Add(action(item));
356+
_executeTasks.Add(action(item).AsTask());
357357
}
358358

359359
// dispatch all tasks of the same priority

EasyEcs.Core/Systems/ExecuteSystemWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ internal ExecuteSystemWrapper(IExecuteSystem system)
1616
System = system;
1717
}
1818

19-
internal Task Update(Context context)
19+
internal ValueTask Update(Context context)
2020
{
2121
if (System.ExecuteFrequency == 1 || (_counter++ > 0 && _counter % System.ExecuteFrequency == 0))
2222
{
2323
return System.OnExecute(context);
2424
}
2525

26-
return Task.CompletedTask;
26+
return ValueTask.CompletedTask;
2727
}
2828
}

EasyEcs.Core/Systems/IEndSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ namespace EasyEcs.Core.Systems;
44

55
public interface IEndSystem
66
{
7-
Task OnEnd(Context context);
7+
ValueTask OnEnd(Context context);
88
}

EasyEcs.Core/Systems/IExecuteSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ namespace EasyEcs.Core.Systems;
55
public interface IExecuteSystem
66
{
77
int ExecuteFrequency => 1;
8-
Task OnExecute(Context context);
8+
ValueTask OnExecute(Context context);
99
}

EasyEcs.Core/Systems/IInitSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ namespace EasyEcs.Core.Systems;
44

55
public interface IInitSystem
66
{
7-
Task OnInit(Context context);
7+
ValueTask OnInit(Context context);
88
}

EasyEcs.UnitTest/Systems/HelloWorldSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class HelloWorldSystem : SystemBase, IExecuteSystem
1111
// Make this system to execute first
1212
public override int Priority => -1;
1313

14-
public Task OnExecute(Context context)
14+
public ValueTask OnExecute(Context context)
1515
{
1616
// Get a singleton component from the context
1717
ref var frameComp = ref context.GetSingletonComponent<FrameComponent>().Value;
@@ -23,6 +23,6 @@ public Task OnExecute(Context context)
2323
$"Time: {DateTime.Now:HH:mm:ss.fff})");
2424
// count frames (update singleton component data)
2525
frameComp.FrameCount++;
26-
return Task.CompletedTask;
26+
return ValueTask.CompletedTask;
2727
}
2828
}

EasyEcs.UnitTest/Systems/ModificationSystem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ModificationSystem : SystemBase, IExecuteSystem, IEndSystem
1616
/// </summary>
1717
/// <param name="context"></param>
1818
/// <returns></returns>
19-
public Task OnExecute(Context context)
19+
public ValueTask OnExecute(Context context)
2020
{
2121
// Should be run concurrently if possible
2222
Console.WriteLine($"{GetType().Name} (Priority: {Priority}, " +
@@ -32,15 +32,15 @@ public Task OnExecute(Context context)
3232
comp.Value.Factor = 0.5f);
3333
}
3434

35-
return Task.CompletedTask;
35+
return ValueTask.CompletedTask;
3636
}
3737

3838
/// <summary>
3939
/// Make size to 0
4040
/// </summary>
4141
/// <param name="context"></param>
4242
/// <returns></returns>
43-
public Task OnEnd(Context context)
43+
public ValueTask OnEnd(Context context)
4444
{
4545
var candidates = context.GroupOf<SizeComponent>();
4646

@@ -51,6 +51,6 @@ public Task OnEnd(Context context)
5151
sizeComponent.Height = 0;
5252
}
5353

54-
return Task.CompletedTask;
54+
return ValueTask.CompletedTask;
5555
}
5656
}

EasyEcs.UnitTest/Systems/NotUnmanagedSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace EasyEcs.UnitTest.Systems;
99

1010
public class NotUnmanagedSystem : SystemBase, IExecuteSystem
1111
{
12-
public Task OnExecute(Context context)
12+
public ValueTask OnExecute(Context context)
1313
{
1414
var results = context.GroupOf<NotUnmanagedComponent>();
1515

@@ -26,6 +26,6 @@ public Task OnExecute(Context context)
2626
$" {string.Join(", ", component.Dictionary.Select(kv => $"{kv.Key}: {kv.Value}"))}");
2727
}
2828

29-
return Task.CompletedTask;
29+
return ValueTask.CompletedTask;
3030
}
3131
}

EasyEcs.UnitTest/Systems/ResizeSystem.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace EasyEcs.UnitTest.Systems;
88

99
public class ResizeSystem : SystemBase, IInitSystem, IExecuteSystem
1010
{
11-
public Task OnInit(Context context)
11+
public ValueTask OnInit(Context context)
1212
{
1313
// Get all entities that have ScaleComponent
1414
var candidates = context.GroupOf<ScaleComponent>();
@@ -21,10 +21,10 @@ public Task OnInit(Context context)
2121
scaleComponent.Factor = 2;
2222
}
2323

24-
return Task.CompletedTask;
24+
return ValueTask.CompletedTask;
2525
}
2626

27-
public Task OnExecute(Context context)
27+
public ValueTask OnExecute(Context context)
2828
{
2929
// Should be run concurrently if possible
3030
Console.WriteLine($"{GetType().Name} (Priority: {Priority}, " +
@@ -52,6 +52,6 @@ public Task OnExecute(Context context)
5252
entity.RemoveComponent<ScaleComponent>();
5353
}
5454

55-
return Task.CompletedTask;
55+
return ValueTask.CompletedTask;
5656
}
5757
}

EasyEcs.UnitTest/Systems/TimeConsumingSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace EasyEcs.UnitTest.Systems;
77

88
public class TimeConsumingSystem : SystemBase, IExecuteSystem
99
{
10-
public Task OnExecute(Context context)
10+
public async ValueTask OnExecute(Context context)
1111
{
1212
Console.WriteLine($"{GetType().Name} (Priority: {Priority}, " +
1313
$"Thread: {Environment.CurrentManagedThreadId}, " +
@@ -17,6 +17,6 @@ public Task OnExecute(Context context)
1717
// other tasks at the same priority level should be able to run concurrently on other threads
1818
// if the system is set to run in parallel. Tasks with lower priority will be queued until
1919
// this task (or the most time-consuming task) is finished
20-
return Task.Delay(Random.Shared.Next(10, 100));
20+
await Task.Delay(Random.Shared.Next(10, 100));
2121
}
2222
}

0 commit comments

Comments
 (0)