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

Bug Burst & Generics speed

Discussion in 'Burst' started by Electrospeed, Jul 27, 2023.

  1. Electrospeed

    Electrospeed

    Joined:
    Jul 16, 2016
    Posts:
    3
    Hi all, I've discovered something interesting. The basic code is here:

    Code (CSharp):
    1. public struct Util<T> where T : struct, INativeList<MyStruct>
    2. {
    3.     public static void Eval_Generic(in T list) { //evaluate }
    4.     public static void Eval_Fixed(in FixedList4096<MyStruct> list) { // evaluate }
    5. }
    What I've discovered is that the
    Eval_Generic
    runs about 5x slower, despite the function signature being the same in the Burst Inspector.

    I am wondering if this is supposed to be so, and if there's a way to get the generic version to run as fast as the native one.
     
  2. Electrospeed

    Electrospeed

    Joined:
    Jul 16, 2016
    Posts:
    3
    I have also realized this may not be a Burst thing, it could come down to the IL emitted by the compiler.
     
  3. billykater

    billykater

    Joined:
    Mar 12, 2011
    Posts:
    326
    One possible cause could be the interaction between non readonly struct and in parameters. Basically the larger the struct you are passing the slower it gets due to defensive copying that can't be optimized as much when using generics. That should be visible in the burst inspector on the caller side of things though.

    The other thing I have seen (though not in burst) is that due to the INativeList<> constraint it sometimes doesn't optimize out the boxing of the struct.
     
    Electrospeed likes this.
  4. Electrospeed

    Electrospeed

    Joined:
    Jul 16, 2016
    Posts:
    3
    I did not know about the performance penalty for
    in
    parameters. I changed them all to
    ref
    and it's fixed the problem completely. TMYK I guess