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

Using System.Buffers.ArrayPool in Physics Queries

Discussion in 'Experimental Scripting Previews' started by james7132, Jan 21, 2018.

  1. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    166
    The .NET Corefx set supports the use of pooled arrays (https://github.com/dotnet/corefx/blob/master/src/System.Buffers/src/System/Buffers/ArrayPool.cs). Would it make sense to use this to allocate the arrays returned by queries like Physics.OverlapSphereAll or Physics.BoxcastAll? If the caller then returns the array to the pool, it would decrease the GC needed by allowing the Physics system to reuse the array.

    Alternatively, if this change could not be made, can we assure that this will be included the upcoming .NET Standard 2.0 update?
     
    SugoiDev and Can-Baycay like this.
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    I'm not aware of anyone who has tried this yet, but Unity 2018.1 beta is shipping now with .NET Standard 2.0 support, and this looks like it is present. So I think it is worth a try.
     
    SugoiDev likes this.
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    ArrayPool is designed for situations where you need a lot of arrays, usually concurrent apps. In Unity you just need a single pre allocated array to handle all physics queries of the non allocating variety. All you would do is end up allocating more arrays then you actually need using ArrayPool.