Search Unity

Flash build performances

Discussion in 'Flash' started by nanaki, Jan 12, 2012.

  1. nanaki

    nanaki

    Joined:
    Jan 4, 2012
    Posts:
    2
    In the last few days I was trying to convert a simple racing game (just a tech demo actually) to flash, after many problems with C# -> as3 conversions and other small things I've managed to run it fine but with poor performance.

    After that, using the unity-flash comunication examples I developed a simple profiler (C# profiler that sends samples data every N frames to the ContentHost swf that prints it), as a result I found out that all built-in unity core functions where really fast, but every C# code heavy on Vector3/Quaternion/Matrix math was far from being fast, then I looked at the as3 converted code and I found this function to be the culprit: cil2as::Copy.

    As I understand it this function is needed to keep the C# struct behavior in as3, doing a full copy of the object when needed, I just can't understand how a function like that, even on a simple Vector3, could be sooo heavy on performances.

    The simplest way to verify this is to try these 2 loops:
    Code (csharp):
    1.  
    2.     void OnGUI()
    3.     {
    4.         float t0 = Time.realtimeSinceStartup;
    5.         for (int i = 0; i < 10000; ++i)
    6.         {
    7.             Vector3 a = Vector3.up,
    8.                     b = Vector3.right,
    9.                     c = (a + b) * Mathf.PI;
    10.         }
    11.         GUI.Label(new Rect(0, 0, 100, 32), ((Time.realtimeSinceStartup - t0) * 1000.0f).ToString());
    12.  
    13.         t0 = Time.realtimeSinceStartup;
    14.         for (int i = 0; i < 10000; ++i)
    15.         {
    16.             Vector3 a = new Vector3(0.0f, 1.0f, 0.0f),
    17.                     b = new Vector3(1.0f, 0.0f, 0.0f),
    18.                     c = (a + b) * Mathf.PI;
    19.         }
    20.         GUI.Label(new Rect(0, 32, 100, 32), ((Time.realtimeSinceStartup - t0) * 1000.0f).ToString());
    21.     }
    22.  
    They do the same thing, but when converted to as3, the first one will call cil2as::Copy on Vector3.up and Vector3.right resulting in a 8x loss of performance.
     
  2. Lucas Meijer_old

    Lucas Meijer_old

    Joined:
    Apr 5, 2008
    Posts:
    436
    The .Copy() calls are indeed to perserve "value type semantics". Right now, we are quite conservative, and call .Copy() even if we could analyze the situation and come to the conclusion that it is not needed. Over time, we'll optimize this more and more so that the generated code ends up with as few .Copy() calls as possible.

    In the meantime, research of the generated code as you've done, can lead to better understanding on how the code conversion works, and as you've seen can make you be able to write c# code that converts into faster as3 code than your original c# code.