Skip to content

Introduce a reference counted layer between ArrowBuffer and memory - #297

Merged
CurtHagenlocher merged 4 commits into
apache:mainfrom
CurtHagenlocher:RefCountedBuffer
Mar 30, 2026
Merged

Introduce a reference counted layer between ArrowBuffer and memory#297
CurtHagenlocher merged 4 commits into
apache:mainfrom
CurtHagenlocher:RefCountedBuffer

Conversation

@CurtHagenlocher

Copy link
Copy Markdown
Contributor

What's Changed

Introduces a reference-counted layer between ArrowBuffer and the underlying memory to allow buffers to be shared between multiple arrays.
Supports export of managed buffers.
Disables experimental workaround that was previously added to support buffer export given that it is no longer necessary.

This is an alternative to #291 that's more flexible.

Closes#111.

…lying memory to allow buffers to be shared between multiple arrays.

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Introduces reference-counted buffer ownership to allow safe sharing of underlying memory across arrays/slices and to support exporting managed buffers via the C Data interface without invalidating the original C# objects.

Changes:

  • Add SharedMemoryOwner/SharedMemoryHandle and update ArrowBuffer to support Retain()-based sharing and export.
  • Add shared-slice APIs (SliceShared) across ArrayData, Array, ChunkedArray, Column, and RecordBatch.
  • Update C Data export behavior/documentation and expand tests to validate reference-counted lifetimes and export semantics.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 5 comments.

Show a summary per file
FileDescription
test/Apache.Arrow.Tests/CDataInterfacePythonTests.csRemoves managed-export toggle usage in Python interop tests.
test/Apache.Arrow.Tests/CDataInterfaceDataTests.csAdds a regression test ensuring export doesn’t invalidate the source array.
test/Apache.Arrow.Tests/ArrowBufferTests.csAdds tests for ArrowBuffer.Retain() and SliceShared lifetime behavior.
test/Apache.Arrow.Tests/ArrayDataReferenceCountingTests.csNew comprehensive tests around shared slicing/export and allocator tracking.
src/Apache.Arrow/RecordBatch.csAdds RecordBatch.SliceShared.
src/Apache.Arrow/Memory/SharedMemoryOwner.csNew ref-counted wrapper around IMemoryOwner<byte>.
src/Apache.Arrow/Memory/SharedMemoryHandle.csNew retainable handle implementing IMemoryOwner<byte>.
src/Apache.Arrow/Memory/NativeMemoryManager.csRemoves old IOwnableAllocation path.
src/Apache.Arrow/Memory/ExportedAllocationOwner.csUpdates export pin/ownership tracking to handle shared handles.
src/Apache.Arrow/Column.csMakes Column disposable and adds SliceShared.
src/Apache.Arrow/ChunkedArray.csMakes ChunkedArray disposable and adds SliceShared.
src/Apache.Arrow/C/CArrowArrayExporter.csUpdates docs + deprecates managed-export toggle as ignored/obsolete.
src/Apache.Arrow/ArrowBuffer.csReplaces direct owner with shared handle and adds Retain() + new export behavior.
src/Apache.Arrow/Arrays/ArrowArrayFactory.csAdds SliceShared factory method.
src/Apache.Arrow/Arrays/ArrayData.csAdds SliceShared implementation (retains buffers only).
src/Apache.Arrow/Arrays/Array.csAdds Array.SliceShared API.
Comments suppressed due to low confidence (1)

src/Apache.Arrow/Memory/SharedMemoryHandle.cs:35

  • After Dispose(), _owner is set to null, but Memory/Retain() dereference _owner without a guard. This turns post-dispose misuse into a NullReferenceException rather than an ObjectDisposedException, and it can also surface via ArrowBuffer.Memory if an ArrowBuffer is accessed after disposal. Consider adding an explicit disposed check (throwing ObjectDisposedException) in Memory and Retain().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadsrc/Apache.Arrow/Arrays/ArrayData.cs Outdated
Comment threadsrc/Apache.Arrow/Column.cs Outdated
Comment threadsrc/Apache.Arrow/ChunkedArray.cs Outdated
Comment threadtest/Apache.Arrow.Tests/ArrowBufferTests.cs Outdated
Comment threadsrc/Apache.Arrow/Memory/ExportedAllocationOwner.cs

@adamreeveadamreeve left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good to me thanks Curt, I've left a few questions.

Comment threadsrc/Apache.Arrow/ChunkedArray.cs
Comment threadsrc/Apache.Arrow/Memory/SharedMemoryOwner.cs Outdated
Comment threadsrc/Apache.Arrow/Memory/SharedMemoryHandle.cs
Comment threadsrc/Apache.Arrow/Memory/SharedMemoryHandle.cs Outdated

@adamreeveadamreeve left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow export of managed buffers via C API

3 participants

@CurtHagenlocher@adamreeve