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

Initial UnityWebRequest performance spike

Discussion in 'Editor & General Support' started by Luke404a, Feb 3, 2020.

  1. Luke404a

    Luke404a

    Joined:
    Feb 3, 2020
    Posts:
    6
    When using the UnityWebRequest functionality, my initial call to 'new' UnityWebRequest generates a performance spike usually around 30ms but can be much greater. It only occurs for the initial use, subsequent new UnityWebRequests are super fast and the data I use is identical.

    From the profiler I can see there's a Mono.JIT spike, however the issue persists when building the project using ILL2CPP.

    Can anyone shed some light on this?

    - I'm using the following constructors:

    Code (CSharp):
    1.  
    2. /// <summary>
    3.     ///   <para>Creates a UnityWebRequest with the default options and no attached DownloadHandler or UploadHandler. Default method is GET.</para>
    4.     /// </summary>
    5.     /// <param name="url">The target URL with which this UnityWebRequest will communicate. Also accessible via the url property.</param>
    6.     /// <param name="uri">The target URI to which form data will be transmitted.</param>
    7.     /// <param name="method">HTTP GET, POST, etc. methods.</param>
    8.     /// <param name="downloadHandler">Replies from the server.</param>
    9.     /// <param name="uploadHandler">Upload data to the server.</param>
    10.     public UnityWebRequest(string url, string method)
    11.  
    Code (CSharp):
    1.  
    2. /// <summary>
    3.     ///   <para>General constructor. Contents of the input argument are copied into a native buffer.</para>
    4.     /// </summary>
    5.     /// <param name="data">Raw data to transmit to the remote server.</param>
    6.     public UploadHandlerRaw(byte[] data)
    7.     {
    8.       if (data != null && data.Length == 0)
    9.         throw new ArgumentException("Cannot create a data handler without payload data");
    10.       this.m_Ptr = UploadHandlerRaw.Create(this, data);
    11.     }
    12.  
    Code (CSharp):
    1.  
    2. /// <summary>
    3.     ///   <para>Default constructor.</para>
    4.     /// </summary>
    5.     public DownloadHandlerBuffer()
    6.     {
    7.       this.InternalCreateBuffer();
    8.     }
    9.  
    - I'm profiling a development build on a high end PC

    Capture2.PNG
    Capture.PNG
     
    Last edited: Feb 3, 2020
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,653
    Which platform does this happen on?
     
  3. Luke404a

    Luke404a

    Joined:
    Feb 3, 2020
    Posts:
    6
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,653
    Have tried removing actual sends, to make sure that it is indeed constructor.
     
  5. Luke404a

    Luke404a

    Joined:
    Feb 3, 2020
    Posts:
    6
    Thanks for the suggestion. Tried this today, it still appears to be the 'new UnityWebRequest' call.

    When running with Deep Profile the 'WebRequestUtils' constructor is generating a large percentage of the spike

    upload_2020-2-6_15-12-52.png
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,653
    Try using constructor that takes url as a System.Uri object instead of string. That one should be more lightweight.
     
    Luke404a likes this.
  7. Luke404a

    Luke404a

    Joined:
    Feb 3, 2020
    Posts:
    6
    Thanks, that has reduced the performance spike.