Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'foreach' still bad in 'modern' unity ( >= 5.4.x)?

Tags:

unity3d

Historically, one of the taboo constructs in Unity3D was the use of c# foreach block because each iteration of the loop would consume a few bytes of data uncessarily.

Curious if that's still the case on modern releases?

My Googling and (very rudimentary) testing is coming up with non-conclusive results and I'd like to leach off of someone else's knowledge here rather than dive into low-level benchmarking myself. :-)

Has anyone poked at this recently to determine if it's still necessary to avoid foreach in Unity3D?

like image 943
slumtrimpet Avatar asked Nov 08 '16 18:11

slumtrimpet


1 Answers

No, foreach is no longer bad in 'modern' unity but the fix is not yet fully released.

It was first fixed on a special build Unity Patch 5.3.5p8 on early July, which you can get from here. This is a special Edition you must get to receive that fix.

Now, Unity upgraded their compiler to Mono 4.4 on Unity 5.5 beta release which is not yet final release. It fixed the foreach problem. Once this version is released in the coming months, foreach memory allocation will be a thing of the past. You can still download for testing purposes but don't release a game with a beta build.

Scripting: Upgrade C# compiler to Mono 4.4. The new compiler still targets C# 4 and .Net 3.5, but provides better performance and many bug fixes.

It is now fixed but the fix is still in beta mode.

like image 101
Programmer Avatar answered Nov 06 '22 16:11

Programmer