Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Performance When Saving Prefabs

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

  1. noethis

    noethis

    Joined:
    Nov 11, 2013
    Posts:
    102
    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?
     
  2. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,554
    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:
    102
    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:
    11
    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.
     
    Cathero and Peter77 like this.
  5. sarahnorthway

    sarahnorthway

    Joined:
    Jul 16, 2015
    Posts:
    19
    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

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,554
    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.
     
  7. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    37
    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
     
  8. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    430
    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:
    37
    Update to Unity 2018.3.4f1 didn't help
     
  10. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    430
    @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:
    37
    It is already submitted, as I wrote earlier. Case 1122675
     
  12. SteenLund

    SteenLund

    Unity Technologies

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

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    37
    Update to Unity 2018.3.5f1 didn't help
     
  14. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    37
    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
     
    jrumps likes this.
  15. milchoDH

    milchoDH

    Joined:
    Jun 17, 2017
    Posts:
    3
    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
     
    jrumps likes this.
  16. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    430
    @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:
    430
    @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:
    11
    @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:
    3
    @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.
     
    jrumps likes this.
  20. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    37
    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.
     
  21. SteenLund

    SteenLund

    Unity Technologies

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

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    430
    @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:
    37
    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.
     
    jrumps likes this.
  24. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    37
    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:
    110
    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)?
     
  26. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    37
    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:
    430
    @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:
    37
    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
  30. HiddenTess

    HiddenTess

    Joined:
    Jan 7, 2016
    Posts:
    7
    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.
     
  31. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    353
    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