Search Unity

Performance When Saving Prefabs

Discussion in 'Prefabs' started by noethis, Dec 13, 2018.

  1. noethis

    noethis

    Joined:
    Nov 11, 2013
    Posts:
    129
    With the new prefab improvements saving prefabs has become a huge issue for me. It takes over 20 seconds to save a prefab that has about 250 gameobjects in it and is used a handful of times in several scenes. This obviously makes Auto-Save not remotely viable and even just occasional saving has slowed down my workflow a lot compared to pre-2018.3.

    Is the performance still a work in progress? Are there things I can do to improve it?
     
    vexe likes this.
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Prefab saving in itself should not have gotten significantly slower for an object of that size. There might be slight decrease because Prefab now have to be imported whenever they are saved, but it should not approach anything near 20 seconds for 250 GameObjects.

    What might be happening is that a lot of other things are dependent on the Prefab you're saving. If the Prefab is used as a nested Prefab in many other Prefabs, or is the base of many Prefab Variants, then all of those Prefabs that are dependent on this one need to be re-imported as well. Obviously such a setup was not possible previously, since there was no nested Prefabs or Prefab Variants.

    If you see a performance decrease even for a Prefab with nothing that depends on it, then that's a real issue and we would appreciate a bug report about it. Particularly if you can show it with a Prefab from 2018.2 or earlier and compare the time for saving it there with how long it takes in 2018.3.

    Apart from that we're focused on performance in general and future improvements to Unity's Asset Database (which is actively in development as we speak) and handling of dependencies should eventually have a positive effect for Prefabs as well, but currently we're a bit constrained by the existing technology stack in Unity.
     
    SugoiDev likes this.
  3. noethis

    noethis

    Joined:
    Nov 11, 2013
    Posts:
    129
    Thanks for the response--I'll see if I can test it in 2018.2 vs 2018.3.
     
    runevision likes this.
  4. Ghat-Smith

    Ghat-Smith

    Joined:
    Aug 16, 2016
    Posts:
    53
    Hi. I also noticed longer saving time since 2018.3. Found the origin of the problem in my case, wanted to share it here.
    Some prefabs had a script with OnValidate method. OnValidate is called when saving the scene. It could slow down the saving.

    In my case, the problematic line was setting the light (realtime) intensity (or light color depending on cases). Even if the set value was the same as the current value. Just added an if case to compare current and new value, and set only if necessary. I guess it was forcing recalculating the light. Don't know if it should be considered as a bug or not.

    Will try to reproduce the problem in an empty project and send a bug report, in case of.
     
  5. sarahnorthway

    sarahnorthway

    Joined:
    Jul 16, 2015
    Posts:
    78
    I also noticed an immediate increase in the time to save a scene in my project after upgrading to 2018.3. Even saving a blank empty unchanged scene took 3 seconds, with the cursor flashing between arrow and progress circle. Larger scenes were taking 5-6 seconds. All saving had been < 1 second before.

    Restarting Unity fixed the problem.

    I'd been working with several prefabs referenced ~50 times in one scene, including a nested one. I'm guessing Unity had them flagged as changed and was checking them over and over on every save? I haven't seen the issue since. In case other people have this issue, try restarting before you waste time debugging it.
     
  6. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    This is problematic, but an increase in time in saving an empty scene without Prefabs is unlikely to be related to Prefabs. There are plenty of other changes in 2018.3.

    If you have larger scenes that do contain Prefabs and want to check how much of the time it takes to save them might be related to Prefab functionality, you could make a copy of the scene where you unpack all Prefabs completely, so there's the same amount of objects but no longer any Prefabs, and then save that for comparison.

    If you have a reliable slowdown between 100% comparable scenes in 2018.3 versus 2018.2, feel free to file a bug report (please attach the project prior to 2018.3 upgrade so we can open it in 2018.2 too) so our QA can have a look at it and send it to a relevant team as appropriate.
     
    BrainSlugs83 likes this.
  7. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    Hi, I got exactly the same issue. Reliable slowdown between 2018.3 and 2018.2 - compared scenes with prefabs (no variants, no nesting) and the same scene with "unpack all Prefabs completely".

    Submitted a bug report with the completed project as I was unable to isolate it in a smaller project. Case 1122675
     
    BrainSlugs83 likes this.
  8. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    There was a performance regression in saving very large prefabs, this has been fixed and next patch should be better.

    Unless of course this is something completely different :)
     
  9. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    Update to Unity 2018.3.4f1 didn't help
     
    BrainSlugs83 likes this.
  10. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @abuki

    Could you please send in a bug report so I can have a look at it.
     
  11. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    It is already submitted, as I wrote earlier. Case 1122675
     
    BrainSlugs83 likes this.
  12. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Oh sorry missed that :)
     
  13. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    Update to Unity 2018.3.5f1 didn't help
     
    BrainSlugs83 likes this.
  14. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    Update to Unity 2018.3.6f1 didn't help - still very unusable. Any progress on that? I am thinking fo switching back to 2018.2
     
    BrainSlugs83 and jrumps like this.
  15. milchoDH

    milchoDH

    Joined:
    Jun 17, 2017
    Posts:
    4
    We're running Unity 2018.3.0f2 and are experiencing a similar problem - saving the project (not the scene) is quite slow ever since we upgraded to 2018.3. I believe this is due to prefabs.

    Here's how much time it took on an M.2 SSD for (what it looks like to be) 38 prefabs to save. Obviously, you should take this with a grain of salt since this is a Deep Profile, but I can assure you that it feels just as slow without the Profiler active.



    This issue is compounded by the fact that we change prefabs on runtime (a tiny change that calls SetActive on them) which causes a huge number of prefabs to become Dirty and have to resaved. Accidentally hitting Ctrl+S (you know... it's a habit that's hard to get out of) seems to be a shortcut for taking a coffee break, since Unity will be locked up for the next minute or two.

    Potentially this might be an issue only with our project since it's relatively big, but I've noticed the Prefab save progress dialogue pop up even on a smaller project (though it takes much shorter), whereas before 2018.3 I never noticed such a slowdown when saving a small project
     
    BrainSlugs83 and jrumps like this.
  16. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @milchoDH

    Yes I have closed a few bugs as `won't fix` exactly because of this. If someone changes the prefabs we have to save them when saving the project, this is no different than before. You are seeing a performance impact because now prefabs are also imported.

    My recommendation is to not touch the prefab assets unless you absolutely have to, in your case I doubt you have to touch it, but I am curious as to why you are touching them?
     
  17. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @abuki

    finally got to your project and you have exactly the issue described above. Something touches the prefab assets after the first time the scene is saved. I don't know what is touching the prefabs.
     
  18. JoeysLucky22

    JoeysLucky22

    Joined:
    May 11, 2013
    Posts:
    15
    @SteenLund

    We're also experiencing this issue in 2018.3.6f1.

    We're doing a good amount of OnValidate throughout our scripts. Saving a scene or duplicating something in the project panel lags the editor for about 20 seconds.

    Edit: Saving a scene with "Save as" saves instantaneously but I assume that's because "Save" is saving the scene and the project.

    Edit2: After some more finagling with how we were validating, saving is back to being quick. Here's a snippet of what we were doing originally:

    Code (CSharp):
    1.  
    2. [SerializeField] Collider m_Collider;
    3. protected virtual void OnValidate() {
    4.     m_Collider = GetComponent<Collider>();
    5. }
    6.  
    And here's the updated code:

    Code (CSharp):
    1.  
    2. [SerializeField] Collider m_Collider;
    3. protected virtual void OnValidate() {
    4.      var colliderComp = GetComponent<Collider>();
    5.      if (colliderComp != m_Collider) {
    6.           m_Collider = colliderComp;
    7.      }
    8. }
    9.  
     
    Last edited: Feb 21, 2019
  19. milchoDH

    milchoDH

    Joined:
    Jun 17, 2017
    Posts:
    4
    @SteenLund

    We have a Prefab pool that keeps prefab instances around rather than destroying them for garbage collection/speed reasons. Before we create an instance of a new prefab, we disable the prefab with SetActive(false), call Instantiate() and then enable back the prefab with SetActive(old_active_state). This ensures that no Awake, Start or OnEnable are called on that object, which makes it possible to call extra initialization methods before Unity calls theirs - this makes it possible we have the exact same flow no matter if the object was just created or re-enabled (we rely on determinism)

    Even though the prefab ends up being in the same state from which it originated, it's still marked as Dirty - so Unity thinks it's changed. If we can mark it as "Force Not Dirty" - basically clearing the Dirty mark, that would fix all of our problems. I couldn't find a way to do that though. Is this possible?

    In the meantime, I am trying to work on a solution without us setting the prefabs to disabled/enabled but that requires quite a substantial code refactor.
     
    BrainSlugs83 and jrumps like this.
  20. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    Well, I just got an e-mail that my issue will be fixed in 2018.3.7f1

    I am going to wait for it before digging deep into the project.
     
    BrainSlugs83 likes this.
  21. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Fingers crossed
     
  22. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @jrumps,

    No that is not what I am saying. Of course you should iterate and experiment.

    What I mean is that you should try not to modify the imported version of the prefabs through script unless you absolutely have to. E.g. avoid using AssetDatabase.LoadMainAssetAtPath and then modify the gameobject or components.
     
  23. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    Hi, the update to 2018.3.7f1 didn't help, so I am back here with questions to clarify.

    By "touching" assets you mean modifying the assets directly in the project folder? Because this is not my case. And if so - there would be no difference between prefab scene and unpacked scene, which is in my case. I have a problem with saving the scene, not project.

    Can you please be more specific on how exactly the serialization of prefabs work and how it can affect the editor performance? I am also using some OnValidate scripts etc. but disabling them won't help.

    What might be a problem in my case (but I am not sure and don't know how to confirm) is that lot of objects in the scene has some reference to some prefabs. Because in versions before 2018.3 you can do "break prefab instance", but its reference is still present, you can still select the prefab. And I was definitely making some (a lot?) objects by duplicating prefab and then modifying its instance in the scene, breaking completely from the original. Can these linkages have some effect? Can I somehow select / find all of these originaly "break prefab instance" in the scene using scripting?

    PS: Kind of strange getting e-mail from the support that in 2018.3.7f1 it should be fixed when I uploaded a fully reproducible project which obviously is not fixed.
     
    BrainSlugs83 and jrumps like this.
  24. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    I didn't want to start flame :) I love Unity, and I understand that some problems might be complex to solve. And even if I sometimes struggle with communication, my all previous bug reports were happily fixed. Just want to point out, there might be some misunderstandings.

    So I believe this issue will have a happy end too :)
     
  25. toddw

    toddw

    Joined:
    May 9, 2010
    Posts:
    130
    Just started using 2018.3.7f1 yesterday and I'm rolling back to 2018.2.21f1 today. The new prefab system has a lot of great improvements for certain scenarios, but it came at a cost of loss of functionality that I've been used to using for years.

    1. I can no-longer edit like components on multiple prefabs at once. For example, I have several enemy prefabs which represent different types of enemies. The need arose to add a new script component to every enemy prefab, yet now I had to add them one by one. This would have been tedious enough to do this to about 40 enemies prefabs in unity 2018.2, but now in 2018.3, there is several seconds of latency that occurs every time you change anything on a prefab, accompanied by a quick flash of a modal window stating something about "Loading" and the prefab name.

    2. Making changes in PlayMaker (which was upgraded to work with Unity 2018.3) is heavily affected by the flashing modal "Loading" window and latency that occurs every time PlayMaker applies changes to the prefab.

    3. There's something weird going on graphically in my game at run time every since the upgrade. The FPS seems about the same but the Game window has a slow laggy look to it which doesn't go away with restarting unity, etc... It's very hard to explain.

    I realize that this new prefab workflow was added to satisfy requests of many users. However, not everyone wanted, or needed it, especially at the cost of performance in an editor that is already painfully slow if you have a large project with a lot of scripts and assets as I do.

    Why not just add a setting in the Editor settings to run the prefab functionality in legacy mode (Unity 2018.2 and below)?
     
    BrainSlugs83, andreiagmu, Ony and 2 others like this.
  26. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    I was able to isolate the problem.
    It is caused by SVG sprites on prefabs. I submitted a new bug report to attach clean project and steps to reproduce, case id: 1134937
     
  27. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @abuki, Thanks, I already have a similar bug that is caused by SVG and have passed it to the people in charge of it.
     
  28. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    Great, thanks! Hoping that now it will be a quick fix :)
     
  29. CarlosCG

    CarlosCG

    Joined:
    Dec 13, 2016
    Posts:
    4
    Hi to everyone,

    We recently update our project from 2018.2.x to 2018.3.7f1 version. In our game we use ScriptableObjects intensively to serialize a lot of information from our tools. Since we made the update, we experiment a similar behaviour with constant delays, freezes and flashing popups saying "Hold on" or "Importing Small Assets". Trying to find a solution we test to save some assets with the Editor Profiler opened and we found that serializing anything to disk is really slow compared to 2018.2.x editor versions.

    In the next picture we're serializing an ScriptableObject with a size of 2KB (177 lines inside the final text file). As you can see the process of creation and saving assets takes 5.5 seconds... this is really weird.

    UnitySlow.png

    Maybe with the new Prefabs system were added new checks or triggers in the SaveAssets process that could be shrinking the normal performance in the saving process?

    Thanks in advance :)
     
    Last edited: Mar 14, 2019
    BrainSlugs83 and andreiagmu like this.
  30. HiddenTess

    HiddenTess

    Joined:
    Jan 7, 2016
    Posts:
    21
    Important discovery: If any of you are using prefab instances that happen to have Tilemap components on them, don't. After a lot of tinkering, I have determined that this is one of the things that is causing slowdowns when saving or making code changes.
     
    BrainSlugs83 likes this.
  31. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    This new prefabs, I literally hate them, this is the first and only thing which made me think about using another engine... give me back simple and fast prefabs!
     
    Last edited: Apr 27, 2019
    BrainSlugs83 likes this.
  32. HelixNGC7293

    HelixNGC7293

    Joined:
    Oct 26, 2016
    Posts:
    13
    Yeah I have similar issue. Every change I made in prefab mode took about 1 sec for auto save. Maybe add a toggle in preference so I can turn off the auto save function for prefab?
     
    BrainSlugs83 likes this.
  33. chickenjoris

    chickenjoris

    Joined:
    Sep 18, 2017
    Posts:
    1
    Everytime I refocus the Unity editor it freezes for 5 seconds.. when hitting play the 1st time since opening the editor everything is fine, but after the first time it shows a loading bar and goes through a big part of our prefabs saying "hold on...".

    Did you ever find a solution to this?
     
    BrainSlugs83 likes this.
  34. HelixNGC7293

    HelixNGC7293

    Joined:
    Oct 26, 2016
    Posts:
    13
    Solved. I have a really long (More than 50,000 letters) Super Text Mesh (Plugin from Asset Store) inside my prefab. After changing it to be dynamically loaded, much better.
     
  35. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    295
    Did you ever find a solution for this? We're still using 2017.4 LTS but probably need to update to 2018 LTS within a year and the new prefabs are one source of worry for me. We do something similar in that we have some initialization code called on prefab instances before activating them (causing Awake etc. to happen), would be very annoying if this workflow causes the editor to constantly resave the same assets. :(
     
    BrainSlugs83 likes this.
  36. milchoDH

    milchoDH

    Joined:
    Jun 17, 2017
    Posts:
    4
    Basically, I had to write some code around it so that we don't touch the prefab (at least not calling SetActive on it). Touching some properties or methods in the prefab game object will result in it being marked as dirty and then they have to be saved (which is the slow bit). But it depends on what you touch - if you only touch fields in your own scripts (without calling "SetDirty") then you should be fine.

    To be honest, your best bet is trying to upgrade on another branch and see what goes wrong - it's hard to predict what's going to happen with large projects.
     
  37. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    295
    We do need to deactivate the prefab, then call some initialization on the instance before Awake(), and then activate the instance and restore the prefab 'activeSelf', which is probably going to trigger this issue. Could also just store our prefabs deactivated, but that would be silly.

    And yeah, a new branch is given with larger updates such as this. :)

    EDIT: I guess there should be an Editor-API to mark stuff as not-dirty..
     
    BrainSlugs83 and andreiagmu like this.
  38. SingularitySystems

    SingularitySystems

    Joined:
    Feb 10, 2014
    Posts:
    48
    I'm having this exact issue whenever I go to update Collaborate: A workaround I found was to save the scene, and then open a blank new scene, before interacting with Collab. I'm currently in 2018.4.2f1. Is this issue resolved in a newer version?
     
    andreiagmu likes this.
  39. andreiagmu

    andreiagmu

    Joined:
    Feb 20, 2014
    Posts:
    175
  40. Politician

    Politician

    Joined:
    Apr 27, 2019
    Posts:
    8
    I've scanned all my project scripts for these instances, including asset scripts, there's none of them.
    It seems like it slows down consequentially as you add prefabs on your scene, no matter they are active or not.
    Project is on ssd, Library folder cleaned and reimported everything. At the same time saving the actual scene via Save As takes less than second, but when it comes to modifying prefabs or saving project while your big scene is loaded (in prefab view or not) it takes like ages. However, if you do that from empty or small scene it saves very fast.
    At the same time, idk if it's caused by the new prefab system, as I remember it was okay when it first time appeared, but since 2018.3.4f1 and up to 2019.2 now everything work incredibly slow.
     
    BrainSlugs83 likes this.
  41. psych77

    psych77

    Joined:
    Mar 1, 2017
    Posts:
    55
    Same here, i cannot work like this.
     
    BrainSlugs83 likes this.
  42. Miguelfanclub

    Miguelfanclub

    Joined:
    Jun 14, 2015
    Posts:
    64
  43. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    5,181
    I am working in 2019.2.5f1.

    I have a prefab with a model and one spotlight parented to it. With only one instance of the prefab in the scene, if I duplicate it there is a 3-5 second pause.

    If I unpack the prefab, then duplication is immediate.

    There is also a long pause is I undo a transformation of a prefab. For instance, if I move a prefab using transform gizmo, even only an inch, then undo, there is a 3-5 second pause before updating.
     
    Last edited: Mar 30, 2020
    BrainSlugs83, NeoUnity and reinfeldx like this.
  44. reinfeldx

    reinfeldx

    Joined:
    Nov 23, 2013
    Posts:
    164
    I'm having this exact problem. I have some models that I've "ProBuilderized" with ProBuilder. It seems like these are the prefabs that make my editor hang. It hangs when adding/duplicating said prefabs in my scene, and also makes it take a lot longer to enter and exit play mode.

    I can duplicate and use other prefabs just fine; the correlation for me seems to be those ProBuilderized prefabs. Also if they aren't prefabs everything is fine. Strange.
     
    Last edited: Apr 18, 2020
    BrainSlugs83, NeoUnity and firstuser like this.
  45. middenview

    middenview

    Joined:
    Nov 6, 2016
    Posts:
    2
    Thanks for the message about prefabs. As soon as I unpacked the prefab performance improved. The problem did not start until I started lighting the prefab. I have (essentially) is one light inside a pipe and one reflection probe. Just experimenting with lighting options now but it seems certain configurations are very slow to converge.
     
    NeoUnity and reinfeldx like this.
  46. AccentDave

    AccentDave

    Joined:
    Nov 16, 2015
    Posts:
    43
    2019.2.21f still a problem. Any little change to a prefab causing 'please wait, importing small assets' too much.

    How about an option in Preferences 'Always Update Prefabs' which works as-is now, but if turned off, will update dependent prefabs when the Prefab Editor is closed (or Save is invoked)? Can't see the rest of them when editing a prefab anyway.
     
    Last edited: Jun 27, 2020
  47. StenCG

    StenCG

    Joined:
    Mar 26, 2015
    Posts:
    66
    2019.3 too slow in big project (~4000 assets, mostly tiny assets) located on SSD, asset pipeline v2 (was converted from v1).
    For example, have in prefab of ~200 lines of YAML text, it takes up to two seconds to save the change of one line, and now I made one edit to the 100 selected prefabs (that are not placed on scene) - I’m waiting for more than two minutes, how can I speed up? Full clean library?

    Edit:
    Well yes, cleaning library helped
     

    Attached Files:

    Last edited: Jun 30, 2020
  48. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    This situation is both sad, depressing and really aggravating to me and everyone in our team. I remember the old times Unity used to be pretty good, now it literally gets worse and worse every iteration. It seems like the more you have the editor open, the more 'prefab' things you do, saving, changing, modifying, etc, the slower things become. Have to periodically restart Unity in hopes it gets better. Unproductive workflow. (2019.3.3f1). Takes like 5-10 secs to save or make changes.

    "It should not have gotten slower..." But it is....

    Just seems like systems and APIs are implemented and shipped quickly to the user without much serious testing done. I doubt anyone in Unity is actually using Unity to make games... otherwise they'd know and wouldn't be surprised on how slow and unusable at times it is... Need repro cases? Just open up Unity, and use it...

    Are you serious? What's the point of making a prefab system if you can't touch prefabs Lol Oh we made a prefab system but its so unusable and slow the fastest way to work with is... to not work with it. Got it.
     
    BrainSlugs83 likes this.
  49. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @vexe

    First of all I recommend you upgrade to the latest version of 2019.4, bugs have been fixed and performance has improved. I can't guarantee it has improved in your case but give it a try.

    Regarding not touching the prefab assets I think there has been a misunderstanding.
    Of course you should edit the prefab assets but you should do it through the Prefab Mode or from script use
    PrefabUtility.LoadPrefabContents
    .

    What is meant by not touching the prefab asset is that you should not use
    AssetDatabase.LoadAssetAtPath
    and start editing the objects you get.
     
    tonialatalo and firstuser like this.
  50. Politician

    Politician

    Joined:
    Apr 27, 2019
    Posts:
    8
    Easy Save asset manager gameobject was the reason of incredibly slow saving
     
    Last edited: Aug 2, 2020
    tonialatalo likes this.