Skip to content

Commit

Permalink
Reduced code.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Jan 31, 2024
1 parent 878381f commit 11ffdf3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 127 deletions.
66 changes: 2 additions & 64 deletions src/Arch.SourceGen/Queries/InlineParallelQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,7 @@ public static void AppendHpParallelQuery(this StringBuilder builder, int amount)
var innerJob = new IForEachJob<T,{{generics}}>();
innerJob.ForEach = iForEach;

var pool = JobMeta<ChunkIterationJob<IForEachJob<T,{{generics}}>>>.Pool;
var query = Query(in description);
foreach (var archetype in query.GetArchetypeIterator())
{
var archetypeSize = archetype.ChunkCount;
var part = new RangePartitioner(Environment.ProcessorCount, archetypeSize);
foreach (var range in part)
{
var job = pool.Get();
job.Start = range.Start;
job.Size = range.Length;
job.Chunks = archetype.Chunks;
job.Instance = innerJob;

var jobHandle = SharedJobScheduler.Schedule(job);
JobsCache.Add(job);
JobHandles.Add(jobHandle);
}

// Return jobs to pool
SharedJobScheduler.Flush();
JobHandle.CompleteAll(JobHandles.Span);

for (var index = 0; index < JobsCache.Count; index++)
{
var job = Unsafe.As<ChunkIterationJob<IForEachJob<T,{{generics}}>>>(JobsCache[index]);
pool.Return(job);
}

JobHandles.Clear();
JobsCache.Clear();
}
InlineParallelChunkQuery(in description, innerJob);
}
""";

Expand Down Expand Up @@ -84,38 +53,7 @@ public static void AppendHpeParallelQuery(this StringBuilder builder, int amount
var innerJob = new IForEachWithEntityJob<T,{{generics}}>();
innerJob.ForEach = iForEach;

var pool = JobMeta<ChunkIterationJob<IForEachWithEntityJob<T,{{generics}}>>>.Pool;
var query = Query(in description);
foreach (var archetype in query.GetArchetypeIterator()) {

var archetypeSize = archetype.ChunkCount;
var part = new RangePartitioner(Environment.ProcessorCount, archetypeSize);
foreach (var range in part)
{
var job = pool.Get();
job.Start = range.Start;
job.Size = range.Length;
job.Chunks = archetype.Chunks;
job.Instance = innerJob;

var jobHandle = SharedJobScheduler.Schedule(job);
JobsCache.Add(job);
JobHandles.Add(jobHandle);
}

// Return jobs to pool
SharedJobScheduler.Flush();
JobHandle.CompleteAll(JobHandles.Span);

for (var index = 0; index < JobsCache.Count; index++)
{
var job = Unsafe.As<ChunkIterationJob<IForEachWithEntityJob<T,{{generics}}>>>(JobsCache[index]);
pool.Return(job);
}

JobHandles.Clear();
JobsCache.Clear();
}
InlineParallelChunkQuery(in description, innerJob);
}
""";

Expand Down
64 changes: 2 additions & 62 deletions src/Arch.SourceGen/Queries/ParallelQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,7 @@ public static StringBuilder AppendParallelQuery(this StringBuilder sb, int amoun
var innerJob = new ForEachJob<{{generics}}>();
innerJob.ForEach = forEach;

var pool = JobMeta<ChunkIterationJob<ForEachJob<{{generics}}>>>.Pool;
var query = Query(in description);
foreach (var archetype in query.GetArchetypeIterator()) {

var archetypeSize = archetype.ChunkCount;
var part = new RangePartitioner(Environment.ProcessorCount, archetypeSize);
foreach (var range in part)
{
var job = pool.Get();
job.Start = range.Start;
job.Size = range.Length;
job.Chunks = archetype.Chunks;
job.Instance = innerJob;

var jobHandle = SharedJobScheduler.Schedule(job);
JobsCache.Add(job);
JobHandles.Add(jobHandle);
}

SharedJobScheduler.Flush();
JobHandle.CompleteAll(JobHandles.Span);

for (var index = 0; index < JobsCache.Count; index++)
{
var job = Unsafe.As<ChunkIterationJob<ForEachJob<{{generics}}>>>(JobsCache[index]);
pool.Return(job);
}

JobHandles.Clear();
JobsCache.Clear();
}
InlineParallelChunkQuery(in description, innerJob);
}
""";

Expand Down Expand Up @@ -84,37 +54,7 @@ public static StringBuilder AppendParallelEntityQuery(this StringBuilder sb, int
var innerJob = new ForEachWithEntityJob<{{generics}}>();
innerJob.ForEach = forEach;

var pool = JobMeta<ChunkIterationJob<ForEachWithEntityJob<{{generics}}>>>.Pool;
var query = Query(in description);
foreach (var archetype in query.GetArchetypeIterator())
{
var archetypeSize = archetype.ChunkCount;
var part = new RangePartitioner(Environment.ProcessorCount, archetypeSize);
foreach (var range in part)
{
var job = pool.Get();
job.Start = range.Start;
job.Size = range.Length;
job.Chunks = archetype.Chunks;
job.Instance = innerJob;

var jobHandle = SharedJobScheduler.Schedule(job);
JobsCache.Add(job);
JobHandles.Add(jobHandle);
}

SharedJobScheduler.Flush();
JobHandle.CompleteAll(JobHandles.Span);

for (var index = 0; index < JobsCache.Count; index++)
{
var job = Unsafe.As<ChunkIterationJob<ForEachWithEntityJob<{{generics}}>>>(JobsCache[index]);
pool.Return(job);
}

JobHandles.Clear();
JobsCache.Clear();
}
InlineParallelChunkQuery(in description, innerJob);
}
""";

Expand Down
3 changes: 2 additions & 1 deletion src/Arch/Arch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
Fixed issue where Unsafe.As did not work on .Net 2.1 for the Dangerous-Utils.
Dangerous API now allows setting/getting of recycled ids.
Fixed archetype duplication after loading a save.
Fixed .Add when a newly non registered component was added. </PackageReleaseNotes>
Fixed .Add when a newly non registered component was added.
Now makes use of the updated and improved JobScheduler 1.1.1.</PackageReleaseNotes>
<PackageTags>c#;.net;.net6;.net7;ecs;game;entity;gamedev; game-development; game-engine; entity-component-system;stride;unity;godot;</PackageTags>

<PackageProjectUrl>https://github.com/genaray/Arch</PackageProjectUrl>
Expand Down

0 comments on commit 11ffdf3

Please sign in to comment.