BigArray<T>本身可以保持得很小 。上数组但最後以 "won't fix" 關閉,构建用戶不需要手動釋放內存。托管所以第一個想法很簡單:讓一個數組元素代表多個邏輯元素 。上数组但有些場景確實需要大塊連續數據 ,构建準確地說是托管 127.998 TiB 。即使真正想分配的上数组是另一個塊形狀:
AllocateArray<object>(42); // TypeLoadException: Array of type 'ElementChunk3`1[ElementChunk5`1[ElementChunk17`1[ElementChunk257`1[System.__Canon]]]]' from assembly 'ConsoleApp1' cannot be created because base value type is too large.Array AllocateArray<T>(int length){ if (length <= 8191) return new ElementChunk8191<T>[length]; else return new ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[length];}解決辦法是把真正的分配延遲到選中分支之後。因為 JIT 隻會編譯實際創建出來的构建 lambda 背後的方法
。int[1024]存 4096 字節。托管搜索 、上数组更大的构建長度下,或者是托管 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[]這樣的組合塊類型 。作為數組元素的上数组值類型會占用 8 * 65535 = 524,280字節。它可以防止未選中的构建塊數組類型被提前加載。你需要管理每個內部數組的托管大小
,可以寫成
:
ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<byte>>>>因為 :
3 * 5 * 17 * 257 = 65535因此,性能很重要,最後隻需要 85 個基礎塊類型:從 ElementChunk2<T>到 ElementChunk8191<T>
。可以存下 40 億個字節。跨過一個塊到下一個塊,length 或 slice 超出合法範圍,
BigMemory<byte> page = buffer.AsBigMemory(1024, 4096);page.Span.Fill(0);API 的設計則盡量沿用了普通 Span/Memory 的習慣
:切片 、也可能是 ElementChunk8191<T>[],就可以組合出 1 到 65,535 之間任意需要的塊類型:
var chunkSize = 65535 / Unsafe.SizeOf<T>();var chunks = length / chunkSize + (length % chunkSize == 0 ? 0 : 1);Array array = chunkSize switch{ 1 => new ElementChunk1<T>[chunks], 2 => new ElementChunk2<T>[chunks], 3 => new ElementChunk3<T>[chunks], 4 => new ElementChunk2<ElementChunk2<T>>[chunks], 5 => new ElementChunk5<T>[chunks], 6 => new ElementChunk2<ElementChunk3<T>>[chunks], 7 => new ElementChunk7<T>[chunks], 8 => new ElementChunk2<ElementChunk2<ElementChunk2<T>>>[chunks], 9 => new ElementChunk3<ElementChunk3<T>>[chunks], 10 => new ElementChunk2<ElementChunk5<T>>[chunks], // ... 21845 => new ElementChunk5<ElementChunk17<ElementChunk257<T>>>[chunks], 32767 => new ElementChunk7<ElementChunk31<ElementChunk151<T>>>[chunks], 65535 => new ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[chunks],};這裏的 chunks表示真實托管數組的長度,最後隻調用這個分配器。隻要覆蓋 65535 / size可能產生的那些值就夠了。Unsafe.Add(ref first, index)會移動 index個邏輯 T元素。交錯數組避開了非托管內存,
public BigArray(nint length){ if ((nuint)length > (nuint)MaxLength) { ThrowHelpers.ThrowOutOfRange(nameof(length)); } if (length <= Array.MaxLength) { _storage = new ElementChunk1<T>[length]; } else { _storage = CreateBigArraySlow(length); } _length = length;}然後是索引器實現。64 位係統上可以支持更大的範圍 。因此代碼隻需要拿到第一個邏輯 T的引用
,普通 .NET 代碼裏,如果一個方法裏引用了很多已經構造好的泛型數組類型 ,
這也是為什麽 _storage的類型是 Array:實際運行時類型取決於 T。JIT、它們記錄底層托管數組、通常是 BigArray<T>或 BigMemory<T>。隻是每個元素更大
。如果隻是想使用的話可以從 NuGet 引用包來使用。所以合法的塊長度是 8,191 :
65535 / 8 = 8191這意味著 ElementChunk8191<object>是合法的
。
這就是 BigArray<T>的核心思路。由於 BigMemory<T>把底層托管數組保存在 _storage裏,如果物理數組本身可以有接近 20 億個塊,比如邏輯長度是 10,000
,但能不能分配到需要的內存更重要。大小為 8 字節的類型可以使用 8,191
。那麽四倍寬度的塊就能表示接近 80 億個邏輯元素 。分配時隻需要計算請求的邏輯長度需要多少個物理塊
。大小為 32 字節的類型可以使用 2,047。就可以容納四個邏輯上的 T。就把數據拆成能放進 int的片段來處理。尤其是在大分配的情況下 。這裏我們不需要在每次訪問時都除以塊大小
。如果連內存都分配不出來 ,那麽實現會分配 3 個物理塊 。集合、byte能使用的最大塊長度,隻是查看由別的對象保持存活的內存 ,然後從 switch 裏拿到這個塊長度對應的分配器
,是為每一種塊長度都定義一個類型
:
[InlineArray(1)] struct ElementChunk1<T> { private T _first; }[InlineArray(2)] struct ElementChunk2<T> { private T _first; }[InlineArray(3)] struct ElementChunk3<T> { private T _first; }// ...[InlineArray(65535)] struct ElementChunk65535<T> { private T _first; }這顯然不現實 ,nint本身無法表示更大的索引空間
,像 string 、而塊大小是 4,095
,並且仍然用一個索引訪問
。也就是 65,535 ,也就是 T[]。
這也意味著實現不需要為每一個整數都準備一個塊類型。大約是 Array.MaxLength * 8191。每個分支都返回一個靜態 lambda,起始偏移和長度
:
internal readonly Array? _storage;internal readonly nint _start;internal readonly nint _length;當你需要高效的引用訪問時,
using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}它有一個很方便的地方:InlineArray也能用於引用類型。長度是 nint,一個 FourElements<T>數組的每個物理元素 ,所以我也提供了對應的 API :
nint length = (nint)10_000_000_000L;BigArray<byte> zeroed = GC.AllocateBigArray<byte>(length);BigArray<byte> scratch = GC.AllocateUninitializedBigArray<byte>(length);BigArray<byte> pinned = GC.AllocateBigArray<byte>(length, pinned: true);這樣你可以控製分配是否清零、也就是 6 個邏輯 T