Search Unity

Official Unite Now - Optimization Tips for Maximum Performance – Part 1

Discussion in 'General Discussion' started by AskCarol, Jun 11, 2020.

  1. AskCarol

    AskCarol

    Joined:
    Nov 12, 2019
    Posts:
    234
    Feel free to post your questions before, after and during the session.


    Unite Now Session

    Unity evangelist Arturo Núñez knows games inside and out. In the first session of this two-part series, profit from Arturo’s experience helping game developers optimize performance. He’ll cover the underlying architecture of Unity’s core systems so you’ll know how to get the most out of Unity and your game.




    When
    Thu, June 18, 9:00 AM PT




    Where
    You can watch the session here!
    Make sure to sign up to get access to all Unite Now content.


    ______________________


    A team of product experts from across Unity will be available during and after the session. Our Community team will continue to field questions to foster an ongoing discussion with the community. Please feel free to ask your questions in this thread.


    Some basic rules

    • Only questions related to the topics of the session are permitted.


    • All questions will be fielded by our Community Managers (@LeonhardP and @AskCarol)
      • Replies will have to be approved by the moderators to show up in the thread.


      • Once approved, the questions will be forwarded to the relevant experts.

    We really look forward to hearing from the community.

    Thank you!
     
    Last edited: Jun 23, 2020
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Thanks Arturo for the presentation and thanks Carol for this forum post. I'd have missed the session if you didn't post it here. :)

    I would like to know why is Unity Technologies repeating themselves, rather than fixing the issues you want us to workaround?

    There are a few slides in the presentation where I believe Unity could just fix/solve it on the engine side, rather than relying on every customer to work around it.

    I just pick one example to get the idea across, you can apply this to more things presented in this, and other Unite sessions.

    upload_2020-6-20_10-11-22.png

    You talk about "GameObjectB" getting transformed, even though it wouldn't need and the recommendation is to restructure the hierarchy.

    A specific hierarchy often makes sense, e.g. to communicate relationship.

    The solution to this specific problem shouldn't be the recommendation that customers have to rethink/reorganize their hierarchies. The solution should be to find a way to allow us to use such hierarchies, without sacrificing performance.

    I've picked this very example, because you've talked about it in Unite sessions, you blogged about it and you wrote tutorials about it. Use that energy to fix the problem instead.

    Have a great day!
     
  3. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Specially when certain Unity features rely on having an hierarchy, like LODGroups. I don't get this resistance in fixing/improving core features like Transform hierarchy inneficiencies and the Resources system, pushing the onus on developers by introducing completely different systems which are more complex and more difficult to use.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    I believe @willgoldstone has mentioned that hierarchy folders are being worked on.
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,724
    I think we’re getting a bit sidetracked with the specific example Peter chose.

    I have watched / read / attended various events and docs and articles about optimization, and most of them contain tips along the lines of:

    “you know about Unity feature X? If you care about performance, don’t use it, it’s the worst thing ever, instead here is how you can work around it and make your game run 20x times faster”

    Which comes back to what Peter was trying to say (I think): instead of wasting energy telling users about workarounds (and then your users wasting energy implementing those), why don’t you just... fix your features?
     
  6. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
    Agreed, and to point out that it is indeed possible to fix, here is an asset that did just that

    https://assetstore.unity.com/packages/tools/utilities/hierarchy-folders-157716
     
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Yep, that's what I tried to express.

    It's the overall way how Unity Technologies handles inefficiencies and not really related to a specific feature.

    They should consider to change their mindset from "Every customer should workaround xyz" to "We fix xyz, so everybody can use it without regret".
     
    laurentlavigne, QFSW, manutoo and 5 others like this.
  8. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    upload_2020-6-23_9-15-24.png

    Would adding the
    MethodImplOptions.AggressiveInlining
    attribute have the same performance benefit when used for such a simple getter?
    Code (CSharp):
    1. public int Health
    2. {
    3.     [MethodImpl(MethodImplOptions.AggressiveInlining)]
    4.     get { return health; }
    5. }
     
    AskCarol likes this.
  9. Sometimes. Unfortunately the AggressiveInlining is just a hint for the JIT, not a command. So, as usual with C#, the answer is maybe, maybe not. And AFAIK with Mono it is even worse.

    I don't like this example though, it is messy and not really necessary most of the time. It is a typical micro-optimization which is great only if you're developing your hot-path but doesn't give you real benefit otherwise (minuscule, comparing how messy it is) .
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    @AskCarol, you should update the URL to https://resources.unity.com/unitenow/onlinesessions/optimization-tips-for-maximum-performance-part-1, the one you have posted just goes to the landing page for Unite Now.


    That example from the slides is broken, since the health field went from private to public.

    Assuming the example is a MonoBehaviour, the amount of serialized data is different between the editor and builds, and deserialization will error out.

    So the correct version of the code would have the #else version be:
    [NonSerialized] public int Health;
     
    Ne0mega, manutoo, hippocoder and 3 others like this.
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    I'm also fishy about the "move rigidbodies in FixedUpdate" tip from a performance standpoint. Auto Sync Physics Transforms was introduced quite a few years ago, and defaults to off, so unless you turn it on or manually call it, moving a transform with a rigidbody will not cause a physics sync anymore.
     
  12. ArturoNereuUnity

    ArturoNereuUnity

    Unity Technologies

    Joined:
    Sep 4, 2014
    Posts:
    17
    Hi Peter, thanks a lot for your comment and you are right, we need to fix many things and we are! However, some of these things are not trivial to fix. But, we do have people working on these small performance things that hopefully will make all these tips obsolete soon.

    As for the hierarchy changes, I would also say that it depends on the project. In many cases it won't make a difference but in some cases, is a nice thing to know that there is a potential performance benefit in changing the hierarchy.

    Thanks so much for your message!
     
    Peter77 likes this.
  13. ArturoNereuUnity

    ArturoNereuUnity

    Unity Technologies

    Joined:
    Sep 4, 2014
    Posts:
    17
    I agree on that, we need to fix it on our end. But today, those problems still persist and we want to make sure that we are transparent and people know that those problems can be at least worked around.

    Thanks for the message!
     
  14. ArturoNereuUnity

    ArturoNereuUnity

    Unity Technologies

    Joined:
    Sep 4, 2014
    Posts:
    17
    Totally, many of these tips are usually micro optimizations that don't make sense to implement. However, my intention was to show that in some cases that might be an issue and there's space for optimizing.

    Thanks for joining the conversation!
     
    Lurking-Ninja likes this.
  15. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    It might not sound much, but in my experience if you have 20+ of the same thing running a complex Update function little things like using fields instead of properties, and reducing indirections (aka: use locals and fields to reduce the number os dots in your functions) do help in shaving off that last millisecond or two that's keeping you from hitting your frame time targets.

    Doing it everywhere, however, is just making your code harder to read. Always base optimization decisions on profiling data.
     
    MadeFromPolygons likes this.
  16. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Firstly why are these videos unlisted on Youtube? After watching the first I went to the Unity channel and tried to find the second and couldn't.



    Regarding using hash values it should be noted that they do not guarantee against hash collisions and that while the chance of this happening in a real project is virtual zero, it does remain a possibility.

    As such I feel this should have been mentioned and should be in the docs too. Its important as it not only affects the developer if they get a stringToHash it also affects the underlying feature too - for example the Animator, even if you use unique names are converted to hashes internally so if they cause a clash then they clash and there is nothing you can do about it.

    The problem isn't that the hashing system is bad, or even that hashing itself is bad, its the lack of feedback and information that there may be a collision. Thus someone, somewhere, some time could run into this and find the wrong animation or trigger playing in an animator and waste days trying to fix an almost invisible issue. In fact I wouldn't be surprised if someone has already ran into this and accidentally fixed it by simply renaming an animation or trigger and never getting to the bottom of why it caused a problem.

    link to discussion
     
    manutoo and Socrates like this.
  17. AskCarol

    AskCarol

    Joined:
    Nov 12, 2019
    Posts:
    234
    We are releasing them as part of a long term content event for Unite Now. The second video wasn't yet available at the time of your posting. But you can now check it out here.
     
    Noisecrime likes this.
  18. ArturoNereuUnity

    ArturoNereuUnity

    Unity Technologies

    Joined:
    Sep 4, 2014
    Posts:
    17
    Thanks for the note Noisecrime. I really wasn't paying too much attention to this issue (hash collision) but yes, we should be making a note about possible downsides of using X approach.
     
    Noisecrime likes this.
  19. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    There's been arguments about the hash issue before. I can't quite remember what we found out, but I think it's something like it's more likely that your entire development studio gets taken out by lightning than you getting a hash collision in an animator, so I'm not sure how relevant it is to write about it.
     
    JoNax97 likes this.
  20. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Doesn't the animator uses the standard C# string.GetHash() function? Because if you get a hash collision there, you would also get a hash collision on dictionaries using strings as keys too, and people use those a lot.

    I did face a real case of hash collision once, but the algorithm was md5.
     
  21. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    I think Dictionary solves hash collisions, so getting collisions there doesn't break stuff, it just slows you down. No clue what algorithm is used by the animator, though.
     
  22. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Noisecrime likes this.
  23. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    I think animator/mecanim uses CRC32 hashes everywhere. Quality collision probability wise is not bad, actual hashing performance (i.e. string -> hash) is not great. But since the animation path hashes are actually stored into data files, we can't change the hashing algorithm, ever :(
     
    AcidArrow and Peter77 like this.
  24. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    I wouldn't call hash collisions in this case a downside, just something to be aware about as it might explain weird behavior when everything else in ones code looks correct.

    BTW although we've picked apart some of the tips in the video, I really like this approach of collecting them altogether in video. It could maybe have been a little bit tighter in editing, but the concepts and tips it covers is going to be useful information for developers.



    Which is totally the wrong attitude to have to a problem like this and one that eventually can and will lead to a bug for someone, somewhere. Obfuscating the issue exists just because its so unlikely to happen is terrible practice, when a simple one or two lines added to the documents could save a developer a huge amount of time and effort trying to track down an issue.

    I might even go further and like to see Unity add some form of hash checking at build time. I was thinking that it might be difficult, but since ( in case of Animator ) all triggers and animation states need to be defined during development and since all of those strings are internally converted to hashes anyway, I feel it should be easily enough to check for collisions and throw up a warning at some point.

    ooh I missed this one. I'm so bad at checking blogs these days, which is a shame as Aras's were always informative and good fun. Interesting his tests of CRC32 on 235886 words shows 5 collisions. Its still very low, that any one developer is unlikely to find a collision in their project, but I wonder across the millions of developer projects whether that chance increases.

    Though as I said, I have no problem with the hash used, I'm just wary that there was never really any discussion about potential collisions, and Unity doesn't even both to take steps to inform/warn the user in cases where it should be able to detect them.
     
    Last edited: Jul 2, 2020
    manutoo likes this.
  25. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    +1 :)
     
  26. Metron

    Metron

    Joined:
    Aug 24, 2009
    Posts:
    1,137
    Hi, don't you store any serialisation version number along your data?
     
    Neto_Kokku and AcidArrow like this.
  27. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
  28. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,325
    So if a collisions can be predicted, could you folks add an error message at compile time when it's detected that two != strings produce a == hash?
     
    AskCarol likes this.
  29. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    In the video you say "reduce hierarchy complexity" and in the "Project Organization" article you say "Use hierarchy to group objects".

    Which of the two methods should I follow?




    upload_2020-7-29_7-50-46.png
     
  30. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,526
  31. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,325
    I don't organize my scene with empty GO, everyone's gotta carry their weight at the mine!
     
  32. CPlusSharp22

    CPlusSharp22

    Joined:
    Dec 1, 2012
    Posts:
    111
    A more advanced optimization video such as one on burst or jobs would be cool!