Search Unity

Unity Mono float performance issues. Easy solved Unity please read.

Discussion in 'Editor & General Support' started by Vincenzo, Feb 18, 2020.

  1. Vincenzo

    Vincenzo

    Joined:
    Feb 29, 2012
    Posts:
    146
    Dear unity team and community.

    Currently many big projects including mine are based on mono, and we are unable to switch to ECS/DOTS at this time, yet we want more performance.

    An easy source of a free lunch is that currently in mono all calculations that involve floats are actually done in doubles. this introduces a high performance cost, anything between 50 to 100% can be gained in float heavy situations by enabling mono to process floats as floats.
    Here is an example of the hell this creates in unity's own math library:
    Code (CSharp):
    1. public static float Dot(Vector3 lhs, Vector3 rhs)
    2. {return (float) ((double) lhs.x * (double) rhs.x + (double) lhs.y * (double) rhs.y + (double) lhs.z * (double) rhs.z);}
    The mono project noticed this themselves in April of 2018 and made a series of commits to resolve this situation, read more about it here:
    https://tirania.org/blog/archive/2018/Apr-11.html

    I wish to propose to merge these changes to the unity fork of mono, they will improve performance for the products of all unity clients that are currently shipping games or are planning to.

    I would like to request you to merge these changes into the next itteration of 2018.4 LTS, because that is where I and many others are shipping their games from.

    I made a pull request on the unity mono fork.
    Check it here:
    https://github.com/Unity-Technologies/mono/pull/1258


    This actually solves bugs that are reported in unity for example:
    https://issuetracker.unity3d.com/is...y-prevents-it-from-returning-a-correct-result

    I am a unity pro customer and I'm sure many other Unity customers will benefit GREATLY from these changes, Please unity consider doing something about the situation that has arisen with you leaving mono in the dust for the last 2 years.

    I wish to invite other members of this community to notify Unity of this situation.
    A programmer in the Unity team can merge all of these changes into Unity in a hour or so which would benefit their clients greatly.

    Thanks guys!
     
  2. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    Hey there,

    Thank you very much for bringing this to our attention. I'll raise this internally to see if we can get this resolved for you. I will be in touch soon!
     
    Vectorbox, Vincenzo and karl_jones like this.
  3. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    So! Good news!

    We've got a Mono upgrade in the works which includes this fix. Right now, it's planned for the second half of this year but this depends on other projects and their timings.

    Backporting seems to be tricky at this stage and it's unlikely to be done for LTS.

    Let me know if you need any further assistance :)
     
    karl_jones likes this.
  4. Vincenzo

    Vincenzo

    Joined:
    Feb 29, 2012
    Posts:
    146
    This is the problem!

    Backporting is NOT tricky, it will be literally 1 hour of work, nobody's project will break and all will keep working as always. only difirence, much faster. This has been held back for too long now, It's been 2 years since this was solved in the main mono project, and can be easy ported into the current mono build of unity, it has no conflicts with it.

    Mono upgrade which probably is .net core / .net 5.0 (I hope) will be a welcome improvement but will not solve the problems we have today.

    Something releasing second half of this year means it will land in an LTS one till one and a half year from now. which is simply put very far in the future.

    Please consider your customers that are shipping games today, not 2 years from now.
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    How do you know this? This change impacts everything, it's not isolated to one area. Changing something like this, albeit to make things faster can and likely will change the behaviour of many areas. We are changing the fundamentals of math operations, that carries a big risk. If someone was using very small values then this would likely change the behaviour. We have a responsibility to keep things stable, especially in LTS. It would be pretty reckless of us to throw a change like this out in an LTS and assume it will be fine. We need time to make sure there's not any side effects and that things are stable.
     
    Last edited: Feb 18, 2020
  6. Vincenzo

    Vincenzo

    Joined:
    Feb 29, 2012
    Posts:
    146
    I understand where you are coming from, however the next request would be to give it as an option.

    If mono dared to just merge this in on the spot, it means that they were confident in such change that could impact thousands of projects around the globe that are dependant on mono, they were feeling it was the right thing to do. they solved any edge cases and it's been solid.
    Just like you guys are fine switching to .net core which will have the same behaviour. (float == float)
    Also in il2cpp all float calculations are actually done as floats. meaning this is already happening to many projects.

    I understand the LTS argument, and as such you can consider it for 2019.3/4

    Another possibility is giving us it optional. the flag is optional in the first place anyway.
     
    Last edited: Feb 18, 2020
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    Does this affect mono only, or is it carried over to IL2CPP builds also?
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    Just mono. Il2cpp is fine.
     
    MechEthan and Peter77 like this.
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This doesn't sound like the kind of thing that should be thrown into an LTS release. 2019.3 or 2020.1 to give it a chance to shake out any issues. My opinion.
     
    Pycorax and karl_jones like this.
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Starting to ship two versions of Mono is more like a week of work, not an hour. Since this is a feature, there's no way that would be done for an LTS.

    Sure, we're gonna merge it to our "master" equivalent branch too. That doesn't automatically get into all Unity releases, just like it didn't get into older "stable" Mono releases.

    If math operation performance is really your bottleneck, I suggest looking at using Burst and/or il2cpp.
     
    Pycorax, NotaNaN, MechEthan and 2 others like this.
  11. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    Thanks for the reply!

    So there is a workaround for builds.
    The only platform where no workaround exists is the editor?

    Do you consider to fix this in a future release, such as 2021.1 for example?
     
  12. Vincenzo

    Vincenzo

    Joined:
    Feb 29, 2012
    Posts:
    146
    If you consider any merge for the current old mono used in unity you should probably consider this total package;
    https://github.com/mono/mono/commit/b7db3364a0d76bcc2f16c4ea5237216a59080432

    It's basically a backport they did in mono for one of their releases. so it pretty much contains everything.

    Ideally we get a much newer mono with all the JIT instrincts, or preferably .net core/ .net 5.

    I understand that moving to .net core is a major undertaking, I only request this simple float fix as a temporary solution because it can gain a lot of performance today, without any major rewrites anywhere. whilst we probably have to wait for 2021.1 for any .net core move.

    I kind-of understand you are affraid some projects have some random calculation where as example this happens underneath:
    Code (CSharp):
    1. 0.00000000000000523786727f * something;
    Where if this calculation is done in doubles the results would be better. but honestly you already allow this code to compile as floats in il2cpp, and once the move to the newer .net is made, it will also just be real floats. same for burst.

    Trying to push this as a temporary solution to 2019.4 would not be a bad idea.
     
  13. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Could it be added as an performance option with warning and shouldn't your tests pick up on the numeric stability of maths operations?
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Unfortunately I can't give you timelines, but it will definitely be done at some point.

    You can use Burst in the editor and that doesn't have this problem.
     
    Peter77 and karl_jones like this.
  15. MartijnGG

    MartijnGG

    Joined:
    May 3, 2018
    Posts:
    74
    Has there been any update on this issue?
     
  16. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    The mono upgrade has not landed yet and is likely to be 2021.2 or later.
     
  17. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    792
    If it takes that long, isn't it better to give up the mono upgrade and go straight to .NET 5+?
    At the time when 2021.3 is released, .NET6 will also be released. Unity is again lagging far behind.
     
  18. Vincenzo

    Vincenzo

    Joined:
    Feb 29, 2012
    Posts:
    146
    @karl_jones What version of mono is being upgraded towards?
    Is it going to be kept up-to-date with the movement within .net core/ .net 5?

    Are there again a lot of specialized hacks and workarounds to make mono work with the unity engine?
    Is there work underway to modify the unity engine to accept more modern .net (like .net core)?
    what work is being done on a modern GC, and modern JIT?
     
  19. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    We have a custom build of Mono. You can see the changes here: https://github.com/Unity-Technologies/mono
    Yes, work is ongoing towards the newer .net versions and we want to stay up to date with future versions. Im afraid I can't offer any details, at the moment. It's a lot to cover and not my area. There will be further information on this in the future though.
     
    FM-Productions likes this.
  20. Vincenzo

    Vincenzo

    Joined:
    Feb 29, 2012
    Posts:
    146
    @karl_jones This cannot be the "new" upgrade /fork of mono you are working on? I mean Right?

    8842 commits behind mono:master.
     
  21. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,300
    No those are the trunk changes, which will be around 2020.2 beta. The work has not landed in trunk yet. As I said it will be 2021.2 or later.
    Those are public branches. Development is done somewhere else and pushed when its ready to merge.
     
  22. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    karl_jones likes this.
  23. stuepfnick

    stuepfnick

    Joined:
    Apr 19, 2017
    Posts:
    18
    Oh man, that's really annoying: still no way to use doubles for the transform (mainly position vector), so a lot of work arounds are needed for big worlds, like storing everything in doubles and move the world around, and then in the background it gets converted from float to double and back to float, so with workaround from double to float to double to float to double. Yeah, makes a lot of sense … lower performance and precision. best of both worlds!
     
    iOwls likes this.