CurtHagenlocher commented on code in PR #504:
URL: https://github.com/apache/arrow-dotnet/pull/504#discussion_r4580802907
##########
src/Apache.Arrow/Arrays/TimestampArray.cs:
##########
@@ -63,6 +70,22 @@ public Builder(TimestampType type)
DataType = type;
}
+ /// <summary>
+ /// Construct a new instance of the <see cref="Builder"/> class
with room for
+ /// <paramref name="capacity"/> values.
+ /// </summary>
+ /// <param name="capacity">Number of values to allocate room
for.</param>
+ /// <param name="allocator">
+ /// Allocator for the builder's working memory, which usually
becomes the built array's
+ /// memory. When <c>null</c>, the builder works in managed memory
and copies
+ /// what it builds into <see cref="MemoryAllocator.Default"/>, as
builders always have.
+ /// </param>
+ public Builder(TimestampType type, int capacity, MemoryAllocator
allocator = null)
+ : base(new TimestampBuilder(type, capacity, allocator))
Review Comment:
Fixed in 35cd570: the null check now happens in the constructor initializer,
before the inner builder allocates. I checked the other typed builders and
didn't find the same pattern. `TimestampWithOffsetArray.Builder` already checks
first, and the others don't throw on a null type.
##########
src/Apache.Arrow/ArrowBuffer.BitmapBuilder.cs:
##########
@@ -26,14 +26,16 @@ public partial struct ArrowBuffer
/// The <see cref="BitmapBuilder"/> class is a complement to <see
cref="ArrowBuffer.Builder{T}"/>
/// and is designed for boolean fields, which are efficiently
bit-packed into byte-aligned memory.
/// </summary>
- public class BitmapBuilder
+ public class BitmapBuilder : IDisposable
{
private const int DefaultBitCapacity = 64;
+ private readonly BuilderMemory _buffer;
+
/// <summary>
/// Gets the number of bits that can be contained in the memory
allocated by the current instance.
/// </summary>
- public int Capacity { get; private set; }
+ public int Capacity => _buffer.Capacity * 8;
Review Comment:
Fixed in 35cd570: `Capacity` is now computed in 64 bits and capped at
`int.MaxValue`. There's no unit test, since reproducing it needs a 256 MB
allocation.
##########
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:
I don't think this one applies. `Apache.Arrow` grants `InternalsVisibleTo`
to `Apache.Arrow.Flight` (see `src/Apache.Arrow/Properties/AssemblyInfo.cs`),
and that makes `private protected` members accessible to derived types in the
friend assembly. `FlightDataStream` already relies on this: it overrides the
`private protected` `WriteMessageAsync`. The Flight projects build and their
tests pass, both locally and in CI on this branch, so I've left the constructor
as it is.
##########
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:
Good catch, fixed in 0425348. `BuildBuffer` came in with #463 after my
lifetime change, so it still used the old constructor and every concatenated
buffer got the `Finalized` lifetime. It now passes the allocator's `Lifetime`
like the other paths, and since all the concatenation methods go through it,
that covers them all. I also checked the remaining places that create a buffer
from an allocator-owned owner; the only others wrap memory the library
allocated natively itself (C Data import, `NativeBuffer`), where `Finalized` is
correct. New tests check that an abandoned concatenation isn't released by
collection for `CallerOwned` and `GarbageCollected` allocators, and still is
for `Finalized` ones.
##########
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:
I believe the current code is correct here. `GC.KeepAlive(obj)` keeps `obj`
reachable from the start of the method up to the point of the call, so calling
it after `Copy` returns roots `this` for the whole of `Copy`, including the
destination `Allocate` and the `CopyTo`. Since `this` holds `_owner`, the
source can't be finalized during the copy. This is the same pattern the
documentation for `GC.KeepAlive` describes, and the same one the writer fix
uses, so I've left it as it is.
##########
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:
Agreed, fixed in 0425348. `CallerOwnedNativeAllocator`'s memory manager now
counts pins. Disposing while pinned marks it disposed, so its span can't be
used any more, but defers freeing the memory until the last pin is released. I
chose deferring over `NativeMemoryManager`'s approach of throwing, since
throwing from `Dispose` is discouraged (CA1065) and deferral can only leak if a
pin is never released. In practice, reference counting in `ArrowBuffer` already
prevents the C Data export path from hitting this, so it's defence in depth.
Covered by `CallerOwnedMemoryIsNotFreedWhilePinned`.
--
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]