Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Code Optimization Practices

Discussion in 'Scripting' started by hamsterbytedev, May 10, 2015.

?

How familiar are you with code optimization?

  1. Very familiar; I always keep optimization in mind and try to use the best practices.

    20 vote(s)
    48.8%
  2. Somewhat familiar; I know it is a big deal and I do what I can.

    12 vote(s)
    29.3%
  3. I've heard of it; I know what it is, I know it is a big deal, but I've not done much.

    5 vote(s)
    12.2%
  4. What's optimization? Bro, I have Quad SLI Titan X's, 64GB of DDR4, and a 5970K. Who cares!

    4 vote(s)
    9.8%
  1. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yes and no. Yes, memory allocation and garbage collection is important for micro optimisation. No your test didn't demonstrate this.

    How most of the collections work internally is my allocating a fixed size in memory for the collection. When this size is overflowed, a new area of memory is allocated, typically twice the original size, and the days is copied from one spot to the other.

    In your example without preallocation this resizing operation would have happened many times. Resulting in performance losses due to allocation, garbage collection and the actual copying.

    So you have showed preallocation of large collection is important. You haven't conclusively proven why.

    If you are going to hunt down micro optimisations at this level it's important to understand what's happening under the hood. And then under the next hood. For several layers down. I still maintain it's a waste of good development time in most cases, but if you are going to do it you might as well get it right.
     
    hamsterbytedev likes this.
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Exactly, the garbage collector can kill your performance if you are not careful. This is why Instantiate and Destroy are so detrimental to games and I always pool my objects at the beginning of a scene or game.
     
  3. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    lol, I suppose you're right. This kind of micro optimization is pretty feeble anyway. As it has been said many times the best thing to do is just always be profiling. Make yourself aware of good practices and and use them when and where you can. You should save this anal stuff for the final stages of optimization and should only even consider changing something if it is causing problems in your software or you find something that performs better.

    So, basically what you are saying is that when you preallocate the size of a collection the performance increase is related to a lack of recursion. Instead of recursively allocating bytes and copying the previous data, it knows how many bytes are needed and populates those bytes in a single execution?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Pretty much yes. To test memory allocation and GC you could run something like this.

    Code (CSharp):
    1. public class Dummy {
    2.     int x;
    3. }
    4.  
    5. for (1 to 10000){
    6.     Dummy dummy = new Dummy ();
    7. }
    Like I said, I don't get this low level very often, so even that might have hidden traps I'm not aware of.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    The main goal with optimizing in Unity is avoiding feeding the gc. After a short while it becomes relatively clear who the worst culprits are. I very rarely fire the up the profiler these days as I generally know what to avoid.
     
    hamsterbytedev and Kiwasi like this.