Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Unity Improving iteration time on C# script changes

Discussion in 'Scripting' started by xoofx, Oct 18, 2021.

  1. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    151
    Yeah, my bad. 2019 used v2, i just had it set to v1. Latest 2020 showing good compile times. Esp, when uninstalling not used packages(rider, version control etc) and upgrading the latest visual studio pack.

    so its pretty much „just“ 2021/2022 that’s behaving bad
     
  2. ChiuanWei

    ChiuanWei

    Joined:
    Jan 29, 2012
    Posts:
    118
    i have fix something like this ...

    i check all the editor window class. and add OnDisable() {} to make sure all scriptobject load to set null or use Editor.DestroyImmediate()
    after all this use the EditorUtility.UnloadUnusedAssetsImmediate() to gc.

    hope this help you.
     
  3. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    151
    Not sure if i understand this right. How is this improving the c# compile times?
     
  4. ChiuanWei

    ChiuanWei

    Joined:
    Jan 29, 2012
    Posts:
    118
    you should delete & gc all your asset in editor class when OnDisable
     
  5. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    151
    I am still confused. On disable never gets called when i compile a script

    Do you have an example script or project that shows what you are doing?
     
  6. ChiuanWei

    ChiuanWei

    Joined:
    Jan 29, 2012
    Posts:
    118




    some things like this. after i have done this modify of editor class...
     
  7. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    341
    Hi @xoofx. It has been 80 days since we last heard anything. Just wondering what the status is for this critical issue.
     
  8. paulrumyancev

    paulrumyancev

    Joined:
    May 4, 2021
    Posts:
    4
    My scenario even worse - Reload Script Assemblies pops up every time I enter Play mode in the editor.
    And it happens without even making any changes to C# scripts.

    (Project Settings > Editor > Enter Play Mode Settings > Domain Reload is Off)

    So to me it seems like the issue is not 100% related to compilation times after some of the scripts were modified. I created separate thread.

    https://forum.unity.com/threads/rel...pon-every-play-stop-in-empty-project.1221063/
     
    Last edited: Jan 6, 2022
  9. IcyPoint

    IcyPoint

    Joined:
    Feb 25, 2020
    Posts:
    3
    For me, it get worse while VS debugger attached.
     
  10. TimHeijden2

    TimHeijden2

    Joined:
    Aug 11, 2016
    Posts:
    51
    I'm also having trouble with very long domain reload times, but I feel like there is something more going on in my instance. Specifically the CreateAndSetChildDomain (unload domain) is taking a very long time for me. I've waited with digging deeper until now because I just started looking into upgrading from 2020.3 to 2021.2. In the new version, it seems like assembly reloads either occur more often, or at least the problem is much more consistent. I simply need to reimport a script in the root Assets folder, at which point I need to wait 1-2 minutes for the reload:
    I've tried turning on "EnableDomainReloadTimings" in the diagnostics preferences, but unfortunately this specific item doesn't provide any more information. I've also tried deep profiling + editor iteration profiler, which after crashing my pc or unity 3x (over 28GB mem usage o_O ) gave me almost nothing, other than noting the time was mostly part of unloading the unity domain.

    Currently trying to figure out what could be causing the domain to be so large (or difficult to unload) but not sure how to go about doing that. There is one plugin in my project that once removed seems to "resolve" the issue, but that could also just be a ghost I'm chasing. Diving deeper into that now, will update if I find anything.

    edit: I've found the root cause of my specific issue, and it was related to the plugin I mentioned containing a MonoBehaviour with a constructor & destructor, and using that monobehaviour in over 80000+ objects in scenes/prefabs, triggering code whenever unity would reload its domain even if in an empty scene. Was able to work around it so its not nearly this bad ^^
     
    Last edited: Jan 11, 2022 at 4:40 PM
  11. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    341
    I logged into FogBugz to check on the status of the asset refresh bug I submitted (1376641) with supporting screenshots and editor iteration data. The associated data shows AssetDatabase.Refresh and Application.UpdateScene occurring twice per editor iteration, which effectively doubles the time it takes before the editor is usable.

    My report was submitted over 75 days ago and there has been no response. Similarly, there have been no new updates to this forum thread in nearly three months. I'm not sure who to page at Unity.
    @xoofx or @lyndon_unity @JoshPeterson What's going on with editor iteration performance? Is there quantitative progress you can share?
     
  12. IcyPoint

    IcyPoint

    Joined:
    Feb 25, 2020
    Posts:
    3
    And there is not any unity release after 2021.2.7 released for a month.
     
  13. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    529
  14. BonneCW

    BonneCW

    Joined:
    Jan 22, 2017
    Posts:
    105
    It was holiday season, what did you expect?
     
    ontrigger, Lurking-Ninja and EagleG like this.
  15. manurocker95

    manurocker95

    Joined:
    Jun 14, 2016
    Posts:
    89
    2021.2.8f1 just came out and it doesn't solve any issue so after a month we are still in the same boat :/
     
  16. josiahms_unity

    josiahms_unity

    Joined:
    Jul 23, 2018
    Posts:
    1
    +1 to this whole thread as I've resorted to doing any sort of scripting prototyping in 2018.4.36 simply because it's quick. This is a really poor workaround as eventually I have to make edits to my projects in the 2021.2.5 build and those are taking 20 seconds or more from saving a script edit to getting into play mode due to the time it takes to reload script assemblies. It's just impossible to stay in any sort of cognitive flow with that much of a wait time.
     
    ontrigger and RobertOne like this.
  17. Cloudwalker_

    Cloudwalker_

    Joined:
    Jan 3, 2014
    Posts:
    46
    My 5900x died and took windows with it, picked up a 12900kf and samsung 980 pro. I went from 15-20 second recompile and script reload to 5 seconds max on my new machine. Fingers crossed it stays this way :D

    2021.2.6
     
  18. Maria_Angelova

    Maria_Angelova

    Unity Technologies

    Joined:
    Mar 3, 2020
    Posts:
    9
    Hello,

    This is an update about our efforts internally to improve iteration time during development.

    As we previously wrote, the regressions seen in 2020.3, 2021.2 and 2022.1 do not have a single root cause, but are a result of many c
    hanges we made which have negative impact. In this post, you can see a list of descriptions of optimizations we are working on and have completed since the last update, as well as a performance comparison of different unity versions for a basic 3D template project.

    Our iteration time task force has been busy with identifying and implementing performance improvements
    We continue the cross-team effort to improve performance monitoring and identify opportunities to optimize everything happening during Script compilation, Domain reload, Enter playmode and Asset importing.

    Here are the improvements positively impacting these areas for the upcoming public releases of 2020.3 and later versions:
    • Removed duplicate SceneUpdate call when entering playmode
    • Jobified/optimized particle system prewarm
    • TerrainTools initialization cost when entering play mode reduced
    • Optimized saving a scene with very large numbers of game objects
    • Optimized performance of FindRuntimeScript/FindEditorScript improving domain reload
    • Speed up scene backup code when entering play mode
    • Static Batching Sorting cost cut dramatically
    • Visual Studio support package version 2.0.12 reduced its initialization cost
    • Visual Scripting package version 1.7.4 (and later) eliminates its domain reload cost for users when not using Visual Scripting
    • Preventing saving Scenes when not dirty
    • Optimized package manager dependency graph generation on entering playmode
    • ADB Filehasher improvements, to ensure multithreading is well distributed, brings faster asset importing
    • SerializationFile write optimization improved asset importing performance by upwards of 25%: the larger the asset, the greater the gain
    • Importing small assets (C# Scripts, .txt files and others) is up to 65% faster since 2021.2
    • Serialization command cache improvement for domain reloads and making player builds faster
    Here are the areas where improvements are work in progress:
    • A set of low-hanging fruit optimizations across the scripting pipeline and domain reload
    • TypeCache v2
    • More parallel script compilation
    • Reduced dependencies between ADB and script compilation/domain reload
    • Prevent clearing an ADB cache which keeps track of assets to importers for 63% faster asset categorization on domain reload
    Low-hanging fruit optimizations across the scripting pipeline and domain reload code base
    As we profile Unity we find “low-hanging fruit” - small code changes which result in small but noticeable speed up. These often scale with the size of the project (e.g., the number of MonoScripts, assemblies to load, the number of times a given attribute is used, etc). Identifying, implementing and landing these simple optimizations is an ongoing process, and we aim at backporting these improvements.

    TypeCache v2
    Our existing TypeCache implementation has an unavoidable domain reload cost: the TypeCache has to be rebuilt from scratch. For an empty 3d template project it takes 0.3 seconds and the cost scales with the amount of types in all loaded assemblies.
    We have begun work on a new implementation of the TypeCache, which will allow us to reuse the cached type info and reduce this cost dramatically or completely eliminate it in some use cases (e.g., entering playmode when there are no script changes and no added or updated packages). This is a relatively large piece of work which will be implemented over the next few months in stages.

    Script compilation time
    A team at Unity is working on script compilation time improvements. The compilation pipeline will be better at parallelizing work, thus improving the total time it takes to compile the script assemblies which changed. This is work in progress, we hope to be able to give you stats of the improvement in our next update post.

    Reduced dependencies between ADB and script compilation/domain reload
    On previous version of Unity the flow has always been: Import compilation assets(.cs,.asmdef,.rsp ect.) -> Compile -> Domain reload -> Import all other assets. This behavior came with a big amount of issues, due to extracting information, needed for binding .cs and .dll files with unity objects, is done during asset importing by reading assembly metadata, and parsing C# text. This is error prone and has limitations:
    • Only able to have 1 MonoBehaviour per file
    • File and class has to have same name
    • Issues with Compilation specific code (Defines)
    • Extracting unity objects from .dll's, with references to other .dll's in the project, was depending on import order
    In 2022.2 we are changing the behavior to be: Compile -> Domain reload -> Import. Now we can use the domain to extract all the needed information. That mean we don't spend time on parsing C# or reading assembly metadata so removing dependencies between script compilation and asset importing should help speed up iteration time. In time we can support multiple MonoBehaviour in a single C# file.

    Prevent clearing an ADB cache which keeps track of assets to importers
    Our asset database team discovered another cache which can be reused instead of rebuilt resulting in 63% faster asset categorization during domain reload. Coming in 2022.2.

    3D template project in numbers
    We end this update with a comparison of the time domain reload takes under different circumstances using a 3D template project when opening it with no changes since the last Unity session. All numbers are seconds.


     
    Lurking-Ninja, Trindenberg and Baste like this.
unityunity