Search Unity

Feedback crazy data packing suggestion Idea

Discussion in 'Entity Component System' started by Razmot, Jun 1, 2020.

  1. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    346
    I keep creating little components like that :

    Code (CSharp):
    1.   public struct Credits : IComponentData
    2.     {
    3.         public uint Value;
    4.     }
    I use Uint(32 bits) for credits because ushort(16 bits, 65535 ) is not enough for my needs.

    But 32 bits is too much, I actually would be fine with 24 bits or maybe even less.

    so if we had something to specify that we just want 24 bits ([burstsize=24] or something),

    then the ECS/Burst framework could pack the data in chunks more optimally ?
     
  2. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    Pointing with sub byte precision is not possible on CPUs without additional special code which sacrifice perfoemance :)

    But Int24 can be achieved already like that:

    Code (CSharp):
    1.     public struct Int24
    2.     {
    3.         private Byte _val1;
    4.         private Byte _val2;
    5.         private Byte _val3;
    6.  
    7.         private Int32 Value
    8.         {
    9.             get
    10.             {
    11.                 var int32 = _val3 << 16 | _val2 << 8 | _val1;
    12.  
    13.                 return int32;
    14.             }
    15.             set
    16.             {
    17.                 _val1 = (Byte)(value >> 0);
    18.                 _val2 = (Byte)(value >> 8);
    19.                 _val3 = (Byte)(value >> 16);
    20.             }
    21.         }
    22.  
    23.         public static implicit operator Int32( Int24 val )
    24.         {
    25.             return val.Value;
    26.         }
    27.         public static implicit operator Int24( Int32 val )
    28.         {
    29.             Int24 result = default;
    30.             result.Value = val;
    31.             return result;
    32.         }
    33.     }
    34.  
    35.  
    36.     public struct Score : IComponentData
    37.     {
    38.         Int24 Value;
    39.     }
     
    Last edited: Jun 3, 2020
    Razmot, EduardoLauer and WAYNGames like this.
  3. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    346
    Thanks for the int24 - cool trick, but I'm not sure if ecs/burst will end up taking 32 bits or 24 . maybe better with structlayout explicit.

    I now think the only fool proof technique is to pack your data together yourself :
    like for example Credits 24 bits + Faction 8 bits in the same IComponentData.
     
  4. djsell

    djsell

    Joined:
    Aug 29, 2013
    Posts:
    77

    Set function is shifting the wrong direction.
     
  5. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    Thanks just fixed in original post
     
  6. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    It will use exactly 24 bits because there is no meaning of 32bits only 3 byte fileds so packing of struct is 1 byte pecision
    Same as with
    Code (CSharp):
    1. public struct Score : IComponentData
    2. {
    3.         Byte Value;
    4. }
    that will be packed byte to byte

    Actually you ca check by creating component data with Int32 and with Int24, create 100_000 entities of each and look on chunk utilization :)
     
    Razmot likes this.