Search Unity

SendMessage Overhead

Discussion in 'Scripting' started by jeffcraighead, Aug 31, 2007.

  1. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    What is the overhead of using SendMessage vs. calling a function from a script component directly?

    Jeff
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    A lot, but it won't matter for most people's code. If performance is a problem and you are using SendMessage in Update, FixedUpdate or in tight loops, see what you can do about caching references and calling methods directly.
     
  3. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    I'd probably think the overhead would be that of several(3+ ?) function calls?

    This is merely a guess though.
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I just did a quick test, and in the simplest case (one object, two scripts), SendMessage() is about 100 times slower than a direct call. It becomes even slower as more scripts and functions are added to the object, because SendMessage has to search through all of them for the correct function.
     
  5. Foxxis

    Foxxis

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    Really? Wow.
    Honestly though, this thread makes me very happy. I know it's wrong, but here's why:
    When I transferred the architecture of my game to Unity from it's humble origins, I stuck with the message system I had devised. In the back of my head I've had plans to rewrite it using SendMessage as I (wrongly) assumed that to be more efficient.

    So, now I won't have to. Another perk of sticking with my own system is that I can pass along more parameters as separate variables as well.

    Anyway, still a bit surprising that it's not more efficient. However, since it has to search perhaps it's inevitable.
     
  6. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    It'd be nice if you posted your test project somewhere. I'm really curious how you tested it reasonably accurately.

    Cheers,
    -Jon
     
  7. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Here are the two scripts. I made a new scene, attached both to the camera, and linked the reference for CallTest.otherScript. When Send Message was checked, I got times of about 1.55 seconds, as opposed to times of 0.015 when unchecked.
     

    Attached Files:

  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I think the comparison is not too good in explaining real world speed. What you are basically seeing is how good to JIT optimizer is vs a real world difference.

    Your function does pretty much nothing, so yes a direct function call that is probably being inlined thus not even being called is a lot faster than SendMessage.
    Message passing needs to find the right method in a hashtable, needs to go through each monobehaviour on the game object etc.

    Generally, if you know who should receive a function then you should definately and absolutely call the function directly. That will always be a lot faster, and everyone loves speed.

    The thing is though, there are a lot of cases where you just don't, where it makes for cleaner design to use message passing. And abstracts implementations in a nice way. The tradeoff for messages is not only speed it is convenience and nice code as well.