Search Unity

Conditionally include fields with IL2CPP and/or Burst?

Discussion in 'Entity Component System' started by Mortuus17, Aug 9, 2021.

  1. Mortuus17

    Mortuus17

    Joined:
    Jan 6, 2020
    Posts:
    105
    Hey guys. Does anybody know whether or not code like this...

    Code (CSharp):
    1. public struct Thing
    2. {
    3.     #if DEBUG
    4.     bool _safety;
    5.     #endif
    6.  
    7.     // actual fields...
    8. }
    ... causes any problems with IL2CPP and/or Burst?
    I'm asking because it works fine in plain old C# but I'm not sure about the compiler tool chains having any problems with it... I vaguely remember there being something.
    And more importantly, IF IT DOES WORK, isn't it kind of inefficient that all the containers in the
    Unity.Collections
    package don't conditionally include their
    AtomicSafetyHandle
    ,
    SharedStatic<T>
    ,
    m_MinIndex
    ,
    m_MaxIndex
    (like... really?) and
    DisposeSentinel
    , guarded by
    ENABLE_UNITY_COLLECTIONS_CHECKS
    ?
    Again, IF IT WORKS, they make up 40 out of 56 bytes of
    NativeArray<T>
    instances which are totally wasted in release mode. C# is already horrendous when it comes to RAM efficiency and some games really need every last bit (hello, Cities: Skylines with mods) but what's worse is that passing them by value in C# code is a really bad idea AAAAND job scheduling is much more inefficient - heck,
    DisposeSentinel
    is a reference type and probably even triggers marshalling, which cannot be optimized away because of the interop layer obfuscating its use (or lack thereof). Didn't @Joachim_Ante once say something along the lines of "the main thread is the most important resource, because it might schedule more jobs"...?

    Ok - the second question was more of a rant with a little bit of hope attached to it... But I mainly just want to know whether or not this conditional inclusion of fields causes any conflicts with IL2CPP and or Burst.

    Thank you for your time ;)
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Judging by NativeList, they are excluded via ENABLE_UNITY_COLLECTIONS_CHECKS, and as for the IL2CPP / Burst question - it works just fine. (Managed types are excluded from burst compilation via BurstDiscard)

    Missing defines on NativeArray could be a quirk of decompiler you're using.
    Since library is only compiled against the current mode (which is DEBUG enabled by default in editor), those fields are included.

    But in release mode - there won't be any extra fields.
    You can confirm that by decompiling release library from the release build that contains NativeArray type.


    Heres some cases where missing fields by defines may cause issues:
    - Serialization / Deserialization (e.g. Unity can act weirdly if some fields of serialized Objects are missing);
    - Explicit byte layout;
    - Marshalling;

    If you don't do the above, then you're probably fine with DEBUG field exclusion.
     
    Ghat-Smith and Mortuus17 like this.
  3. Mortuus17

    Mortuus17

    Joined:
    Jan 6, 2020
    Posts:
    105