This demo project is for support of this discussion post regarding performance impact when using Foreach.
The conclusion of this demo is:
Using foreach in a dll project will not bring performance lost
Thus, NO NEED to replace Foreach in a dll project.
- open ForeachDll solution in Monodevelop/Visual Studio, and build the project. The output dll will be copied automatically to Unity project
- open ForeachDemo project in Unity, open "TestScene" under "Scenes" folder
- open 'Profiler' window of Unity
- run Unity editor, after several frames, click on 'Profiler' to observe the result (should be as below)
- notice that, only "ForeachCompiled_InUnity" caused '40B' gc alloc, which is caused by a 'cast struct to interface'
- to dig deeper, you could build the project by il2cpp. Among all output cpp files, find "Bulk_Assembly-CSharp_0.cpp" and "Bulk_ForeachDll_0.cpp", and you could verify: