Search Unity

Runtime compilation / CodeDom

Discussion in 'Scripting' started by Trindenberg, Dec 28, 2018.

  1. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    I have seen lots of people pose the question of runtime compilation to no avail. It has been exceptionally useful within the Editor, instead of hardcoding and recompiling the whole program, work with part of the script at runtime. In fact I found it ran much faster in the player than if I hardcoded (not sure why that is). Is there a solution on build? How is it possible that it runs within the Editor, but not outside the Editor, what do I need to reference or include in my build?

    When I do a CompilerResults c = provider.CompileAssemblyFromSource(options, code)

    The first exception was that it couldn't find Mono.Exe. So I added the directory from the Unity folders (tried both). After that, I catch this exception:

    System.Exception: Compiler failed to produce the assembly. Output: ''

    at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch (System.CodeDom.Compiler.CompilerParameters options, System.String[] fileNames) [0x002d1] in <3845a180c26b4889bc2d47593a665814>:0

    at Microsoft.CSharp.CSharpCodeGenerator.FromSourceBatch (System.CodeDom.Compiler.CompilerParameters options, System.String[] sources) [0x00094] in <3845a180c26b4889bc2d47593a665814>:0

    at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSourceBatch (System.CodeDom.Compiler.CompilerParameters options, System.String[] sources) [0x0000f] in <3845a180c26b4889bc2d47593a665814>:0

    at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromSource (System.CodeDom.Compiler.CompilerParameters options, System.String[] sources) [0x00006] in <3845a180c26b4889bc2d47593a665814>:0

    at UIControl.RunTestClicked () [0x000af] in <855256a5b7d843d3889f8c78c5802735>:0


    If there really is no solution, I guess I will I have to create an auto-script generator of some description. There is visual node based scripting (not sure how that translates at build?), but the idea is fragmented script writing, eg. Class1.x = Class2.y / Class3.x and quick testing for performance. Getting names from Reflection, turning the names in code, compiling the code into an Action delegate. Below I was messing around with loops, manually, in parallel using C# parallel, and then a hardcoded Unity.Job.




    But the main thing I discovered was that compiling the same code at runtime/play mode, ran much faster - which was kind of interesting. Maybe it's a different memory access to a whole assembly, which at the same time is slow going back and forth from Visual Studio, to then be recompiled (waiting for the cursor to flash).
     

    Attached Files:

  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    I think slowdown comes from the whole assembly being rebuilt, and then domain has to be reloaded, and everything else is reloaded as well. Which comes as a performance hit.

    In playmode, you're not rebuiling whole assembly, and not reloading whole domain, that's why it seems to be faster.

    Unfortunately, I cannot help you with CodeDom, as I've never used it.
     
  3. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    Here are some interesting results, compiled code within the IDE, is faster than Unity-compiled code within the Build? Wondering if anyone can tell me why that is. kind of wonder what the failed result would be, would it be the same as editor-compiled, or even faster not sure. :)

    Will add the mono compiler which works in both (although slower).

     
  4. DaseinPhaos

    DaseinPhaos

    Joined:
    Nov 20, 2017
    Posts:
    7
    There is a replacement for the Unity mono CodeDom compiler:
    https://github.com/aeroson/mcs-ICodeCompiler

    Although no longer maintained, the old build still works for me (.Net4 API on Unity2019.3), also this assembly is way smaller (2mb-) comparing to newer solutions based on roslyn (10mb+)
     
  5. unity_qB-CHOd_Gc3mJw

    unity_qB-CHOd_Gc3mJw

    Joined:
    Mar 25, 2021
    Posts:
    1
    Same exact problem, did you manage to solve it somehow?
     
  6. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    I used Roslyn since, although not fully tested at runtime.