CurtHagenlocher commented on code in PR #525:
URL: https://github.com/apache/arrow-dotnet/pull/525#discussion_r4793594453


##########
src/Apache.Arrow/Memory/AllocationLifetime.cs:
##########
@@ -0,0 +1,98 @@
+// 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.
+
+namespace Apache.Arrow.Memory
+{
+    /// <summary>
+    /// Describes what happens to a block of memory if the <see 
cref="ArrowBuffer"/> that owns it
+    /// is never disposed.
+    /// </summary>
+    /// <remarks>
+    /// <para>
+    /// The lifetime is stated by whoever creates the memory: by a <see 
cref="MemoryAllocator"/>
+    /// through <see cref="MemoryAllocator.Lifetime"/>, or by the caller when 
wrapping memory with
+    /// <see cref="ArrowBuffer(System.Buffers.IMemoryOwner{byte}, 
AllocationLifetime)"/>.
+    /// </para>
+    /// <para>
+    /// It describes a correctness contract, not a performance strategy. 
Disposing a buffer is
+    /// always at least as fast as not disposing it, whatever its lifetime.
+    /// </para>
+    /// <para>
+    /// Whatever the lifetime, a span over a buffer must not be used after the 
buffer is disposed:
+    /// disposal may free the memory or make it available for reuse. The 
values here describe only
+    /// what happens to memory whose buffer is <em>never</em> disposed.
+    /// </para>
+    /// <para>
+    /// Only <see cref="Finalized"/> causes Arrow to release memory from a 
finalizer. With the other
+    /// lifetimes, Arrow releases memory only when the last buffer using it is 
disposed.

Review Comment:
   Good catch. The lifetime only controls Arrow's own finalizer. 2fd4528 says 
so in the `AllocationLifetime` and `MemoryAllocator.Lifetime` docs: the 
`GarbageCollected` and `CallerOwned` guarantees only hold if the owner, and 
anything behind it, never frees or recycles the memory from a finalizer of its 
own. It uses `NativeMemoryAllocator` as the example: its memory is `Finalized` 
however you wrap it.
   



##########
src/Apache.Arrow/ArrowBuffer.Builder.cs:
##########
@@ -227,7 +227,7 @@ internal ArrowBuffer Build(int byteSize, MemoryAllocator 
allocator = default)
                 IMemoryOwner<byte> memoryOwner = 
memoryAllocator.Allocate(bufferLength);
                 Memory.Slice(0, currentBytesLength).CopyTo(memoryOwner.Memory);
 
-                return new ArrowBuffer(memoryOwner);
+                return new ArrowBuffer(memoryOwner, memoryAllocator.Lifetime);

Review Comment:
   Good catch. Fixed in 5f1334a, at the source: `MemoryAllocator.Allocate` now 
checks the allocator's `Lifetime` before allocating, and throws 
`InvalidOperationException` if it's `Unspecified` or undefined. So there's 
never memory that `ArrowBuffer` then refuses to take, at this call site or any 
other. `AllocatorReportingAnInvalidLifetimeIsRejectedBeforeAllocating` checks 
that nothing is allocated.
   



##########
src/Apache.Arrow/Ipc/MessageBody.cs:
##########
@@ -32,9 +32,10 @@ internal sealed class MessageBody
         private readonly SharedMemoryOwner _owner;
 
         /// <param name="owner">The body's memory, of which this takes 
ownership.</param>
-        public MessageBody(IMemoryOwner<byte> owner)
+        /// <param name="lifetime">The lifetime of the allocator <paramref 
name="owner"/> came from.</param>
+        public MessageBody(IMemoryOwner<byte> owner, AllocationLifetime 
lifetime)
         {
-            _owner = new SharedMemoryOwner(owner);
+            _owner = new SharedMemoryOwner(owner, finalizeWhenUnreachable: 
lifetime == AllocationLifetime.Finalized);

Review Comment:
   Agreed. 5f1334a moves the check into a shared helper. `MessageBody` now uses 
it, and releases the body before throwing, since it takes ownership of it. In 
practice the new check in `Allocate` rejects such an allocator before a body is 
ever allocated, so this is a backstop.
   



##########
src/Apache.Arrow/ArrowBuffer.cs:
##########
@@ -33,9 +33,63 @@ public ArrowBuffer(ReadOnlyMemory<byte> data)
             _memory = data;
         }
 
+        // Not used by Arrow itself: internal callers state a lifetime 
explicitly. It is kept because
+        // applications call it through reflection, from before the public 
constructor below existed;
+        // it keeps the behaviour it always had.
         internal ArrowBuffer(IMemoryOwner<byte> memoryOwner)
+            : this(memoryOwner, AllocationLifetime.Finalized)

Review Comment:
   The constructor is kept on purpose: applications call it through reflection, 
so it stays, still meaning `Finalized`. The PR description was out of date and 
now says so.
   



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