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.

Assets [WIP] ILOptimizer (makes every Unity game faster without any changes, in seconds!)

Discussion in 'Works In Progress - Archive' started by Peter77, May 12, 2018.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    I marked the thread as [WIP], but it actually is or was more of a proof-of-concept where I would like to share the results with you.

    I recently came across the following feedback item:
    https://feedback.unity3d.com/suggestions/vector3-and-other-structs-optimization-of-operators

    It says if Unity would not use custom constructors in their code, such as the Vector3 math implementation, the program execution could be a lot faster. He made a test by implementing an own set of Vector math and improved performance a lot.

    I thought it would be interesting to find out how it affects performance, if these changes would actually exist in Unity's own dll files. In other words, like if Unity Technologies would make these changes to Unity.

    In this case, nobody would have to change any code in their project and simply every Unity game might run automagically faster. Too good to be true? Let's find out!

    I wrote a tool which I call "IL Optimizer", that replaces code in Unity's DLL. For example, ILOptimizer replaces many Vector3 method implementations to avoid calling custom constructors and even inline's some method calls as well.

    The performance improvement is actually impressive. My game runs about 4% faster. That's a lot for simply not doing any changes in user code and just replacing some trivial code in the UnityEngine.Vector3 class.

    I did another Vector3 non-sense test, where I process 128*1024 Vector3's and do some math on them. While I believe only a few project have such heavy Vector processing, it also improved performance a lot. The performance improved from 34ms down to 25ms. It runs 11ms faster, that's like 30%?!, simply by replacing a few Vector3 methods!

    Here is a video where I explain what the tool is doing.





    A message to Unity Technologies:
    You can make every project on this planet that uses Unity run significantly faster with a few trivial changes in your Vector classes. Also, why is Vector3Int using properties rather than fields for its members?
     
  2. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    336
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Very interesting approach - well presented. Nice work. :cool:

    But I believe the difference there is that you have to specifically add that into your project. What Peter has done is a sort of 'for free' performance improvement - no changes to client projects or source code.
     
    Aka_ToolBuddy and Peter77 like this.
  4. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,892
    Wow, looks super interesting! Any drawbacks you've found or is it really just free performance?
     
    Last edited: May 20, 2019
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    I can't think of any drawbacks, nor did I observe any yet, but I also didn't test much. It was pretty much just an experiment for me.

    Perhaps it's even an ignition spark for someone to come up with a great tool that optimizes IL. If you pause the video at the right moment, you can spot many TODO's what such program could do to make the optimizer much more powerful than what I did :)
     
  6. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,892
    Looks really awesome, does it work with ILCPP?
     
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    If IL2CPP processes the modified assemblies, then yes it does.

    You would need to hook in the IL optimization before the IL2CPP step. I haven't bothered doing that, because I believe (and I could be completely wrong depending on whether Unity has such hook in their build-pipeline) that shouldn't be much of a problem and it wasn't very interesting for me :)
     
    QFSW likes this.
  8. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,892
    That's what I thought, would be much better if you know, Unity actually made these changes ;)
     
  9. Aka_ToolBuddy

    Aka_ToolBuddy

    Joined:
    Feb 25, 2014
    Posts:
    480
    Last edited: Jun 9, 2020
    Peter77, LudiKha, Igualop and 2 others like this.
  10. Aka_ToolBuddy

    Aka_ToolBuddy

    Joined:
    Feb 25, 2014
    Posts:
    480
    GCatz likes this.