GitHub user kronic added a comment to the discussion: Proposal: Pluggable 
allocator API

@CurtHagenlocher 
Thanks for sharing this prototype! The pluggable allocator design, explicit 
`AllocationLifetime` contracts, and zero-copy handover in builders are huge 
improvements.

I have a concrete real-world use case that aligns closely with this PR, and I'd 
love to share a quick example of what would make it work seamlessly: **sharing 
Arrow data across processes via Memory-Mapped Files (`MemoryMappedFile` / named 
shared memory) without IPC serialization or copying**.

### My Use Case: Cross-Process Shared Memory (MMF)

In my setup, one process executes queries against Parquet files and writes the 
resulting `RecordBatch` buffers directly into a named `MemoryMappedFile`, while 
a consumer .NET process reads them directly from that shared memory region.

To wrap a `MemoryMappedViewAccessor` pointer in .NET, I use a custom 
`MemoryManager<byte>` (which implements `IMemoryOwner<byte>`):

```csharp
public sealed unsafe class MappedMemoryOwner : MemoryManager<byte>
{
    private readonly MemoryMappedViewAccessor _accessor;
    private readonly byte* _pointer;
    private readonly int _length;

    public MappedMemoryOwner(MemoryMappedViewAccessor accessor, int length)
    {
        _accessor = accessor;
        _length = length;
        byte* ptr = null;
        _accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
        _pointer = ptr + _accessor.PointerOffset;
    }

    public override Span<byte> GetSpan() => new(_pointer, _length);

    public override MemoryHandle Pin(int elementIndex = 0) => new(_pointer + 
elementIndex);

    public override void Unpin() { }

    protected override void Dispose(bool disposing)
    {
        _accessor.SafeMemoryMappedViewHandle.ReleasePointer();
        if (disposing)
        {
            _accessor.Dispose();
        }
    }
}
```

### What I Would Love to See Supported

1. **Public way to wrap an existing `IMemoryOwner<byte>` into `ArrowBuffer`:**
   When reading from an already-populated MMF, I don't need an allocator to 
*allocate* new memory — I already have a `MappedMemoryOwner` pointing to the 
existing shared memory region. Right now, `new ArrowBuffer(IMemoryOwner<byte>)` 
is `internal`. Making a public factory or constructor (e.g., 
`ArrowBuffer.FromMemoryOwner(IMemoryOwner<byte> owner, AllocationLifetime 
lifetime = AllocationLifetime.CallerOwned)`) would let us wrap existing MMF 
regions with zero copies and proper disposal tracking when the `RecordBatch` is 
disposed.
2. **Writing directly into MMF via `MemoryAllocator` (`CallerOwned`):**
   On the producer side, passing a custom MMF-backed `MemoryAllocator` with 
`AllocationLifetime.CallerOwned` into builders (`new 
Int32Array.Builder(capacity, mmfAllocator)`) and IPC/ADBC readers so that 
buffers are allocated straight inside the named shared memory segment and 
handed over on `Build()` without intermediate copies. The deterministic 
disposal fixes in this PR (like releasing buffers on exceptions and 
reference-counting dictionary bodies) are critical for this scenario, because 
leaked MMF handles won't be reclaimed by a finalizer.

Overall, the direction of this PR (especially `CallerOwned` lifetime and 
`GetSpan`/`Advance` hand-over) solves the biggest blockers for zero-copy shared 
memory workflows in .NET. Looking forward to seeing this land!


GitHub link: 
https://github.com/apache/arrow-dotnet/discussions/509#discussioncomment-20078604

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to