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-unsubscr...@arrow.apache.org.apache.org

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

Reply via email to