Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

AddComponent weird performance

Discussion in 'Scripting' started by davidebarbieri, Jun 26, 2018.

  1. davidebarbieri

    davidebarbieri

    Joined:
    Feb 12, 2013
    Posts:
    21
    Hello,

    I've a weird problem. In the project I'm working on, the main performance bottleneck
    is caused at the moment by a set of 6 AddComponents (10 ms).
    This surprised me, so I created an empty scene, a dummy component and a test script to check its timing.

    The test script is pretty simple:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class AddComponentTest : MonoBehaviour
    5. {
    6.     void Start()
    7.     {
    8.         StartCoroutine(Test());
    9.     }
    10.  
    11.     IEnumerator Test()
    12.     {
    13.         do
    14.         {
    15.             GameObject go = new GameObject();
    16.  
    17.             var time = Time.realtimeSinceStartup;
    18.  
    19.             go.AddComponent<TestComp>();
    20.  
    21.             Debug.Log("Time: " + (Time.realtimeSinceStartup - time) * 1000 + " ms");
    22.        
    23.             yield return new WaitForSeconds(1);
    24.         }
    25.         while (true);
    26.     }
    27. }
    The dummy component:

    Code (CSharp):
    1.  
    2.     public class TestComp : MonoBehaviour
    3.     {
    4.     }
    5.  
    This AddComponent alone, takes 3-4 ms on my machine, which looks like to be very expensive to me.
    The crazy thing, anyway, is that if I test the same script outside the game project,
    in an empty project, the timing is below 0.1 ms.

    I tested the project on 2017.2.0, 2018.1.3, 2018.1.6: same results.
    I tried .net 3.5, .net 4.5 runtime versions.

    Anyone faced a similar problem?
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Sorry, brother, I can't duplicate your problem. I created a new project in Unity 2017.3.1f1, attached your script to an empty
    GameObject
    , and got this when I ran:

    Time: 0.1029968 ms

    I'm running Windows 10 on a Dell XPS 8700 (Intel i7 @ 3.4 GHz).
     
  3. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    Could you expand a little more on what you mean by this?
    Are you saying you run the same code in the Unity IDE in two different contexts, where one context has it running attached to an empty
    GameObject
    in an otherwise empty scene, and there are no other scenes. while the other context (the one where it runs slowly) has other scenes besides the one containing the
    GameObject
    with this code attached to it?
     
  4. davidebarbieri

    davidebarbieri

    Joined:
    Feb 12, 2013
    Posts:
    21
    In the former project (which has a lot a classes and scenes), I created a new blank scene.
    I created an empty gameobject, and placed the script component on it.
    ---> Time: 4 ms

    I created a new blank project, I created a new scene.
    I created an empty gameobject, and placed the script component on it.
    ---> Time: 0.1 ms

    So the scene is the same in both projects, but weirdly AddComponent is very slow just in the former project.
    It seems it happens just with my classes anyway, since if I AddComponent<BoxCollider>() it is 0.1 ms on both projects.
     
  5. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    That is very interesting. Obviously, you need to do some profiling on your own classes. A simple starting approach would be make sure you know which constructors/methods are called when AddComponent is called. Look at your code and see if you can spot something that runs differently in the context with other scenes and classes from how it runs in a blank project.

    If you post your class's code, I'll see if anything suggests itself to me.
     
  6. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    I would guess, there is some global static class somewhere in your folder running in your bigger project, which you might forgot?
     
  7. davidebarbieri

    davidebarbieri

    Joined:
    Feb 12, 2013
    Posts:
    21
    The added class is just a dummy:
    public class TestComp : MonoBehaviour { }
     
  8. davidebarbieri

    davidebarbieri

    Joined:
    Feb 12, 2013
    Posts:
    21
    There are actually (plugins), but why they should weight on the AddComponent? Can they register on this kind of event?
     
  9. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    664
    No telling without seeing their code. However, if your problem only exists in one context and not the other, I suggest you use the "Moose in Alaska" technique. It goes like this:

    In your context with the plug-ins and whatever else you have, take out about half of what you added to create that context. If the problem remains, take out half of what's left (if the problem goes away, replace the half you kept with the half you removed and confirm that the problem then returns). When you can create the problem with only half of your original context, repeat this procedure until you have so little more than an empty project that the problem is easy to see.

    I use this approach for debugging otherwise inscrutable problems all the time and it rarely fails me.
     
  10. davidebarbieri

    davidebarbieri

    Joined:
    Feb 12, 2013
    Posts:
    21
    ahah, ok, I'll do this way.
    thanks
     
  11. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Sort of, I'm guessing EditorApplication.update might be used somewhere.
     
  12. davidebarbieri

    davidebarbieri

    Joined:
    Feb 12, 2013
    Posts:
    21
    It does the same in the build too!
    (we filed a bug report)
     
  13. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    345
    Last edited: Jul 7, 2022