Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Mono Upgrade Future plans for the Mono runtime upgrade

Discussion in 'Experimental Scripting Previews' started by JoshPeterson, Apr 3, 2017.

  1. Alloc

    Alloc

    Joined:
    Jun 5, 2013
    Posts:
    241
    According to Unity Docs and some discussions I found: nope.
     
    Qbit86 likes this.
  2. Deleted User

    Deleted User

    Guest

    Please make safe! Please wake up and hit their backs! Ow! Please make carefully! Hackers are very clever because they are lucky to hack Unity's Editor.

    Because Github user Harry ( Cra0zy ) blames me about consternation of Mono mkbundle - If they use hxE, Hex Editor or Reserve engineering can unpack bundled mono applications. How do you protect Unity's Editor executable?
    I don't like they're commenced. I feel scared if hackers can read our built games.... I am very shocked because it is not safer than assemblies. I wish we have high-protected bundle executable of Unity's Editor and We wish we have built-in mkbundle-like GUI builder for Game files with embedded managed built game launcher... Thanks!

    PS: Who delete my post from Google+ - Because it is really important. I am sad you hurt me. Thanks! I don't want help you now. And Bye I don't buy Unity3D Pro
     
    Last edited by a moderator: Nov 20, 2017
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    What generational GC are you guys going with?

    In my experience the lack of fine grained tuning in most .NET GC's is the main issue. Not sure what the latest is in the .NET world here. But as of a couple years back it was still not good. With the MS approaching being that it's all automatic for the most part. Which works fine for web apps, no so much for low latency/high throughput applications.

    Java generally took the opposite approach, exposed all the knobs.

    The difference is often not minor. An average example would be say a 20ms pause twice a minute or more, compared to a 3ms-5ms pause every 3-4 minutes. That was my experience working on high throughput realtime game servers under load. If you can't control promotion rates and generation size manually, you are basically hosed.

    Unity probably has the talent to mess with the GC internals and expose that kind of thing. Or maybe there are generational GC's that expose more knobs now. I know the default GC still doesn't, but I've heard there are more options out there now.
     
  4. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I have the opposite experience. Even the crash mentioned in the docs is not reproduceable.
     
  5. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    612
    I suppose the note there means accessing a [threadstatic] field from the unity javascript thing from non-mainthread? If you've tested that or if there's no threading in unity script, I have no clue to be honest, feel free to ignore this.

    But mainly, I agree with you: I haven't found any issues using [ThreadStatic] in pure C# code. I've since removed all my uses of it though for other reasons (it doesn't work well with the threadpool but that's not a bug), and I haven't found a reduction in getthreadcontext crashes or something.
     
  6. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I tested it on a MonoBehaviour field, works as expected.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Threading;
    4. using UnityEngine;
    5.  
    6. public class Foo
    7. {
    8.     private int x;
    9.  
    10.     public Foo(int x)
    11.     {
    12.         this.x = x;
    13.     }
    14.  
    15.     public override string ToString()
    16.     {
    17.         return x.ToString();
    18.     }
    19. }
    20.  
    21. public class Test : MonoBehaviour
    22. {
    23.     [ThreadStatic] public static Foo foo;
    24.  
    25.     private IEnumerator Start()
    26.     {
    27.         foo = new Foo(0);
    28.  
    29.         var thread = new Thread(() =>
    30.         {
    31.             foo = new Foo(10);
    32.             GC.Collect();
    33.             Thread.Sleep(1000);
    34.             GC.Collect();
    35.             GC.Collect();
    36.             Debug.Log(foo);
    37.         });
    38.         thread.Start();
    39.  
    40.         yield return new WaitForSeconds(.5f);
    41.         GC.Collect();
    42.         GC.Collect();
    43.         GC.Collect();
    44.         Debug.Log(foo);
    45.     }
    46. }
    Since UnityScript is a walking dead, I tested ThreadStatic in C# only.
     
  7. Alloc

    Alloc

    Joined:
    Jun 5, 2013
    Posts:
    241
    Hm, interesting. Would wish a dev from Unity would respond on this topic though, as we can't just "hope" that it works all the time and not just in simple tests. Issues coming from using this would be a bitch to debug with users ;)
     
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Sorry, I missed this part of the thread earlier. I think that [ThreadStatic] should work in C#. If there is a case that doesn't work, please let me know.

    If it does not work in UnityScript, we likely won't fix it now though.
     
  9. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    It is too early to tell which GC we will use. We have experimented with sgen (Mono's default GC) internally. Likely that will be the first generational GC we try, as Mono supports it. The .NET GC from Microsoft is also an option, but it doesn't work with Mono now, so we would have to make it work with both Unity and Mono.

    At the moment we don't have a release timeline for a new GC anyway. We'll keep your suggestions in mind though.
     
  10. Alloc

    Alloc

    Joined:
    Jun 5, 2013
    Posts:
    241
    Hi Josh,

    Cool, thanks for the feedback. Maybe the docs should be updated accordingly? :)

    Regarding .NET 4.5: Is there a list of things that currently don't work or general reasonings it's still considered experimental? I'd love to see our game switch but of course relying on a core element like the runtime that's officially marked as experimental doesn't feel right for a public release ;)

    Regards,
    Chris
     
  11. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Is there documentation saying that [ThreadStatic] doesn't work? I'm not aware of that, but if you can point me to it, I'll have a look.

    There is not a public list. We have enough known bugs that we're not quite ready to make it non-experimental yet. We're getting close to fixing them though. We also have some internal tests that aren't working yet on all platforms. Once we get those two things in good shape, we will remove the experimental tag. We're getting close, but we're not quite there yet. We might make it by the end of the 2018.1 beta period though.
     
  12. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I've got a bunch of stuff from nuget that targets 4.6 and haven't had any issues yet. .Net Standard support will be nice though because so many packages are moving to it, I already have a couple that I'm a major release behind on due to .Net Standard dependencies.
     
  13. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    We're supporting .Net Standard 2.0 in the Unity 2018.1 release. The first beta should be out soon, so please give it a try with your .Net Standard libraries!
     
    windperson and Astarorr like this.
  14. Alloc

    Alloc

    Joined:
    Jun 5, 2013
    Posts:
    241
    Yeah, at the end of this page: https://docs.unity3d.com/Manual/Attributes.html
    Reading that again though I think "added to a Unity script." might refer to Unity's JS. In my opinion not very precise though as the language is indeed referred to as JavaScript in a lot of the docs too and even if it is talking only about the JS I think that's normally called "UnityScript"? Anyway, something like "the ThreadStatic attribute defined in the .NET library should not be used in JavaScript as it will cause a crash." might be more precise there :)

    And thank you for the info regarding the experimental state :)
     
  15. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Actually, I was wrong about [ThreadStatic]. It should not be used, even though it seems to work in some cases. Unity has an asset garbage collector (this is entirely different from the .NET garbage collector). The asset GC cleans up assets (like textures) that are no longer used. Unfortunately, the asset GC cannot properly handle the [ThreadStatic] attribute. So some managed object with [ThreadStatic] might be using an asset, but the asset GC doesn't see it as an owner, and will incorrectly clean up that asset.

    So in this case, I think the documentation is correct. Here "Unity script" means a script in any language, either C# or UnityScript/JavaScript. I was incorrect. Sorry for the confusion.
     
    MechEthan and Zuntatos like this.
  16. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Could you update the documentation to reflect that? The term "Unity script" is very unfortunate what with that almost being the name of a scripting language. Knowing that's there's a good reason for not allowing ThreadStatic and what exactly the consequences of using it anyway could be would also be nice.
     
  17. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Yes, I'll improve the documentation.
     
    MechEthan likes this.
  18. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    So if I understand this correctly, it should just not be used in conjunction with any UnityEngine.Object derriving object? Plain C# classes without any references to any assets should just work fine?
     
  19. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Yes, that is my understanding. Although I'm not completely familiar with this restriction (based on my original incorrect answer). If I get more information about this, I'll add it to the discussion.
     
    MechEthan likes this.
  20. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    @JoshPeterson
    Is this wording correct?
     
  21. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Yes, that looks good. I'm going to include a bit more detail in the documentation update I'm preparing (we're currently discussing it), but I'll include this wording as well. Thanks!
     
  22. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    The [ThreadStatic] issue repro:

    Code (CSharp):
    1. using System;
    2. using System.Threading;
    3. using System.Threading.Tasks;
    4. using UnityEngine;
    5.  
    6. public class Test : MonoBehaviour
    7. {
    8.     [ThreadStatic]
    9.     private static Texture field;
    10.  
    11.     private void Start()
    12.     {
    13.         Debug.Log($"Main thread id = {Thread.CurrentThread.ManagedThreadId}");
    14.  
    15.         var mainThreadContext = SynchronizationContext.Current;
    16.         var asset = new Texture2D(64, 64);
    17.  
    18.         Task.Run(() =>
    19.         {
    20.             field = asset;
    21.             Debug.Log($"Assigned to the field, thread id = {Thread.CurrentThread.ManagedThreadId}");
    22.  
    23.             using (var waitHandle = new ManualResetEvent(false))
    24.             {
    25.                 mainThreadContext.Send(_ =>
    26.                 {
    27.                     Resources.UnloadUnusedAssets().completed += asyncOp => VerifyAsset(asset, waitHandle);
    28.                 }, null);
    29.  
    30.                 waitHandle.WaitOne();
    31.             }
    32.             Debug.Log($"Task on thread {Thread.CurrentThread.ManagedThreadId} has just completed");
    33.         });
    34.     }
    35.  
    36.     private static void VerifyAsset(Texture2D asset, EventWaitHandle waitHandle)
    37.     {
    38.         if (asset != null)
    39.         {
    40.             Debug.Log("<color=green>Asset is still alive</color>");
    41.         }
    42.         else
    43.         {
    44.             Debug.Log("<color=red>Asset has been cleaned up</color>");
    45.         }
    46.         waitHandle.Set();
    47.     }
    48. }
     
    Last edited: Jan 3, 2018
    MechEthan likes this.
  23. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,163
    It now nearly half a year. Do we able to use C#7 anytime soon?
     
    Qbit86 likes this.
  24. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    If you're on a newer Unity, you can try the official Incremental Compiler. It brings C#7+ features to the table and might give you much shorter compile times.
     
    ThainaYu likes this.
  25. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    In Unity 2018.3 (which is not yet released), Unity uses Roslyn to compile C# code, and C#7 will be supported.
     
    479813005, Alverik, Arkade and 3 others like this.
  26. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    What's the relation between that and the incremental compiler? Is the incremental compiler simply using the features that are meant for .3 earlier`than the standard compiler, or does it bundle it's own copy of Roslyn?
     
    M_R likes this.
  27. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    The incremental compiler is simply using Roslyn earlier. In 2018.3, we're using Roslyn everywhere.
     
    dadude123, Alverik, Arkade and 4 others like this.
  28. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,163
    Glory on the great unity
     
  29. Lokisrim

    Lokisrim

    Joined:
    Jan 22, 2014
    Posts:
    1
    @JoshPeterson , do you will upgrade to mono 5.14 or mono 5.12 ? Or is another solution ?
     
  30. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    We're not tracking a specific release version of Mono. Instead, we pull from Mono's master branch, and apply a number of Unity-specific fixes. Also, we will cherry-pick back specific bug fixes from later versions of Mono that we need. You can get a rough idea of the Mono version by running mono.exe from the Editor\Data\MonoBleedingEdge\bin directory (on Windows) with the --version argument.
     
    codestage likes this.
  31. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    214
    Are you guys any closer to picking a 2018.x release for switching to the new .NET runtime by default for new projects? A colleague of mine made a good point in saying that this would be the ideal time for us to also switch over as it would demonstrate confidence of the stability of the new runtime. I'd hope it'll be 2018.2 but perhaps with the Roslyn compiler showing up for 2018.3 it'd be more likely to happen then.
     
  32. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    The new scripting runtime will not be the default for new projects in 2018.2. We're hoping to make it the default for 2018.3, but we're not quite there yet.

    Regarding stability, things are going well - we still have new bugs coming in, and we're correcting them. But most of of the niche variety that we tend to see when a big change like this begins to stabilize. We're working now on two remaining issues before we can make the new scripting runtime the default:

    1. Build size for AOT platforms. We're close to having the builds with the .NET Standard 2.0 profile for the same code on the old and new scripting runtime be the same size. We're expecting to complete this for 2018.3.
    2. Internal tooling. We've have lots of internal tooling and test infrastructure using .NET. We're migrating that to the new scripting runtime now. We need that to be complete before we can flip the switch. We're also expecting this for 2018.3.

    So, we can't make any promises yet, but the next release or two should see the new scripting runtime be the default for new projects.
     
    JJJohan and codestage like this.
  33. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    368
    Are you making any effort to move toward CLS compliance, and would there be any chance of recognizing VB scripts and using VB's Roslyn compiler? Precompiled managed DLL's already work fine...this would be convenient, though.
     
  34. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    No, we're not aiming for CLS compliance or VB script support. We're focused on C# from a scripting perspective.
     
  35. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    368
    That's unfortunate on CLS compliance. I raised this issue almost 3 years ago. It would've cost you nothing if you'd just updated code standards and let it come over time as various modules and systems were updated, replaced, refactored naturally through ongoing development.
     
  36. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    @JoshPeterson

    I would like to use this assembly
    Microsoft.CodeAnalysis.CSharp.Scripting
    . How would I go about adding that?

    The workflow normally would be to create a normal .net core 2.0 application in visual studio, open references and install the package from NuGet.

    First of all how would I even get the assemblies, as VS doesn't even put them into the output directory anymore unless you "publish" your project.

    I tried the naive way for now, created a new Unity project, changed the player settings to .net 4.x and api version to "net standard 2.0".
    Then I published my test project in visual studio, and simply moved all the assemblies into a folder in unity.

    Unity then refuses to load anything:


    This is 2018.2.0b4
    Scripting Runtime Version: .NET 4.x Equivalent
    Scripting Backend: Mono
    Api Compatibility: .NET Standard 2.0
    Allow unsafe code checked!
    Installed the incremental compiler from the package manager window.

    How can this be done?

    edit:
    When trying to run it, a new error appears:
    Code (CSharp):
    1. TypeLoadException: Could not set up parent class, due to: Could not load type of field 'Microsoft.CodeAnalysis.ParseOptions:_lazyErrors' (0) due to: Could not load file or assembly 'System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. assembly:System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a type:<unknown type> member:(null) signature:<none> assembly:*****\Assets\PublishedFromVisualStudio\Microsoft.CodeAnalysis.dll type:ParseOptions member:(null) signature:<none>
    2. SyntaxParseTest.Start () (at Assets/SyntaxParseTest.cs:15)
    Moving in the assembly it wants doesn't do anything.

    Should I prepare a repro case, and should I add the separate (<100kb) .net core project as well (the one where the assemblies are actually coming from)?

    If this has to do with code-generation at runtime and stuff like that: I would even be ok with it crashing when attempting to generate code. All I need is the SyntaxParser, SyntaxWalker and Visitor.
     
    Last edited: Jun 5, 2018
  37. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Unity does not support assemblies compiled against .NET Core. I believe that is the root of this issue. I'm not too familiar with
    Microsoft.CodeAnalysis.CSharp.Scripting
    . Is there a version built against .NET Standard 2.0? If so, that should work with Unity.

    See this documentation for details about our .NET profile support in 2018.1 and later: https://docs.unity3d.com/Manual/dotnetProfileSupport.html
     
  38. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    It's Roslyn, the c# compiler, some other team (I guess?) at unity uses it to make the "Incremental Compiler" that you guys are working on.

    https://www.nuget.org/packages/Microsoft.CodeAnalysis.CSharp.Scripting/
    https://github.com/dotnet/roslyn/wiki/NuGet-packages


    Yeah, I guess so, that's the one I was using. Any idea why that wouldn't work then?
    There is a version for .NET 4.6 available as well. So I'll try that one then and hope it works.

    edit: The 4.6 version works! The unity gods (that is you Josh, and whoever else is working on this fundamental stuff :p) be praised! This just unlocked so many things for me! :)

    But there are still some strange errors. I'm not sure if you'd like to look at them, or if they are not relevant.



    Is that something I should be concerned about? :eek:
    Maybe it will screw things up when creating a full build of the game?

    Here's a larger image that shows all the assemblies. The whole project is just this folder + one .cs file for testing directly in /Assets/

    https://i.imgur.com/buuwzrY.png

    edit2: wow, it even compiles stuff, producing working
    Action<>s
    and
    Func<>
    s. Cool stuff, I wonder if that also works when building for Mac or Android.
     
    Last edited: Jun 5, 2018
  39. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    I'm glad it worked!

    Based on the initial errors, it looks like Microsoft.CodeAnalysis.CSharp.Scripting was built against .NET Core 2.0, not .NET Standard 2.0, but I might be wrong.

    I'm not entirely sure where these errors come from, so I'm not sure about their impact. Can you submit a bug report with this project? I'd like to understand what is going on here.
     
    dadude123 likes this.
  40. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    I'll have a bug report ready for you in 5minutes or so. Will edit my post here then.
    Uploaded the whole project with the built in bug reporter, but didn't receive the usual email yet that contains the id.
     
    Last edited: Jun 7, 2018
    JoshPeterson likes this.
  41. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    Got the email now.

    Case 1045941
     
  42. unity_OlQdonHqmJheWQ

    unity_OlQdonHqmJheWQ

    Joined:
    Jun 14, 2018
    Posts:
    5
    @JoshPeterson If you guys aren't following this thread, please have a look and weigh in on it:
    https://github.com/dotnet/standard/issues/682

    It's essentially about the existential status of netstandard 2.0, and specifically, that there is no timeline or indication that Span (heavily relevant to our interests!) will ever become part of that standard (or perhaps, any standard...).

    Essentially, it appears that microsoft is planning on abandoning netstandard as a 'api surface interface' and replacing it with .net core as 'the reference implementation' as net full framework was previously.

    Obviously this is very irritating to a lot of people, but for unity in general is feel like this is particularly relevant and if you guys haven't been involved in that conversation, you really should be.
     
  43. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Yes, we're following this discussion. There is a plan in the .NET community to address issues like this. See this PR about the changes to the governance model:

    https://github.com/dotnet/standard/pull/764

    Unity is planning to have two representatives on this group to review API standardization requests. So I hope that we will be able to evolve .NET Standard and add useful things (like Span).
     
  44. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789
    @JoshPeterson Any news on those error messages? Have you been able to figure out what's going on here?
     
  45. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Yes, we've done some investigation. It looks like Mono is specifically ignoring these assemblies for some reason, although I'm not sure why yet. I'll follow up and try to determine the cause.
     
  46. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    It looks like Mono ignores some of these assemblies because the .NET Core implementations of them have some Windows-specific dependencies.

    Initially, make sure that any of these assemblies which are already in the .NET class libraries are referenced from the .NET class libraries via an mcs.rsp file instead of being copied into the project.

    Also, the error might be benign. Is it preventing you from building and running?
     
  47. strich

    strich

    Joined:
    Aug 14, 2012
    Posts:
    374
    @JoshPeterson is there an ETA for the first public beta of 2018.3 with C# 7 support? Also, what minor version are you planning to target? v7.2+ would be great, as that should be enough for the Span<> and Memory<> packages.
     
  48. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    The first public beta is scheduled for the end of July, and we're supporting C# 7.2.
     
    NYUnity, MechEthan, JesOb and 3 others like this.
  49. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,163
    Sadly at the end of august 2081.3 still not released
     
    Qbit86 likes this.
  50. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Yeah, when I wrote that post, the schedule said 2018.3 would be out in July, but things slip sometimes.