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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

MonoBehaviour optimization?

Discussion in 'Scripting' started by W-Walter, Mar 23, 2015.

  1. W-Walter

    W-Walter

    Joined:
    Dec 14, 2013
    Posts:
    14
    In the unity docs, it states that caching the (let's say) Transform component is faster than using MonoBehaviour.transform every frame. (I'm talking generically here, the same applies to MonoBehaviour.renderer etc)

    So, why doesn't MonoBehaviour cache it internally? It saves us scripters time and wouldnt add any overhead, besides we're doing it anyway.
    (Also, it would only need to find and cache a component when its requested for the first time).

    I'm assuming I'm missing something, because I think its stupid if MonoBehaviour.transform is doing a GetComponent every time its called.

    Back a bit I wrote a class I called OptimizedMonoBehaviour which did basically that for all components (renderer, transform, rigidbody etc) (it inherited from MonoBehaviour).

    So why does unity not do something within MonoBehaviour, or like I said... Am I missing something here.
    Thanks :)
     
  2. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
  3. W-Walter

    W-Walter

    Joined:
    Dec 14, 2013
    Posts:
    14
    Thanks for the reply, I see how they are trying to modularize it.. But my question wasnt really referring to Unity 5 which makes it irrelevant now. But back when they did have the quick access properties I dont understand why they didnt do caching internally.
     
  4. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    Well... In my experience as a developer, I would say that they probably scrapped the idea early on during development of Unity 4 and was working on the modularization which would appear on Unity 5. So yeah, practically nobody was actually working on the Monobehaviors for the older versions since after Unity 3.5. They probably leave it to the individual developers to customize our own wrappers and variations of the internal UnityEngine classes like have a customized Coroutine that can be intercepted.
     
  5. Mr-Mud

    Mr-Mud

    Joined:
    Mar 8, 2015
    Posts:
    37
    The size of an instance in memory is compromised of the objectheader (8 bytes) and all references it stores to other objects. Each cache will create a reference; increasing the size of an instance by 4 bytes each. This however does not apply for methods (they are stored once, elsewhere).

    Considering Unity had about 10(?) shorthands, each MonoBehaviour would use at least 40 bytes for this alone; even if you would never use these shorthands.

    So I guess it is mostly a size related issue. This article goes into more detail about object size (rather advanced).