Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
avatar image
0
Question by LunaTrap · Aug 11, 2015 at 04:19 PM · performancelistperformance optimizationgarbage-collection

List Performance Question

Hi! i have a question about, if you could come up with a better optimized way to do this

what it happens is that, my AI cast a sphere, to detects the colliders of waypoints around it, and then save all the detected waypoint colliders in a list, but if you look at the profiler sample, this actions call the GC a lot, and it makes 3% of cpu time from only 6 AIs, is there a better way to do this? does the GC comes from the List.Clear() ? is there a better way?

 protected virtual void DetectWaypoints()
         {
             Profiler.BeginSample("Waypoints");
             waypoints.Clear();
     
             foreach (Collider col in Physics.OverlapSphere(this.transform.position, 5f))
             {
                 if (col.tag == "WayPointAI")
                 {
                     waypoints.Add(col);
                 }
             }
     
             Profiler.EndSample();
         }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by YoungDeveloper · Aug 11, 2015 at 04:48 PM

If there's nothing much happening in your scene, those 3% percent isn't a big deal really, as it's pretty much just a percentage considering everything in the scene. Instead you should look at the spikes and allocation.

Physics.OverlapSphere returns a new collection on each call, so there's really no way you can cache that list and clear() (only List reference). If only you'd write your own overlap sphere.

It's quite funny because if you want to talk or actually optimize things, there much much more than just such visible list clear things. For example, while and primitive for is very very slightly faster than foreach, and strings you are comparing to "Waypoints" and "WayPointAI" - you are actually creating new string each that time. So thats 9*2=18 + 10*2=20 == 38 bytes per frame. So at the end, if you are calling DetectWaypoints() every frame in update, you are generating 38 bytes or useless GC trash every frame, thats aproximately 2300 bytes per second of GC trash.. so... yeah.

Comment
Add comment · Show 4 · 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 LunaTrap · Aug 11, 2015 at 04:57 PM 0
Share

thanks, do For and While really are better thans ForEach?

the string Waypoint is for the sample, so that does not counts, and the string i normally store it in a const string on another class

a class that hold all my tags in const strings

DetectWaypoints() is not called every frame, this is called once, every random amount of seconds

so your suggestion is to change For ins$$anonymous$$d of Foreach?

avatar image YoungDeveloper · Aug 11, 2015 at 05:02 PM 1
Share

Thats a micro optimization, and doesnt really matter, but if you want to feel better, you can change it :D

avatar image Jessespike · Aug 11, 2015 at 05:35 PM 1
Share

Physics.OverlapSphere is being called every iteration. Try calling OverlapSphere once and store the results, then iterate through that.

  Collider[] waypointColliders = Physics.OverlapSphere(this.transform.position, 5f);
  for(int i = 0, length = waypointColliders.Length; i < length; ++i) 
  {
avatar image LunaTrap · Aug 11, 2015 at 05:49 PM 0
Share

thats a very good point! thanks a lot!

avatar image
1

Answer by LunaTrap · Aug 11, 2015 at 07:04 PM

I think is just reduced the performance hit on my problem above, thanks to the help from Jessepike and Young Developer

so im answering my self in case someone is reading this in search of an answer, i get this results after changing the code above, to this onem the cpu use % did not changed much but the GC came down from 4KB to 1.4KB

 protected virtual void DetectWaypoints()
     {
         Profiler.BeginSample("Waypoints");
         waypoints.Clear();
 
         waypointColliders = Physics.OverlapSphere(this.transform.position, 5f, waypointsLayer);
 
         for (int i = 0, length = waypointColliders.Length; i < length; ++i)
         {
             if (waypointColliders[i].tag == "WayPointAI")
             {
                 waypoints.Add(waypointColliders[i]);
             }
         }
 
         Profiler.EndSample();
     }
Comment
Add comment · Show 2 · 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 Eric5h5 · Aug 11, 2015 at 07:12 PM 1
Share

Use GameObject.CompareTag ins$$anonymous$$d of string comparisons.

avatar image LunaTrap · Aug 11, 2015 at 07:15 PM 0
Share

does it helps? i know strings are very heavy

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

25 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

Related Questions

How to improve the Performance of Removing Trees during runtime? 1 Answer

A node in a childnode? 1 Answer

Reasonable heap alloc. per second and total ? 0 Answers

Performance of one big object vs lots of smaller objects 0 Answers

Question regarding TileMap Performance 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