Copilot commented on code in PR #504:
URL: https://github.com/apache/arrow-dotnet/pull/504#discussion_r4581298698


##########
src/Apache.Arrow/Arrays/BinaryArray.cs:
##########
@@ -63,11 +74,16 @@ public abstract class BuilderBase<TArray, TBuilder> : 
IArrowArrayBuilder<byte, T
             private int _availableValueBufferByteCount;
 
             protected BuilderBase(IArrowType dataType)
+                : this(dataType, null)
+            {
+            }
+
+            protected BuilderBase(IArrowType dataType, MemoryAllocator 
allocator)
             {
                 DataType = dataType;
-                ValueOffsets = new ArrowBuffer.Builder<int>();
-                ValueBuffer = new ArrowBuffer.Builder<byte>();
-                ValidityBuffer = new ArrowBuffer.BitmapBuilder();
+                ValueOffsets = new ArrowBuffer.Builder<int>(0, allocator);
+                ValueBuffer = new ArrowBuffer.Builder<byte>(0, allocator);
+                ValidityBuffer = new ArrowBuffer.BitmapBuilder(0, allocator);

Review Comment:
   This allocator-aware binary builder still builds its three buffers without 
exception-safe cleanup. If `ValueOffsets.Build` or `ValueBuffer.Build` fails 
after the validity buffer (or an earlier buffer) was created, a caller-owned 
allocator leaks those buffers because the builder's `Dispose` only releases 
working memory. Wrap the build sequence and dispose intermediate buffers until 
ownership is transferred to the returned array.



##########
src/Apache.Arrow/Arrays/FixedSizeBinaryArray.cs:
##########
@@ -98,11 +98,39 @@ public abstract class BuilderBase<TArray, TBuilder> : 
IArrowArrayBuilder<byte[],
             protected abstract TArray Build(ArrayData data);
 
             protected BuilderBase(IArrowType dataType, int byteWidth)
+                : this(dataType, byteWidth, null)
+            {
+            }
+
+            protected BuilderBase(IArrowType dataType, int byteWidth, 
MemoryAllocator allocator)
             {
                 DataType = dataType;
                 ByteWidth = byteWidth;
-                ValueBuffer = new ArrowBuffer.Builder<byte>();
-                ValidityBuffer = new ArrowBuffer.BitmapBuilder();
+                ValueBuffer = new ArrowBuffer.Builder<byte>(0, allocator);
+                ValidityBuffer = new ArrowBuffer.BitmapBuilder(0, allocator);

Review Comment:
   The newly allocator-backed fixed-size-binary builder can leak its validity 
buffer when `ValueBuffer.Build` or the final array construction fails after 
validity has been built. This is permanent with a caller-owned allocator, since 
`Dispose` only releases the builder's working buffers. Dispose intermediate 
buffers on every failure before returning the completed array.



##########
src/Apache.Arrow/Arrays/TimestampWithOffsetArray.cs:
##########
@@ -213,11 +213,25 @@ public Builder(TimeUnit unit = TimeUnit.Microsecond)
             }
 
             public Builder(TimestampWithOffsetType type)
+                : this(type, null)
+            {
+            }
+
+            /// <summary>
+            /// Creates a builder whose working memory comes from <paramref 
name="allocator"/>.
+            /// </summary>
+            /// <param name="type">The timestamp-with-offset type to 
build.</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(TimestampWithOffsetType type, MemoryAllocator 
allocator)
             {
                 _type = type ?? throw new ArgumentNullException(nameof(type));
-                _timestampBuilder = new TimestampArray.Builder(type.Unit, 
"UTC");
-                _offsetBuilder = new Int16Array.Builder();
-                _validityBuilder = new ArrowBuffer.BitmapBuilder();
+                _timestampBuilder = new TimestampArray.Builder(new 
TimestampType(type.Unit, "UTC"), 0, allocator);
+                _offsetBuilder = new Int16Array.Builder(0, allocator);
+                _validityBuilder = new ArrowBuffer.BitmapBuilder(0, allocator);

Review Comment:
   This new allocator-backed builder also builds two child arrays before its 
validity buffer and final `StructArray`. Any later failure leaves the earlier 
caller-owned arrays unreleased, so a failed build leaks native memory. Add 
rollback for the partially built children/buffer and transfer ownership only on 
successful construction.



##########
src/Apache.Arrow/Arrays/VariantArray.cs:
##########
@@ -390,6 +390,26 @@ public class Builder
             private static readonly Lazy<(byte[] Metadata, byte[] Value)> 
NullPlaceholder =
                 new Lazy<(byte[], byte[])>(() => new 
VariantBuilder().Encode(VariantValue.Null));
 
+            public Builder()
+                : this(null)
+            {
+            }
+
+            /// <summary>
+            /// Creates a builder whose working memory comes from <paramref 
name="allocator"/>.
+            /// </summary>
+            /// <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(MemoryAllocator allocator)
+            {
+                _metadataBuilder = new BinaryArray.Builder(BinaryType.Default, 
allocator);
+                _valueBuilder = new BinaryArray.Builder(BinaryType.Default, 
allocator);
+                _validityBuilder = new ArrowBuffer.BitmapBuilder(0, allocator);

Review Comment:
   The allocator-backed child builders are now disposable, but this `Build` 
sequence still has no rollback. If the value array or validity bitmap fails to 
build after metadata has succeeded, the already-built arrays/buffers are 
leaked; with a caller-owned allocator this is permanent. Keep partial results 
in a cleanup scope and transfer them only after the `StructArray` is 
constructed.



##########
src/Apache.Arrow/Arrays/LargeBinaryArray.cs:
##########
@@ -48,18 +59,47 @@ public abstract class BuilderBase<TArray, TBuilder> : 
IArrowArrayBuilder<byte, T
         protected int NullCount => this.ValidityBuffer.UnsetBitCount;
 
         protected BuilderBase(IArrowType dataType)
+            : this(dataType, null)
+        {
+        }
+
+        protected BuilderBase(IArrowType dataType, MemoryAllocator allocator)
         {
             DataType = dataType;
-            ValueOffsets = new ArrowBuffer.Builder<long>();
-            ValueBuffer = new ArrowBuffer.Builder<byte>();
-            ValidityBuffer = new ArrowBuffer.BitmapBuilder();
+            ValueOffsets = new ArrowBuffer.Builder<long>(0, allocator);
+            ValueBuffer = new ArrowBuffer.Builder<byte>(0, allocator);
+            ValidityBuffer = new ArrowBuffer.BitmapBuilder(0, allocator);

Review Comment:
   This new allocator-backed base has the same failure path: `Build` creates 
several caller-owned buffers in sequence, but if a later allocation or 
`Build(data)` throws, earlier buffers are never disposed. That turns ordinary 
build failures into permanent native-memory leaks for 
`CallerOwnedNativeAllocator`; clean up each buffer until the final array owns 
them.



##########
src/Apache.Arrow/Arrays/BooleanArray.cs:
##########
@@ -33,9 +33,22 @@ public class Builder : IArrowArrayBuilder<bool, 
BooleanArray, Builder>
             public int NullCount => ValidityBuffer.UnsetBitCount;
 
             public Builder()
+                : this(null)
             {
-                ValueBuffer = new ArrowBuffer.BitmapBuilder();
-                ValidityBuffer = new ArrowBuffer.BitmapBuilder();
+            }
+
+            /// <summary>
+            /// Creates a builder whose working memory comes from <paramref 
name="allocator"/>.
+            /// </summary>
+            /// <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(MemoryAllocator allocator)
+            {
+                ValueBuffer = new ArrowBuffer.BitmapBuilder(0, allocator);
+                ValidityBuffer = new ArrowBuffer.BitmapBuilder(0, allocator);

Review Comment:
   Now that this builder can allocate from `CallerOwnedNativeAllocator`, 
`Build` needs failure cleanup: it builds the validity buffer and then the value 
buffer, so an allocation failure (or constructor failure) after the first step 
leaks the already-created buffer. Dispose intermediate buffers until the 
`BooleanArray` has taken ownership.



-- 
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]

Reply via email to