Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by saffronsnail · Aug 27, 2015 at 10:24 PM · c#unity 5javascriptunity5

Do we still need to avoid foreach loops?

While searching for a solution to a bug in my game I ran across this page saying that foreach loops are buggy and shouldn't be used. However, this post is a couple of years old. Does anybody know if this bug still exists?

Additionally, will the unityscript's for (var in container) syntax have the same problems since they compile to the same bytecode?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by pako · Aug 28, 2015 at 10:37 AM

I read the forum thread in your link, and I think the juice in this issue is that:

  1. Unity uses and old version of Mono

  2. This old version of Mono boxes structs collections, and boxing/unboxing is a "heavy" operation

Therefore, if you don't use struct collections, this issue does not apply. And if you use struct collections minimally, and infrequently, the impact on performance will be minimal and most probably unnoticeable.

A mono upgrade in Unity has not happened yet, and will not happen until IL2CPP is mature and used on all required platforms:

http://answers.unity3d.com/questions/924021/what-is-the-version-of-net-in-unity-5.html

https://www.reddit.com/r/Unity3D/comments/2e4q6c/unity_upgrading_to_c_5_and_net_45/

Here is a link to Unity Roadmap, if you want to follow the developments:

http://unity3d.com/unity/roadmap

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by mikelortega · Aug 28, 2015 at 08:44 AM

The way the compiler handles iterators in foreach loops generates unnecessary memory allocations. Remember you should reduce memory allocations to minimize the Garbage Collector load. It usually won't be a problem when you use foreach to do initializations, but it may allocate too much memory when called constantly (every frame).

I think you will have the same problem with UnityScript's for (var in container)

I recommend you to check the GC Alloc column in the Profiler. It will tell you when allocations are made in your foreach loops.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dave-Carlile · Aug 28, 2015 at 12:50 PM 0
Share

The memory allocations are due to a bug in the old version of $$anonymous$$ono that Unity is still using, not because of unrolling loops. See @Pako's answer.

avatar image
0

Answer by andrew-fray · Jul 31, 2017 at 01:28 PM

They upgrade the mono compiler (but not the runtime) in 5.5 (https://blogs.unity3d.com/2016/11/29/unity-5-5-is-ready-for-you/) and that means foreach is much better, including being 0-alloc for Lists: http://jacksondunstan.com/articles/3805

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is C# or javascript easier to understand? 2 Answers

Master Audio source volume control with slider 1 Answer

How to make a sprite retract to its original position? 0 Answers

Accessing GameObjects when changing scenes (levels) 1 Answer

Distance is always zero 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges