Is 'foreach' still bad in 'modern'

2019-04-25 08:43发布

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?

标签: unity3d
1条回答
我命由我不由天
2楼-- · 2019-04-25 09:23

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.

查看更多
登录 后发表回答