Copilot commented on code in PR #504:
URL: https://github.com/apache/arrow-dotnet/pull/504#discussion_r4580782682
##########
src/Apache.Arrow.Flight/Internal/FlightDataStream.cs:
##########
@@ -37,8 +37,8 @@ internal class FlightDataStream : ArrowStreamWriter
private readonly IAsyncStreamWriter<Protocol.FlightData>
_clientStreamWriter;
private Protocol.FlightData _currentFlightData;
- public FlightDataStream(IAsyncStreamWriter<Protocol.FlightData>
clientStreamWriter, FlightDescriptor flightDescriptor, Schema schema)
- : base(new MemoryStream(), schema)
+ public FlightDataStream(IAsyncStreamWriter<Protocol.FlightData>
clientStreamWriter, FlightDescriptor flightDescriptor, Schema schema,
ArrowContext context)
+ : base(new MemoryStream(), schema, leaveOpen: false, options:
null, context?.Allocator, context?.CompressionCodecFactory)
Review Comment:
This constructor is `private protected` in `Apache.Arrow`, but
`FlightDataStream` is compiled in the separate `Apache.Arrow.Flight` assembly,
so this base-constructor call is inaccessible and the Flight project will not
compile. Make the allocator/codec-factory constructor at least `protected`, or
route this class through an accessible public/protected context constructor
while preserving the null-context default behavior.
##########
src/Apache.Arrow/Arrays/ArrayDataConcatenator.cs:
##########
@@ -605,16 +605,12 @@ public void Visit(RunEndEncodedType type)
}
var slicedValues = new List<ArrayData>(_arrayDataList.Count);
- ArrowBuffer.Builder<short> int16Builder = null;
- ArrowBuffer.Builder<int> int32Builder = null;
- ArrowBuffer.Builder<long> int64Builder = null;
-
- switch (runEndsTypeId)
- {
- case ArrowTypeId.Int16: int16Builder = new
ArrowBuffer.Builder<short>(); break;
- case ArrowTypeId.Int32: int32Builder = new
ArrowBuffer.Builder<int>(); break;
- case ArrowTypeId.Int64: int64Builder = new
ArrowBuffer.Builder<long>(); break;
- }
+ // Only the builder for the run-ends type is used. A builder
with no capacity
+ // allocates nothing, and the using declarations release
working memory even if
+ // concatenation throws partway through (for example on
run-end overflow).
+ using ArrowBuffer.Builder<short> int16Builder = new
ArrowBuffer.Builder<short>(0, _allocator);
+ using ArrowBuffer.Builder<int> int32Builder = new
ArrowBuffer.Builder<int>(0, _allocator);
+ using ArrowBuffer.Builder<long> int64Builder = new
ArrowBuffer.Builder<long>(0, _allocator);
Review Comment:
This allocator-aware path still leaves the other concatenation results
unsafe: the rest of `ArrayDataConcatenator` allocates from `_allocator` but
routes each owner through `BuildBuffer`, which constructs `new
ArrowBuffer(owner)` with the default `Finalized` lifetime. With
`CallerOwnedNativeAllocator` or `PooledManagedAllocator`, those buffers can
therefore be released/recycled by a finalizer while a span is still in use,
defeating the selected allocator's lifetime contract. Thread the allocator (or
its `Lifetime`) through `BuildBuffer` and all of its callers, and construct the
buffer with that lifetime.
##########
src/Apache.Arrow/Memory/BuilderMemory.cs:
##########
@@ -0,0 +1,290 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using System;
+using System.Buffers;
+
+namespace Apache.Arrow.Memory
+{
+ /// <summary>
+ /// The working memory of a buffer builder.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// Working memory grows in a <em>temporary</em> allocator and the built
buffer belongs to a
+ /// <em>long-term</em> one. When the two are the same allocator, the
working memory can be
+ /// handed to the built buffer instead of being copied; otherwise building
copies it.
+ /// </para>
+ /// <para>
+ /// A builder given no allocator uses managed memory as its temporary
allocator and
+ /// <see cref="MemoryAllocator.Default"/> as its long-term one, exactly
reproducing how
+ /// builders have always behaved: growth never touches native memory, and
building makes a
+ /// single native allocation of the exact size. Passing an allocator opts
into the hand-over.
+ /// </para>
+ /// <para>
+ /// After a hand-over the builder no longer owns writable memory. It keeps
a retained
+ /// reference to the built buffer so that its contents stay readable, and
copies them into
+ /// fresh working memory the first time the builder is written to again.
Code that builds
+ /// once and discards the builder therefore allocates once; code that
keeps appending after
+ /// a build pays one copy, which is what every build cost before.
+ /// </para>
+ /// <para>
+ /// When it can hand over, working memory is always a multiple of <see
cref="Padding"/>
+ /// bytes, so that a builder created with the right capacity never has to
grow to pad its
+ /// result.
+ /// </para>
+ /// </remarks>
+ internal sealed class BuilderMemory : IDisposable
+ {
+ internal const int Padding = 64;
+
+ /// <summary>
+ /// Hand over only when at most this fraction of the working memory
would be unused
+ /// (1/2). Doubling growth means this almost always holds; it rules
out handing a large
+ /// reservation to a small array, which would hold the slack for the
array's lifetime.
+ /// </summary>
+ private const int MaxSlackDivisor = 2;
+
+ private readonly MemoryAllocator _temporary;
+ private readonly MemoryAllocator _longTerm;
+ private readonly bool _canHandOver;
+
+ // Exactly one of these is in use: _owner/_memory while the builder
owns its working
+ // memory, _shared after that memory has been handed to a built
buffer. Both are empty
+ // after disposal.
+ private IMemoryOwner<byte> _owner;
+ private Memory<byte> _memory;
+ private ArrowBuffer _shared;
+ private bool _isShared;
+
+ // How many bytes of _shared hold data, as opposed to padding. A later
build can share
+ // the same buffer only if it would hold exactly the same bytes.
+ private int _sharedUsedBytes;
+
+ /// <param name="allocator">
+ /// The allocator for both working memory and built buffers, or
<c>null</c> for managed
+ /// working memory copied into <see cref="MemoryAllocator.Default"/>
when built.
+ /// </param>
+ /// <param name="capacityBytes">Initial capacity in bytes.</param>
+ public BuilderMemory(MemoryAllocator allocator, int capacityBytes)
+ : this(allocator ?? ManagedMemoryAllocator.Instance, allocator ??
MemoryAllocator.Default.Value, capacityBytes)
+ {
+ }
+
+ private BuilderMemory(MemoryAllocator temporary, MemoryAllocator
longTerm, int capacityBytes)
+ {
+ if (capacityBytes < 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(capacityBytes));
+ }
+
+ _temporary = temporary;
+ _longTerm = longTerm;
+ _canHandOver = ReferenceEquals(temporary, longTerm);
+ _owner = _temporary.Allocate(RoundUp(capacityBytes));
+ _memory = _owner.Memory;
+ }
+
+ /// <summary>
+ /// Whether built buffers can take over the working memory. When
false, the builder
+ /// behaves exactly as builders always have.
+ /// </summary>
+ public bool CanHandOver => _canHandOver;
+
+ /// <summary>
+ /// Size of the working memory in bytes. Reading this never copies.
+ /// </summary>
+ public int Capacity => _isShared ? _shared.Length : _memory.Length;
+
+ /// <summary>
+ /// The writable working memory. If it has been handed to a built
buffer, the contents
+ /// are first copied into memory the builder owns, so writes never
reach that buffer.
+ /// </summary>
+ public Memory<byte> Memory
+ {
+ get
+ {
+ if (_isShared)
+ {
+ Detach(_shared.Length);
+ }
+
+ return _memory;
+ }
+ }
+
+ /// <summary>
+ /// Grows the working memory to at least <paramref
name="capacityBytes"/>, preserving
+ /// its contents.
+ /// </summary>
+ /// <remarks>
+ /// The contents of the added region are unspecified: allocators are
not required to zero
+ /// memory, so callers that need zeroes must write them.
+ /// </remarks>
+ public void Grow(int capacityBytes)
+ {
+ int rounded = RoundUp(capacityBytes);
+ if (rounded <= Capacity)
+ {
+ return;
+ }
+
+ if (_isShared)
+ {
+ Detach(rounded);
+ return;
+ }
+
+ ThrowIfDisposed();
+ _owner = _temporary.Resize(_owner, rounded);
+ _memory = _owner.Memory;
+ }
+
+ /// <summary>
+ /// Zeroes the working memory, keeping its capacity. Discards rather
than copies memory
+ /// that was handed to a built buffer, so clearing a builder after a
build is cheap.
+ /// </summary>
+ public void Reset()
+ {
+ if (_isShared)
+ {
+ int capacity = _shared.Length;
+ ReleaseShared();
+ _owner = _temporary.Allocate(capacity);
+ _memory = _owner.Memory;
+ }
+
+ // Cleared explicitly in both cases, since allocators need not
zero what they return.
+ _memory.Span.Clear();
+ }
+
+ /// <summary>
+ /// Builds a buffer holding the first <paramref name="usedBytes"/>
bytes, zero-padded to
+ /// a multiple of <paramref name="byteSize"/>.
+ /// </summary>
+ /// <param name="target">
+ /// Allocator that should own the result; <c>null</c> means this
builder's long-term
+ /// allocator. The working memory is handed over only when that is
also the temporary
+ /// allocator; otherwise the data is copied.
+ /// </param>
+ public ArrowBuffer Build(int usedBytes, int byteSize, MemoryAllocator
target)
+ {
+ int padded =
checked((int)BitUtility.RoundUpToMultiplePowerOfTwo(usedBytes, byteSize));
+ MemoryAllocator destination = target ?? _longTerm;
+ bool handOver = _canHandOver && ReferenceEquals(destination,
_temporary);
+
+ if (_isShared)
+ {
+ // Built again holding exactly the same bytes: share the same
memory. A Resize can
+ // change the length without writing to the builder, so the
padded length alone
+ // is not enough; sharing after a shrink would expose the
removed values.
+ if (handOver && usedBytes == _sharedUsedBytes && padded ==
_shared.Length)
+ {
+ return _shared.Retain();
+ }
+
+ ArrowBuffer sharedCopy = Copy(_shared.Span, usedBytes, padded,
destination);
+ GC.KeepAlive(this);
+ return sharedCopy;
+ }
+
+ ThrowIfDisposed();
+
+ if (!handOver || padded == 0 || padded > _memory.Length ||
+ _memory.Length - padded > _memory.Length / MaxSlackDivisor)
+ {
+ ArrowBuffer copy = Copy(_memory.Span, usedBytes, padded,
destination);
+
+ // The span copied from refers to working memory this object
owns. With a
+ // finalized native allocator, a collection during Allocate
could otherwise free
+ // that memory before it was read (see the same issue in
ArrowStreamWriter).
+ GC.KeepAlive(this);
Review Comment:
`GC.KeepAlive(this)` is reached only after `Copy` has returned, but `Copy`
first calls `allocator.Allocate(padded)` and only then reads the source span.
For a finalized native temporary allocator, that allocation can trigger a
collection that finalizes `_owner` after `_memory.Span` was passed as a raw
span, so the subsequent `source.Slice(...).CopyTo(...)` can read freed memory.
Keep the builder/owner rooted across both the destination allocation and the
source copy (for example, pass the owning `BuilderMemory` into `Copy` and call
`GC.KeepAlive` after the copy inside that method).
##########
src/Apache.Arrow/Memory/CallerOwnedNativeAllocator.cs:
##########
@@ -0,0 +1,134 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using System;
+using System.Buffers;
+using System.Runtime.InteropServices;
+using System.Threading;
+
+namespace Apache.Arrow.Memory
+{
+ /// <summary>
+ /// Allocates aligned native memory that the caller is responsible for
releasing.
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// Unlike <see cref="NativeMemoryAllocator"/>, buffers from this
allocator have no
+ /// finalizer. Memory is released only by disposal, so it can never be
freed while a
+ /// span over it is still in use, and no GC memory pressure is reported
because the
+ /// garbage collector plays no part in reclaiming it.
+ /// </para>
+ /// <para>
+ /// The corresponding obligation is that a buffer which is never disposed
is leaked for
+ /// the lifetime of the process. Use this allocator when buffers need a
stable, aligned
+ /// address — interop and explicitly aligned paths — and disposal can be
guaranteed.
+ /// </para>
+ /// </remarks>
+ public sealed unsafe class CallerOwnedNativeAllocator : MemoryAllocator
+ {
+ public CallerOwnedNativeAllocator(int alignment = DefaultAlignment)
+ : base(alignment)
+ {
+ if (alignment <= 0 || (alignment & (alignment - 1)) != 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(alignment),
"Alignment must be a positive power of two.");
+ }
+ }
+
+ public override AllocationLifetime Lifetime =>
AllocationLifetime.CallerOwned;
+
+ protected override IMemoryOwner<byte> AllocateInternal(int length, out
int bytesAllocated)
+ {
+#if NET6_0_OR_GREATER
+ int offset = 0;
+ void* ptr = NativeMemory.AlignedAlloc((nuint)length,
(nuint)Alignment);
+ if (ptr == null)
+ {
+ throw new
OutOfMemoryException($"NativeMemory.AlignedAlloc({length}, {Alignment})
returned null.");
+ }
+#else
+ void* ptr = AlignedNative.AlignedAlloc(length, Alignment, out int
offset);
+#endif
+ new Span<byte>((byte*)ptr + offset, length).Clear();
+
+ bytesAllocated = length + Alignment;
+ return new CallerOwnedMemoryManager(ptr, offset, length);
+ }
+
+ /// <summary>
+ /// A <see cref="MemoryManager{T}"/> over native memory with no
finalizer, so that
+ /// reachability never determines when the memory is freed (CA2015).
+ /// </summary>
+ private sealed class CallerOwnedMemoryManager : MemoryManager<byte>
+ {
+ private readonly int _offset;
+ private readonly int _length;
+ private IntPtr _ptr;
+
+ public CallerOwnedMemoryManager(void* ptr, int offset, int length)
+ {
+ _ptr = (IntPtr)ptr;
+ _offset = offset;
+ _length = length;
+ }
+
+ public override Span<byte> GetSpan()
+ {
+ IntPtr ptr = Volatile.Read(ref _ptr);
+ if (ptr == IntPtr.Zero)
+ {
+ throw new
ObjectDisposedException(nameof(CallerOwnedMemoryManager));
+ }
+
+ return new Span<byte>((byte*)ptr + _offset, _length);
+ }
+
+ public override MemoryHandle Pin(int elementIndex = 0)
+ {
+ if (elementIndex < 0 || elementIndex > _length)
+ {
+ throw new
ArgumentOutOfRangeException(nameof(elementIndex));
+ }
+
+ IntPtr ptr = Volatile.Read(ref _ptr);
+ if (ptr == IntPtr.Zero)
+ {
+ throw new
ObjectDisposedException(nameof(CallerOwnedMemoryManager));
+ }
+
+ // Native memory does not move, so there is nothing to pin;
the handle
+ // holds a reference to this manager so the memory stays
reachable.
+ return new MemoryHandle((byte*)ptr + _offset + elementIndex,
default, this);
+ }
+
+ public override void Unpin()
+ {
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ IntPtr ptr = Interlocked.Exchange(ref _ptr, IntPtr.Zero);
+ if (ptr != IntPtr.Zero)
+ {
+#if NET6_0_OR_GREATER
+ NativeMemory.AlignedFree((void*)ptr);
+#else
+ AlignedNative.AlignedFree((void*)ptr);
+#endif
Review Comment:
`Pin` returns a `MemoryHandle` whose pointer is valid until `Unpin`, but
this manager does not track active pins and `Dispose(bool)` frees `_ptr`
unconditionally. Disposing the owner while a consumer still holds the returned
handle therefore leaves that pinned pointer dangling; `NativeMemoryManager`
already prevents this case by rejecting disposal while `_pinCount > 0`. Track
pin counts and defer/reject the free until all handles are unpinned.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]