Skip to content

Commit b91e4f5

Browse files
committed
Fix code review findings
1 parent 251d955 commit b91e4f5

File tree

6 files changed

+24
-13
lines changed

6 files changed

+24
-13
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Text;
8+
9+
namespace Microsoft.Data.Analysis
10+
{
11+
internal static class ArrayUtility
12+
{
13+
// Maximum size of one-dimensional array.
14+
// See: https://msdn.microsoft.com/en-us/library/hh285054(v=vs.110).aspx
15+
// Polyfilling Array.MaxLength API for netstandard2.0
16+
public const int ArrayMaxSize = 0X7FEFFFFF;
17+
}
18+
}

src/Microsoft.Data.Analysis/DataFrameBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void EnsureCapacity(int numberOfValues)
8080
if (newLength > Capacity)
8181
{
8282
//Double buffer size, but not higher than MaxByteCapacity
83-
var doubledSize = (int)Math.Min((long)ReadOnlyBuffer.Length * 2, MaxCapacityInBytes);
83+
var doubledSize = (int)Math.Min((long)ReadOnlyBuffer.Length * 2, ArrayUtility.ArrayMaxSize);
8484
var newCapacity = Math.Max(newLength * Size, doubledSize);
8585

8686
var memory = new Memory<byte>(new byte[newCapacity]);

src/Microsoft.Data.Analysis/ReadOnlyDataFrameBuffer.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ public ReadOnlyMemory<T> RawReadOnlyMemory
3636

3737
protected int Capacity => ReadOnlyBuffer.Length / Size;
3838

39-
//The maximum size in any single dimension for byte array is 0x7FFFFFc7 - 2147483591
40-
//See https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element
41-
public const int MaxCapacityInBytes = 2147483591;
42-
43-
public static int MaxCapacity => MaxCapacityInBytes / Size;
39+
public static int MaxCapacity => ArrayUtility.ArrayMaxSize / Size;
4440

4541
public ReadOnlySpan<T> ReadOnlySpan
4642
{

src/Microsoft.Data.Analysis/StringDataFrameColumn.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ namespace Microsoft.Data.Analysis
1818
/// <remarks> Is NOT Arrow compatible </remarks>
1919
public partial class StringDataFrameColumn : DataFrameColumn, IEnumerable<string>
2020
{
21-
//The maximum size in any single dimension for array containing other types than byte or single byte structure is 0X7FEFFFFF - 2146435071
22-
//See https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element
23-
public static int MaxCapacity = 2146435071 / Unsafe.SizeOf<IntPtr>(); // Size of pointer
21+
public static int MaxCapacity = ArrayUtility.ArrayMaxSize / Unsafe.SizeOf<IntPtr>(); // Max Size in bytes / size of pointer (8 bytes on x64)
2422

2523
private readonly List<List<string>> _stringBuffers = new List<List<string>>(); // To store more than intMax number of strings
2624

src/Microsoft.Data.Analysis/VBufferDataFrameColumn.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ namespace Microsoft.Data.Analysis
1818
/// </summary>
1919
public partial class VBufferDataFrameColumn<T> : DataFrameColumn, IEnumerable<VBuffer<T>>
2020
{
21-
//The maximum size in any single dimension for array containing other types than byte or single byte structure is 0X7FEFFFFF - 2146435071
22-
//See https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element
23-
public static int MaxCapacity = 2146435071 / Unsafe.SizeOf<VBuffer<T>>();
21+
22+
public static int MaxCapacity = ArrayUtility.ArrayMaxSize / Unsafe.SizeOf<VBuffer<T>>();
2423

2524
private readonly List<List<VBuffer<T>>> _vBuffers = new List<List<VBuffer<T>>>(); // To store more than intMax number of vbuffers
2625

test/Microsoft.Data.Analysis.Tests/BufferTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public void TestAppend_SizeMoreThanMaxBufferCapacity()
457457
[X64Fact("32-bit dosn't allow to allocate more than 2 Gb")]
458458
public void TestAppendMany_SizeMoreThanMaxBufferCapacity()
459459
{
460-
const int MaxCapacityInBytes = 2147483591;
460+
const int MaxCapacityInBytes = 0X7FEFFFFF;
461461

462462
//Check appending values with extending column size over MaxCapacity of ReadOnlyDataFrameBuffer
463463
PrimitiveDataFrameColumn<byte> intColumn = new PrimitiveDataFrameColumn<byte>("Byte1", MaxCapacityInBytes - 5);

0 commit comments

Comments
 (0)