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
  4. Dismiss Notice

Resolved Are jobs run using IJobExtensions.Run() burst-compiled?

Discussion in 'Burst' started by rohan_zatun, Jan 8, 2022.

  1. rohan_zatun

    rohan_zatun

    Joined:
    Aug 18, 2021
    Posts:
    6
    as the topic says

    Most of the documentation only mentions that this can be used for debugging purposes but I need the burst compilation on this one job and it also needs to run synchronously on the main thread.

    Will it be burst-compiled if I run it using Run() instead of Schedule()?
     
  2. rohan_zatun

    rohan_zatun

    Joined:
    Aug 18, 2021
    Posts:
    6
    I'd also like to know if running a job this way has an overhead compared to just calling the function.

    I'm heavily relying on the manual SIMD-ification using Unity.Mathematics library and so using Burst Compiler is a must, and does give a significant improvement, but I don't know what the overhead of running it on the main thread is.
     
  3. Mortuus17

    Mortuus17

    Joined:
    Jan 6, 2020
    Posts:
    105
    Don't know - don't care. But you can easily test it by placing...
    Code (CSharp):
    1. if (Unity.Burst.CompilerServices.Constant.IsConstantExpression(1))
    2. {
    3.     throw new Exception("This was a P/Invoke of a Burst compiled job");
    4. }
    ... in your job struct's
    Execute()
    , with the
    [BurstCompile(CompileSynchronously = true)]
    attribute on the IJob struct.
     
  4. rohan_zatun

    rohan_zatun

    Joined:
    Aug 18, 2021
    Posts:
    6
    did the test

    works, Run() compiles the job with burst
     
    Baggers_ and Mortuus17 like this.
  5. Baggers_

    Baggers_

    Joined:
    Sep 10, 2017
    Posts:
    97
    A more complete, less flippant, answer:

    - As you've seen, yes, Run uses the burst compiled version if the job has the burst attribute, burst compilation is enabled, and if the compilation has finished (CompileSynchronously will naturally delay execution to compile on first use)

    - Yes there is an overhead compared to calling a regular method. There is (iirc) always a slight cost to transitioning between managed and unmanaged code, but as long as your burst code is doing something substantial, the cost is almost always worth it. I also recall Run having more of an overhead than burst function pointers, but (at the time of writing) the argument restrictions on those can be frustrating enough that Run on a job can still feel preferable.
     
    Luxxuor and sheredom like this.