Search Unity

Does the length of code on a prefab matter?

Discussion in 'Scripting' started by RedHotFlashman, May 28, 2020.

  1. RedHotFlashman

    RedHotFlashman

    Joined:
    Mar 16, 2018
    Posts:
    35
    This is a question about squeezing performance.
    In your code you have one function that has a LOT of lines of code. This function has to be executed on all members of a list. My question is: If you instantiate a class with lots of lines of code, does this affect the memory? Does the memory grow if you instantiate classes with long code?
    What is best:

    Case 1. On the Class (Long code exists on each member - I assume)

    class Controller
    MyObject[] list;
    void DoLoop()
    --for(i....i++)
    ----listitem.DoLongCode();

    class MyObject
    public void DoLongCode()
    --// Long Code HERE?

    Case 2. On the Controller (Long code exists only in 1 place)


    class Controller
    MyObject[] list;
    void DoLoop()
    --for(i....i++)
    ---// Long Code HERE?
    ----listitem.SetResultAfterLongCode();

    class MyObject
    public void SetResultAfterLongCode()

    PS: I could check this with the profiler and draw my own conclusions, but I want to hear from somebody who knows how C# is executed on the system.
     
    Last edited: May 29, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    This is not a concern at all. Yes your program code ends up in memory, but it is going to be such a negligible amount of memory taken up in your game compared to other things:
    • Textures
    • Sound
    • 3d Models
    • Data you are generating and storing in collections and arrays. (Maybe you're recording the positions of a bunch of objects in a list that keeps growing each frame, for example)
    These things are orders of magnitude more memory than any "long code" you could write in your lifetime. You could literally write the entire Harry Potter and Game of Thrones series novels worth of code and it would be at most one or two MB. A single texture is easily bigger than that. Don't worry about how many lines of code you're writing.
     
    RedHotFlashman likes this.
  3. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    To be a bit more detailed:
    the things of your class instances which affects memory are the member-variables. The actual logic is stored in the class only. In other words: the methods are not duplicated in memory for every instance of the class.

    However, when calling a method it will also have a little footprint on the memory because of the local variables used.

    In your case it doesn't matter where you put the code from a memory perspective.
    However, probably it is better to have it in the class because it probably belongs to it somehow.

    In general: try to not create long (spaghetti) code.
     
    RedHotFlashman likes this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Oh yeah, @Hosnkobf is correct, it doesn't matter where you put the code - there will only be one copy in memory.
     
    RedHotFlashman likes this.
  5. RedHotFlashman

    RedHotFlashman

    Joined:
    Mar 16, 2018
    Posts:
    35
    Thx for the quick ANSWERS!
    @Hosnkobf : the methods are not duplicated in memory for every instance of the class.
    @PraetorBlue : it doesn't matter where you put the code - there will only be one copy in memory.