Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

(Case 1138093) Array.Sort allocates 112B each time it is called

Discussion in 'Experimental Scripting Previews' started by pavelkouril, Mar 18, 2019.

  1. pavelkouril

    pavelkouril

    Joined:
    Jul 22, 2016
    Posts:
    129
    This is happening with the new Mono runtime, both with .NET Standard and full desktop .NET, on both 2018.3 and 2019.1.

    Reported this issue because the runtime did not allocate anything when calling
    Array.Sort
    , so it technically is a regression.

    However, I have no ide how plausible it is for the error to be fixed in a timely manner - can Unity deviate from the reference mscorlib implementation and fix the issue, or would this need to be fixed in the reference mscorlib first, and then propagated into Unity later?
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    Thanks for the bug report, we will investigate this issue.
     
  3. pavelkouril

    pavelkouril

    Joined:
    Jul 22, 2016
    Posts:
    129
  4. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    276
    A bit more explanation. It seems things like `Array.Sort` and `List.Sort` overloads all end up using the same underlying sort routine. IIRC, that means they use the one with a delegate based comparison routine. If you want to avoid allocations you'll want to use that overload, as any comparison object overload will produce GC allocations from creating a delegate wrapping the comparison object.

    That means using the version taking a `Comparison<T>` delegate may avoid allocations.
     
  5. pavelkouril

    pavelkouril

    Joined:
    Jul 22, 2016
    Posts:
    129
    Yeah, that makes sense. :)

    The issue is that there is no analogical
    Comparison<T>
    overload for the
    Sort<T>(T[] array, int index, int length, IComparer<T> comparer)
    call - therefore, the only solution is custom sorting code, until the
    Comparison<T>
    overload for that call appears in mscorlib, or the current overload is fixed and the fix is propagated to Unity, since the bug report says you use the .NET Core mscorlib? (Because mono uses it, I guess?)