Skip to content

Commit eb51ee1

Browse files
authored
Re-ordered loops to reduce allocations for multiplexing jobs (#26)
1 parent 8ba27d7 commit eb51ee1

2 files changed

Lines changed: 176 additions & 154 deletions

File tree

src/EcsR3/Systems/Batching/Convention/Multiplexing/MultiplexingBatchedRefSystem.cs

Lines changed: 88 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,24 @@ protected override void ProcessGroup(ReadOnlyMemory<ComponentBatch<T>> component
5555
protected void ProcessJobsMultithreaded(ReadOnlyMemory<ComponentBatch<T>> componentBatches, T[] componentPools)
5656
{
5757
var closureBatches = componentBatches;
58-
foreach (var job in Jobs)
58+
59+
ThreadHandler.For(0, componentBatches.Length, i =>
5960
{
60-
ThreadHandler.For(0, componentBatches.Length, i =>
61-
{
62-
var batch = closureBatches.Span[i];
63-
job.Process(batch.Entity, ref componentPools[batch.Component1Allocation]);
64-
});
65-
}
61+
var batch = closureBatches.Span[i];
62+
foreach (var job in Jobs)
63+
{ job.Process(batch.Entity, ref componentPools[batch.Component1Allocation]); }
64+
});
6665
}
6766

6867
protected void ProcessJobs(ReadOnlyMemory<ComponentBatch<T>> componentBatches, T[] componentPools)
6968
{
7069
var batchesSpan = componentBatches.Span;
71-
foreach (var job in Jobs)
70+
71+
for(var i=0;i<batchesSpan.Length;i++)
7272
{
73-
for(var i=0;i<batchesSpan.Length;i++)
74-
{
75-
var batch = batchesSpan[i];
76-
job.Process(batch.Entity, ref componentPools[batch.Component1Allocation]);
77-
}
73+
var batch = batchesSpan[i];
74+
foreach (var job in Jobs)
75+
{ job.Process(batch.Entity, ref componentPools[batch.Component1Allocation]); }
7876
}
7977
}
8078
}
@@ -124,27 +122,30 @@ protected void ProcessJobsMultithreaded(ReadOnlyMemory<ComponentBatch<T1, T2>> c
124122
{
125123
var (components1, components2) = componentPools;
126124
var closureBatches = componentBatches;
127-
foreach (var job in Jobs)
125+
126+
ThreadHandler.For(0, componentBatches.Length, i =>
128127
{
129-
ThreadHandler.For(0, componentBatches.Length, i =>
128+
var batch = closureBatches.Span[i];
129+
foreach (var job in Jobs)
130130
{
131-
var batch = closureBatches.Span[i];
132131
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
133132
ref components2[batch.Component2Allocation]);
134-
});
135-
}
133+
}
134+
});
136135
}
137136

138137
protected void ProcessJobs(ReadOnlyMemory<ComponentBatch<T1, T2>> componentBatches, (T1[], T2[]) componentPools)
139138
{
140139
var (components1, components2) = componentPools;
141140
var batchesSpan = componentBatches.Span;
142-
foreach (var job in Jobs)
141+
142+
for(var i=0;i<batchesSpan.Length;i++)
143143
{
144-
for(var i=0;i<batchesSpan.Length;i++)
144+
var batch = batchesSpan[i];
145+
foreach (var job in Jobs)
145146
{
146-
var batch = batchesSpan[i];
147-
job.Process(batch.Entity, ref components1[batch.Component1Allocation], ref components2[batch.Component2Allocation]);
147+
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
148+
ref components2[batch.Component2Allocation]);
148149
}
149150
}
150151
}
@@ -195,27 +196,30 @@ protected void ProcessJobsMultithreaded(ReadOnlyMemory<ComponentBatch<T1, T2, T3
195196
{
196197
var (components1, components2, components3) = componentPools;
197198
var scopedComponentBatches = componentBatches;
198-
foreach (var job in Jobs)
199+
200+
ThreadHandler.For(0, componentBatches.Length, i =>
199201
{
200-
ThreadHandler.For(0, componentBatches.Length, i =>
202+
var batch = scopedComponentBatches.Span[i];
203+
foreach (var job in Jobs)
201204
{
202-
var batch = scopedComponentBatches.Span[i];
203205
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
204206
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation]);
205-
});
206-
}
207+
}
208+
});
207209
}
208210

209211
protected void ProcessJobs(ReadOnlyMemory<ComponentBatch<T1, T2, T3>> componentBatches, (T1[], T2[], T3[]) componentPools)
210212
{
211213
var (components1, components2, components3) = componentPools;
212214
var batchesSpan = componentBatches.Span;
213-
foreach (var job in Jobs)
215+
216+
for (var i = 0; i < batchesSpan.Length; i++)
214217
{
215-
for(var i=0;i<batchesSpan.Length;i++)
218+
var batch = batchesSpan[i];
219+
foreach (var job in Jobs)
216220
{
217-
var batch = batchesSpan[i];
218-
job.Process(batch.Entity, ref components1[batch.Component1Allocation], ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation]);
221+
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
222+
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation]);
219223
}
220224
}
221225
}
@@ -265,29 +269,31 @@ protected void ProcessJobsMultithreaded(ReadOnlyMemory<ComponentBatch<T1, T2, T3
265269
{
266270
var (components1, components2, components3, components4) = componentPools;
267271
var scopedComponentBatches = componentBatches;
268-
foreach (var job in Jobs)
272+
273+
ThreadHandler.For(0, componentBatches.Length, i =>
269274
{
270-
ThreadHandler.For(0, componentBatches.Length, i =>
275+
var batch = scopedComponentBatches.Span[i];
276+
foreach (var job in Jobs)
271277
{
272-
var batch = scopedComponentBatches.Span[i];
273278
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
274-
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
279+
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
275280
ref components4[batch.Component4Allocation]);
276-
});
277-
}
281+
}
282+
});
278283
}
279284

280285
protected void ProcessJobs(ReadOnlyMemory<ComponentBatch<T1, T2, T3, T4>> componentBatches, (T1[], T2[], T3[], T4[]) componentPools)
281286
{
282287
var (components1, components2, components3, components4) = componentPools;
283288
var batchesSpan = componentBatches.Span;
284-
foreach (var job in Jobs)
289+
290+
for(var i=0;i<batchesSpan.Length;i++)
285291
{
286-
for(var i=0;i<batchesSpan.Length;i++)
292+
var batch = batchesSpan[i];
293+
foreach (var job in Jobs)
287294
{
288-
var batch = batchesSpan[i];
289-
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
290-
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
295+
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
296+
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
291297
ref components4[batch.Component4Allocation]);
292298
}
293299
}
@@ -338,30 +344,31 @@ protected void ProcessJobsMultithreaded(ReadOnlyMemory<ComponentBatch<T1, T2, T3
338344
{
339345
var (components1, components2, components3, components4, components5) = componentPools;
340346
var scopedComponentBatches = componentBatches;
341-
foreach (var job in Jobs)
347+
348+
ThreadHandler.For(0, componentBatches.Length, i =>
342349
{
343-
ThreadHandler.For(0, componentBatches.Length, i =>
350+
var batch = scopedComponentBatches.Span[i];
351+
foreach (var job in Jobs)
344352
{
345-
var batch = scopedComponentBatches.Span[i];
346353
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
347-
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
354+
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
348355
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation]);
349-
});
350-
}
356+
}
357+
});
351358
}
352359

353360
protected void ProcessJobs(ReadOnlyMemory<ComponentBatch<T1, T2, T3, T4, T5>> componentBatches, (T1[], T2[], T3[], T4[], T5[]) componentPools)
354361
{
355362
var (components1, components2, components3, components4, components5) = componentPools;
356-
357363
var batchesSpan = componentBatches.Span;
358-
foreach (var job in Jobs)
364+
365+
for(var i=0;i<batchesSpan.Length;i++)
359366
{
360-
for(var i=0;i<batchesSpan.Length;i++)
367+
var batch = batchesSpan[i];
368+
foreach (var job in Jobs)
361369
{
362-
var batch = batchesSpan[i];
363370
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
364-
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
371+
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
365372
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation]);
366373
}
367374
}
@@ -412,31 +419,33 @@ protected void ProcessJobsMultithreaded(ReadOnlyMemory<ComponentBatch<T1, T2, T3
412419
{
413420
var (components1, components2, components3, components4, components5, components6) = componentPools;
414421
var scopedComponentBatches = componentBatches;
415-
foreach (var job in Jobs)
422+
423+
ThreadHandler.For(0, componentBatches.Length, i =>
416424
{
417-
ThreadHandler.For(0, componentBatches.Length, i =>
425+
var batch = scopedComponentBatches.Span[i];
426+
foreach (var job in Jobs)
418427
{
419-
var batch = scopedComponentBatches.Span[i];
420428
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
421-
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
422-
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation],
429+
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
430+
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation],
423431
ref components6[batch.Component6Allocation]);
424-
});
425-
}
432+
}
433+
});
426434
}
427435

428436
protected void ProcessJobs(ReadOnlyMemory<ComponentBatch<T1, T2, T3, T4, T5, T6>> componentBatches, (T1[], T2[], T3[], T4[], T5[], T6[]) componentPools)
429437
{
430438
var (components1, components2, components3, components4, components5, components6) = componentPools;
431439
var batchesSpan = componentBatches.Span;
432-
foreach (var job in Jobs)
440+
441+
for(var i=0;i<batchesSpan.Length;i++)
433442
{
434-
for(var i=0;i<batchesSpan.Length;i++)
443+
var batch = batchesSpan[i];
444+
foreach (var job in Jobs)
435445
{
436-
var batch = batchesSpan[i];
437446
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
438-
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
439-
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation],
447+
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
448+
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation],
440449
ref components6[batch.Component6Allocation]);
441450
}
442451
}
@@ -487,31 +496,33 @@ protected void ProcessJobsMultithreaded(ReadOnlyMemory<ComponentBatch<T1, T2, T3
487496
{
488497
var (components1, components2, components3, components4, components5, components6, components7) = componentPools;
489498
var scopedComponentBatches = componentBatches;
490-
foreach (var job in Jobs)
499+
500+
ThreadHandler.For(0, componentBatches.Length, i =>
491501
{
492-
ThreadHandler.For(0, componentBatches.Length, i =>
502+
var batch = scopedComponentBatches.Span[i];
503+
foreach (var job in Jobs)
493504
{
494-
var batch = scopedComponentBatches.Span[i];
495505
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
496-
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
497-
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation],
506+
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
507+
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation],
498508
ref components6[batch.Component6Allocation], ref components7[batch.Component7Allocation]);
499-
});
500-
}
509+
}
510+
});
501511
}
502512

503513
protected void ProcessJobs(ReadOnlyMemory<ComponentBatch<T1, T2, T3, T4, T5, T6, T7>> componentBatches, (T1[], T2[], T3[], T4[], T5[], T6[], T7[]) componentPools)
504514
{
505515
var (components1, components2, components3, components4, components5, components6, components7) = componentPools;
506516
var batchesSpan = componentBatches.Span;
507-
foreach (var job in Jobs)
517+
518+
for(var i=0;i<batchesSpan.Length;i++)
508519
{
509-
for(var i=0;i<batchesSpan.Length;i++)
520+
var batch = batchesSpan[i];
521+
foreach (var job in Jobs)
510522
{
511-
var batch = batchesSpan[i];
512523
job.Process(batch.Entity, ref components1[batch.Component1Allocation],
513-
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
514-
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation],
524+
ref components2[batch.Component2Allocation], ref components3[batch.Component3Allocation],
525+
ref components4[batch.Component4Allocation], ref components5[batch.Component5Allocation],
515526
ref components6[batch.Component6Allocation], ref components7[batch.Component7Allocation]);
516527
}
517528
}

0 commit comments

Comments
 (0)