Copilot commented on code in PR #419:
URL: https://github.com/apache/arrow-dotnet/pull/419#discussion_r3818376429
##########
src/Apache.Arrow/Memory/NativeBuffer.cs:
##########
@@ -109,6 +106,24 @@ public void Grow(int newElementCount, bool zeroFill = true)
Length = newCount;
}
+ /// <summary>
+ /// The element count to grow to: double the current length to
amortise repeated grows, but never
+ /// past the largest buffer that can be addressed, and never below
what the caller asked for.
+ /// </summary>
+ /// <remarks>
+ /// Doubling used to be unconditional and checked, so once the buffer
passed half of the maximum
+ /// its next grow threw <see cref="OverflowException"/> however little
was asked for, even though
+ /// the requested size still fit. Saturating instead keeps growth
amortised right up to the
+ /// ceiling; a request that genuinely cannot be addressed still
overflows at the byte-size
+ /// calculation in <see cref="Grow"/>, as before.
+ /// </remarks>
+ internal static int ComputeGrowCount(int length, int newElementCount,
int elementSize)
+ {
+ int maxCount = int.MaxValue / elementSize;
+ long doubled = (long)length * 2;
+ return (int)Math.Max(newElementCount, Math.Min(doubled, maxCount));
+ }
Review Comment:
ComputeGrowCount divides by elementSize; if elementSize is 0 (or negative),
this will throw DivideByZeroException (or compute an invalid maxCount). Since
this helper is now callable from tests/other internal code, add a simple
argument check so failures are explicit and easier to diagnose.
--
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]