Search Unity

Most efficient way for Buffers Management.

Discussion in 'Entity Component System' started by Opeth001, Aug 23, 2019.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Hi everyone,

    im using ENet-CSharp as a Reliable UDP networking library to send and receive Messages between Clients and Server. ( i tried with the New Unity NetCode but i think it's too early )

    in server side im using 4 Dynamic Buffers per Entity ( Connected Client ) as OutgoingReliableMessages , IncomingReliableMessages, OutgoingUnreliableMessages & IncomingUnreliableMessages.

    Code (CSharp):
    1.  
    2.  
    3. // the 1024 value will be tweaked  to the highest possible value to prevent allocating a heap memory block if the internal capacity is exhausted.
    4.  
    5. [InternalBufferCapacity(1024)]
    6. public struct OutgoingReliableMessages : IBufferElementData
    7. {
    8.    public byte Value;
    9. }
    10.  
    11. [InternalBufferCapacity(1024)]
    12. public struct IncomingReliableMessages : IBufferElementData
    13. {
    14.    public byte Value;
    15. }
    16.  


    The logic i found to structure the Messages Buffers is:

    if a Message Type Collection Contains more than 255 element ( very improbable ), this message collection will be splited to n / 255 message collections within the buffer.

    If you think this logic is bad or can be better your suggestions will be highly appreciated!



    now the messages conversion is another story ^_^ ( im new to unity's UnsafeUtility & pointers in C# ).

    the Add/Remove message from buffer operation will be executed mulitple Times per frame so,

    What is the most efficient way to convert a NativeArray<T> to NativeArray<byte> and vice versa ?


    im also using 2 other functions to convert from Structs to NativeArray<byte> .

    Code (CSharp):
    1.  public static NativeArray<byte> ConvertToNativeBytes<T>(T value, Allocator allocator) where T : unmanaged
    2.     {
    3.         var size = UnsafeUtility.SizeOf<T>();
    4.         var ret = new NativeArray<byte>(size, allocator);
    5.  
    6.         unsafe
    7.         {
    8.             UnsafeUtility.CopyStructureToPtr(ref value, ret.GetUnsafePtr());
    9.         }
    10.  
    11.         return ret;
    12.     }
    13.  
    14.     public static T ConvertToType<T>(NativeArray<byte> value) where T : unmanaged
    15.     {
    16.         unsafe
    17.         {
    18.             UnsafeUtility.CopyPtrToStructure(value.GetUnsafePtr(), out T item);
    19.             return item;
    20.         }
    21.     }
    If you think this logic is bad or can be better your suggestions will be highly appreciated!

    Thank you!!!
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    Thank you For your Help! :)

    can you please give more details about using a single T byte to define the Type + Size ?