Search Unity

Feedback Please mark NativeArray<T> properties as readonly

Discussion in 'Scripting' started by RamType0, Apr 6, 2021.

  1. RamType0

    RamType0

    Joined:
    Sep 11, 2018
    Posts:
    67
    We need to dispose native collections(e.g. NativeArray<T>,NativeList<T>) when we finished to use them.
    And then, C# has language feature for this kind of context, "using".
    But currently, "using" doesn't work well for when we are writing into their properties..(CS1654 error appears)
    But in C#8 with Unity2020.2,we have solution for this issue.

    Code (CSharp):
    1.  struct NativeCollectionWithReadOnlyProperties :IDisposable
    2.     {
    3.         public int NonReadOnlyProperty
    4.         {
    5.             get => default;
    6.             set { }
    7.         }
    8.         public readonly int ReadOnlyProperty
    9.         {
    10.             get => default;
    11.             set { }
    12.         }
    13.         public readonly int this[int i]
    14.         {
    15.             get => default;
    16.             set { }
    17.         }
    18.         public readonly int Length
    19.         {
    20.             get => default;
    21.             set { }
    22.         }
    23.         public readonly int Capacity
    24.         {
    25.             get => default;
    26.             set { }
    27.         }
    28.         public void Dispose()
    29.         {
    30.          
    31.         }
    32.         public static void Sample()
    33.         {
    34.             using (var a = new NativeCollectionWithReadOnlyProperties())
    35.             {
    36.                 a.NonReadOnlyProperty = 0; //Error:CS1654
    37.                 a.ReadOnlyProperty = 0; //OK!
    38.                 a[0] = 0; // OK!
    39.                 a.Length = 0; // OK!
    40.                 a.Capacity = 0; // OK!
    41.             }
    42.         }
    43.     }
    By just marking their properties "readonly"!
    This will definitely improve usability of native collections.
    Please.
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    I never came across a
    readonly
    property with a setter. Do you have a reference how this is supposed to work / what behavior can be expected?
     
  3. RamType0

    RamType0

    Joined:
    Sep 11, 2018
    Posts:
    67
    https://docs.microsoft.com/dotnet/c...roposals/csharp-8.0/readonly-instance-members
    "readonly" member(property, method) ensures it never causes mutating.

    This was proposed to prevent implicit copy while accessing member of non-"readonly" struct which is passed as `in` arguments.

    `in` arguments are proposed to reducing copy to improve performance.
    But accessing non-readonly properties of `in` arguments causes implicit copy every time, it would rather make worse performance.

    So this feature also potentially improve performance of "Entities.ForEach".

    When we access dynamic buffer as read only in Entities.ForEach, we must mark it as `in` argument.
    https://docs.unity3d.com/Packages/com.unity.entities@0.17/manual/ecs_entities_foreach.html
    The document says that we should use `in` rather than passed-by-value to prevent copy, but it would rather make much copies for DynamicBuffer. This feature also solves this issue.