Skip to content

Commit 75d6b33

Browse files
committed
Merge branch 'use-pinning-for-structs'
2 parents b32afa3 + 7d69da6 commit 75d6b33

File tree

19 files changed

+364
-166
lines changed

19 files changed

+364
-166
lines changed

src/EcsRx.Examples/ExampleApps/Playground/Components/StructComponent2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace EcsRx.Examples.ExampleApps.Playground.Components
66
[StructLayout(LayoutKind.Sequential)]
77
public struct StructComponent2 : IComponent
88
{
9-
public bool IsTrue { get; set; }
10-
public int Value { get; set; }
9+
public byte IsTrue;
10+
public int Value;
1111
}
1212
}

src/EcsRx.Examples/ExampleApps/Playground/StructBased/Struct1Application.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected override void RunProcess()
2727

2828
ref var basicComponent2 = ref entity.GetComponent<StructComponent2>(StructComponent2TypeId);
2929
basicComponent2.Value += 10;
30-
basicComponent2.IsTrue = true;
30+
basicComponent2.IsTrue = 1;
3131
}
3232
}
3333
}

src/EcsRx.Examples/ExampleApps/Playground/StructBased/Struct2Application.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected override void RunProcess()
3333

3434
ref var basicComponent2 = ref entity.GetComponent<StructComponent2>(StructComponent2TypeId);
3535
basicComponent2.Value += 10;
36-
basicComponent2.IsTrue = true;
36+
basicComponent2.IsTrue = 1;
3737
}
3838
}
3939
}

src/EcsRx.Examples/ExampleApps/Playground/StructBased/Struct3Application.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected override void RunProcess()
4040

4141
ref var basicComponent2 = ref Components2[entity.ComponentAllocations[StructComponent2TypeId]];
4242
basicComponent2.Value += 10;
43-
basicComponent2.IsTrue = true;
43+
basicComponent2.IsTrue = 1;
4444
}
4545
}
4646
}

src/EcsRx.Examples/ExampleApps/Playground/StructBased/Struct3BApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected override void RunProcess()
4747
Components2[component2Allocation] = new StructComponent2
4848
{
4949
Value = basicComponent2.Value + 10,
50-
IsTrue = true
50+
IsTrue = 1
5151
};
5252
}
5353
}

src/EcsRx.Examples/ExampleApps/Playground/StructBased/Struct3CApplication.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected override void RunProcess()
4747
Components2[component2Allocation] = new StructComponent2
4848
{
4949
Value = basicComponent2.Value + 10,
50-
IsTrue = true
50+
IsTrue = 1
5151
};
5252
}
5353
}

src/EcsRx.Examples/ExampleApps/Playground/StructBased/Struct4Application.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace EcsRx.Examples.ExampleApps.Playground.StructBased
77
{
88
public class Struct4Application : BasicLoopApplication
99
{
10-
private Batch<StructComponent, StructComponent2>[] _componentBatch;
10+
private PinnedBatch<StructComponent, StructComponent2> _componentBatch;
1111

1212
protected override void SetupEntities()
1313
{
@@ -30,15 +30,15 @@ protected override void SetupEntity(IEntity entity)
3030

3131
protected override void RunProcess()
3232
{
33-
for (var i = _componentBatch.Length - 1; i >= 0; i--)
33+
for (var i = _componentBatch.Batches.Length - 1; i >= 0; i--)
3434
unsafe
3535
{
36-
ref var batch = ref _componentBatch[i];
36+
ref var batch = ref _componentBatch.Batches[i];
3737
ref var basic = ref *batch.Component1;
3838
ref var basic2 = ref *batch.Component2;
3939
basic.Position += Vector3.One;
4040
basic.Something += 10;
41-
basic2.IsTrue = true;
41+
basic2.IsTrue = 1;
4242
basic2.Value += 10;
4343
}
4444
}

src/EcsRx.Examples/ExampleApps/Playground/StructBased/Struct4BApplication.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace EcsRx.Examples.ExampleApps.Playground.StructBased
88
{
99
public class Struct4BApplication : BasicLoopApplication
1010
{
11-
private Batch<StructComponent, StructComponent2>[] _componentBatch;
11+
private PinnedBatch<StructComponent, StructComponent2> _componentBatch;
1212

1313
protected override void SetupEntities()
1414
{
@@ -31,16 +31,16 @@ protected override void SetupEntity(IEntity entity)
3131

3232
protected override void RunProcess()
3333
{
34-
Parallel.For(0, _componentBatch.Length, i =>
34+
Parallel.For(0, _componentBatch.Batches.Length, i =>
3535
{
3636
unsafe
3737
{
38-
ref var batch = ref _componentBatch[i];
38+
ref var batch = ref _componentBatch.Batches[i];
3939
ref var basic = ref *batch.Component1;
4040
ref var basic2 = ref *batch.Component2;
4141
basic.Position += Vector3.One;
4242
basic.Something += 10;
43-
basic2.IsTrue = true;
43+
basic2.IsTrue = 1;
4444
basic2.Value += 10;
4545
}
4646
});

src/EcsRx.Examples/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void Main(string[] args)
4242
//new Struct3Application().StartApplication();
4343
//new Class4Application().StartApplication();
4444
//new Struct4Application().StartApplication();
45-
//new Struct4BApplication().StartApplication();
45+
new Struct4BApplication().StartApplication();
4646

4747
}
4848
}

src/EcsRx.Plugins.Batching/Accessors/BatchAccessor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class BatchAccessor<T1, T2> : BatchAccessor, IBatchAccessor<T1, T2>
5252
where T1 : unmanaged, IComponent
5353
where T2 : unmanaged, IComponent
5454
{
55-
public Batch<T1, T2>[] Batch { get; private set; }
55+
public PinnedBatch<T1, T2> Batch { get; private set; }
5656

5757
public BatchAccessor(IObservableGroup observableGroup, IComponentDatabase componentDatabase, IBatchBuilder batchBuilder, IComponentTypeLookup componentTypeLookup)
5858
: base(observableGroup, componentDatabase, batchBuilder, componentTypeLookup)
@@ -82,7 +82,7 @@ public class BatchAccessor<T1, T2, T3> : BatchAccessor, IBatchAccessor<T1, T2, T
8282
where T2 : unmanaged, IComponent
8383
where T3 : unmanaged, IComponent
8484
{
85-
public Batch<T1, T2, T3>[] Batch { get; private set; }
85+
public PinnedBatch<T1, T2, T3> Batch { get; private set; }
8686

8787
public BatchAccessor(IObservableGroup observableGroup, IComponentDatabase componentDatabase, IBatchBuilder batchBuilder, IComponentTypeLookup componentTypeLookup)
8888
: base(observableGroup, componentDatabase, batchBuilder, componentTypeLookup)
@@ -116,7 +116,7 @@ public class BatchAccessor<T1, T2, T3, T4> : BatchAccessor, IBatchAccessor<T1, T
116116
where T3 : unmanaged, IComponent
117117
where T4 : unmanaged, IComponent
118118
{
119-
public Batch<T1, T2, T3, T4>[] Batch { get; private set; }
119+
public PinnedBatch<T1, T2, T3, T4> Batch { get; private set; }
120120

121121
public BatchAccessor(IObservableGroup observableGroup, IComponentDatabase componentDatabase, IBatchBuilder batchBuilder, IComponentTypeLookup componentTypeLookup)
122122
: base(observableGroup, componentDatabase, batchBuilder, componentTypeLookup)
@@ -154,7 +154,7 @@ public class BatchAccessor<T1, T2, T3, T4, T5> : BatchAccessor, IBatchAccessor<T
154154
where T4 : unmanaged, IComponent
155155
where T5 : unmanaged, IComponent
156156
{
157-
public Batch<T1, T2, T3, T4, T5>[] Batch { get; private set; }
157+
public PinnedBatch<T1, T2, T3, T4, T5> Batch { get; private set; }
158158

159159
public BatchAccessor(IObservableGroup observableGroup, IComponentDatabase componentDatabase, IBatchBuilder batchBuilder, IComponentTypeLookup componentTypeLookup)
160160
: base(observableGroup, componentDatabase, batchBuilder, componentTypeLookup)
@@ -196,7 +196,7 @@ public class BatchAccessor<T1, T2, T3, T4, T5, T6> : BatchAccessor, IBatchAccess
196196
where T5 : unmanaged, IComponent
197197
where T6 : unmanaged, IComponent
198198
{
199-
public Batch<T1, T2, T3, T4, T5, T6>[] Batch { get; private set; }
199+
public PinnedBatch<T1, T2, T3, T4, T5, T6> Batch { get; private set; }
200200

201201
public BatchAccessor(IObservableGroup observableGroup, IComponentDatabase componentDatabase, IBatchBuilder batchBuilder, IComponentTypeLookup componentTypeLookup)
202202
: base(observableGroup, componentDatabase, batchBuilder, componentTypeLookup)

src/EcsRx.Plugins.Batching/Accessors/IBatchAccessor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public interface IBatchAccessor<T1, T2> : IBatchAccessor
1212
where T1 : unmanaged, IComponent
1313
where T2 : unmanaged, IComponent
1414
{
15-
Batch<T1, T2>[] Batch { get; }
15+
PinnedBatch<T1, T2> Batch { get; }
1616
}
1717

1818
public interface IBatchAccessor<T1, T2, T3> : IBatchAccessor
1919
where T1 : unmanaged, IComponent
2020
where T2 : unmanaged, IComponent
2121
where T3 : unmanaged, IComponent
2222
{
23-
Batch<T1, T2, T3>[] Batch { get; }
23+
PinnedBatch<T1, T2, T3> Batch { get; }
2424
}
2525

2626
public interface IBatchAccessor<T1, T2, T3, T4> : IBatchAccessor
@@ -29,7 +29,7 @@ public interface IBatchAccessor<T1, T2, T3, T4> : IBatchAccessor
2929
where T3 : unmanaged, IComponent
3030
where T4 : unmanaged, IComponent
3131
{
32-
Batch<T1, T2, T3, T4>[] Batch { get; }
32+
PinnedBatch<T1, T2, T3, T4> Batch { get; }
3333
}
3434

3535
public interface IBatchAccessor<T1, T2, T3, T4, T5> : IBatchAccessor
@@ -39,7 +39,7 @@ public interface IBatchAccessor<T1, T2, T3, T4, T5> : IBatchAccessor
3939
where T4 : unmanaged, IComponent
4040
where T5 : unmanaged, IComponent
4141
{
42-
Batch<T1, T2, T3, T4, T5>[] Batch { get; }
42+
PinnedBatch<T1, T2, T3, T4, T5> Batch { get; }
4343
}
4444

4545
public interface IBatchAccessor<T1, T2, T3, T4, T5, T6> : IBatchAccessor
@@ -50,7 +50,7 @@ public interface IBatchAccessor<T1, T2, T3, T4, T5, T6> : IBatchAccessor
5050
where T5 : unmanaged, IComponent
5151
where T6 : unmanaged, IComponent
5252
{
53-
Batch<T1, T2, T3, T4, T5, T6>[] Batch { get; }
53+
PinnedBatch<T1, T2, T3, T4, T5, T6> Batch { get; }
5454
}
5555

5656
}

src/EcsRx.Plugins.Batching/Batches/Batch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using EcsRx.Components;
44

55
namespace EcsRx.Plugins.Batching.Batches
6-
{
6+
{
77
[StructLayout(LayoutKind.Sequential)]
88
public unsafe struct Batch<T1, T2> : IEquatable<Batch<T1, T2>>
99
where T1 : unmanaged, IComponent
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using EcsRx.Components;
4+
5+
namespace EcsRx.Plugins.Batching.Batches
6+
{
7+
[StructLayout(LayoutKind.Sequential)]
8+
public struct PinnedBatch<T1, T2> : IDisposable
9+
where T1 : unmanaged, IComponent
10+
where T2 : unmanaged, IComponent
11+
{
12+
public readonly Batch<T1, T2>[] Batches;
13+
public readonly GCHandle[] Handles;
14+
15+
public PinnedBatch(Batch<T1, T2>[] batches, GCHandle[] handles)
16+
{
17+
Batches = batches;
18+
Handles = handles;
19+
}
20+
21+
public void Dispose()
22+
{
23+
if (Handles == null) { return; }
24+
foreach (var handle in Handles)
25+
{
26+
if(handle.IsAllocated)
27+
{ handle.Free(); }
28+
}
29+
}
30+
}
31+
32+
[StructLayout(LayoutKind.Sequential)]
33+
public struct PinnedBatch<T1, T2, T3> : IDisposable
34+
where T1 : unmanaged, IComponent
35+
where T2 : unmanaged, IComponent
36+
where T3 : unmanaged, IComponent
37+
{
38+
public readonly Batch<T1, T2, T3>[] Batches;
39+
public readonly GCHandle[] Handles;
40+
41+
public PinnedBatch(Batch<T1, T2, T3>[] batches, GCHandle[] handles)
42+
{
43+
Batches = batches;
44+
Handles = handles;
45+
}
46+
47+
public void Dispose()
48+
{
49+
if (Handles == null) { return; }
50+
foreach (var handle in Handles)
51+
{
52+
if(handle.IsAllocated)
53+
{ handle.Free(); }
54+
}
55+
}
56+
}
57+
58+
[StructLayout(LayoutKind.Sequential)]
59+
public struct PinnedBatch<T1, T2, T3, T4> : IDisposable
60+
where T1 : unmanaged, IComponent
61+
where T2 : unmanaged, IComponent
62+
where T3 : unmanaged, IComponent
63+
where T4 : unmanaged, IComponent
64+
{
65+
public readonly Batch<T1, T2, T3, T4>[] Batches;
66+
public readonly GCHandle[] Handles;
67+
68+
public PinnedBatch(Batch<T1, T2, T3, T4>[] batches, GCHandle[] handles)
69+
{
70+
Batches = batches;
71+
Handles = handles;
72+
}
73+
74+
public void Dispose()
75+
{
76+
if (Handles == null) { return; }
77+
foreach (var handle in Handles)
78+
{
79+
if(handle.IsAllocated)
80+
{ handle.Free(); }
81+
}
82+
}
83+
}
84+
85+
[StructLayout(LayoutKind.Sequential)]
86+
public struct PinnedBatch<T1, T2, T3, T4, T5> : IDisposable
87+
where T1 : unmanaged, IComponent
88+
where T2 : unmanaged, IComponent
89+
where T3 : unmanaged, IComponent
90+
where T4 : unmanaged, IComponent
91+
where T5 : unmanaged, IComponent
92+
{
93+
public readonly Batch<T1, T2, T3, T4, T5>[] Batches;
94+
public readonly GCHandle[] Handles;
95+
96+
public PinnedBatch(Batch<T1, T2, T3, T4, T5>[] batches, GCHandle[] handles)
97+
{
98+
Batches = batches;
99+
Handles = handles;
100+
}
101+
102+
public void Dispose()
103+
{
104+
if (Handles == null) { return; }
105+
foreach (var handle in Handles)
106+
{
107+
if(handle.IsAllocated)
108+
{ handle.Free(); }
109+
}
110+
}
111+
}
112+
113+
[StructLayout(LayoutKind.Sequential)]
114+
public struct PinnedBatch<T1, T2, T3, T4, T5, T6> : IDisposable
115+
where T1 : unmanaged, IComponent
116+
where T2 : unmanaged, IComponent
117+
where T3 : unmanaged, IComponent
118+
where T4 : unmanaged, IComponent
119+
where T5 : unmanaged, IComponent
120+
where T6 : unmanaged, IComponent
121+
{
122+
public readonly Batch<T1, T2, T3, T4, T5, T6>[] Batches;
123+
public readonly GCHandle[] Handles;
124+
125+
public PinnedBatch(Batch<T1, T2, T3, T4, T5, T6>[] batches, GCHandle[] handles)
126+
{
127+
Batches = batches;
128+
Handles = handles;
129+
}
130+
131+
public void Dispose()
132+
{
133+
if (Handles == null) { return; }
134+
foreach (var handle in Handles)
135+
{
136+
if(handle.IsAllocated)
137+
{ handle.Free(); }
138+
}
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)