You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by "ovska (via GitHub)" <gi...@apache.org> on 2023/03/19 19:40:37 UTC

[GitHub] [arrow] ovska opened a new issue, #34636: [C#] Reduce allocations when using ArrayPool

ovska opened a new issue, #34636:
URL: https://github.com/apache/arrow/issues/34636

   ### Describe the enhancement requested
   
   Quick single-use buffers are rented using [helper methods](/apache/arrow/blob/main/csharp/src/Apache.Arrow/Extensions/ArrayPoolExtensions.cs) that cause a (small) allocation on every invocation due to the delegate capturing references to the reader/writer and other related variables. This somewhat defeats the purpose of using pooled arrays, especially small ones for quick operations such as reading the message length.
   
   Here's a reference implementation for achieving roughly the same convenience without any allocations (outside the ones the pool might make):
   ```csharp
   public readonly struct ArrayLease : IDisposable
   {
       private readonly byte[] _array;
       private readonly ArrayPool<byte> _pool;
   
       public ArrayLease(byte[] array, ArrayPool<byte> pool)
       {
           _array = array;
           _pool = pool;
       }
   
       public void Dispose()
       {
           _pool.Return(_array);
       }
   }
   
   public static ArrayLease RentReturn(this ArrayPool<byte> pool, int length, out Memory<byte> buffer)
   {
       byte[] array = pool.Rent(length);
       buffer = array.AsMemory(0, length);
       return new ArrayLease(array, pool);
   }
   
   ```
   
   
   Example usage:
   ```diff
   - await Buffers.RentReturnAsync(4, async (buffer) =>
   + using (Buffers.RentReturn(4, out Memory<byte> buffer))
     {
         int footerLength;
         checked
         {
             footerLength = (int)(BaseStream.Position - offset);
         }
   
         BinaryPrimitives.WriteInt32LittleEndian(buffer.Span, footerLength);
   
         await BaseStream.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
   - }).ConfigureAwait(false);
   + }
   ```
   
   ### Component(s)
   
   C#


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [C#] Reduce allocations when using ArrayPool [arrow]

Posted by "CurtHagenlocher (via GitHub)" <gi...@apache.org>.
CurtHagenlocher closed issue #34636: [C#] Reduce allocations when using ArrayPool
URL: https://github.com/apache/arrow/issues/34636


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org