在 組建超大托管數 上構
internal static Func<int,托管 bool, bool, Array> CreateBigArrayAllocator(int chunkLength){ return chunkLength switch { 1 => static (chunks, pinned, uninitialized) => AllocateArray<ElementChunk1<T>>(chunks, pinned, uninitialized), ..., 8191 => static (chunks, pinned, uninitialized) => AllocateArray<ElementChunk8191<T>>(chunks, pinned, uninitialized), ..., 65535 => static (chunks, pinned, uninitialized) => AllocateArray<ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>>(chunks, pinned, uninitialized), ..., _ => throw new UnreachableException(), };}實際的 switch 有 510 個 case,而且塊大小是上数组 65,535。就把數據拆成能放進 int的构建片段來處理。就可以組合出 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表示真實托管數組的長度
,反射以及大量現有代碼 。上数组數組隻是构建編程模型的一部分。而且對任意 T來說也不一定合法。托管類型係統
、上数组而且它更適合非托管數據。构建而且分配用的托管輔助方法標記為 NoInlining。不同的上数组是,
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;}然後是构建索引器實現。集合、托管準確地說是 127.998 TiB 。分配時隻需要計算請求的邏輯長度需要多少個物理塊 。這裏當然說的是理論上限,
但這個限製針對的是數組的元素個數 ,塊長度是 :
65535 / Unsafe.SizeOf<T>()所以 byte可以使用 65,535 的塊長度 。由於 BigMemory<T>把底層托管數組保存在 _storage裏 ,GitHub 上曾經有一個很長的 issue 討論 64 位數組支持,Span 以及很多相關 API 都是圍繞 32 位長度和索引設計的。但數組元素類型不一定是 T本身 ,大約是 Array.MaxLength * 65535;對 64 位運行時上的 long或對象引用來說,
[InlineArray(4)]struct FourStrings{ private string _first;}它也能用於泛型 :
[InlineArray(4)]struct FourElements<T>{ private T _first;}這樣一來,代碼不會執行和類型不會被加載不能簡單畫等號。然後實現使用引用偏移
,真正的邏輯終點由 _length記錄。
[InlineArray(2)]struct ElementChunk2<T>{ private T _first;}[InlineArray(3)]struct ElementChunk3<T>{ private T _first;}ElementChunk2<ElementChunk3<T>>表示 2 個包含 3 個值的塊
,代碼會選擇 8191分支並創建 ElementChunk8191<object>[];65535分支仍然存在給用於 byte這樣的類型使用,但非常小 。就可以容納四個邏輯上的 T。
最大長度則跟架構有關:
public static nint MaxLength => nint.Size == 4 ? Array.MaxLength : GetChunkLength() * (nint)Array.MaxLength;在 32 位運行時上
,而不用把每個字段都手寫出來 。它會讓 GC 壓力更大 ,BigArray<T>另外記錄真實的邏輯長度
,如果連內存都分配不出來,而元素又內聯保存在這些塊裏,
sizeof(T) | chunkSize | 最壞情況多出的元素數 | 最壞情況多出的字節數 |
|---|---|---|---|
| 1 | 65535 | 65534 | 65534 B |
| 2 | 32767 | 32766 | 65532 B |
| 3 | 21845 | 21844 | 65532 B |
| 4 | 16383 | 16382 | 65528 B |
| 8 | 8191 | 8190 | 65520 B |
| 16 | 4095 | 4094 | 65504 B |
| 257 | 255 | 254 | 65278 B |
| 32768+ | 1 | 0 | 0 B |
可以看到最壞情況是邏輯長度剛好比塊大小的整數倍多 1 ,split、
在 64 位運行時上 ,我們還會用 Span<T>、byte[1024]存 1024 字節,像 string、排序
、反射和基礎類庫等很多地方 。其他長度都可以由這些基礎長度相乘得到
。它會分配一個 ElementChunk1<T>[],
BigSpan 和 BigMemory
隻有持有存儲的類型還不夠。它可以讓一個 struct 表示固定數量的重複字段 ,起始偏移和長度 :
internal readonly Array? _storage;internal readonly nint _start;internal readonly nint _length;當你需要高效的引用訪問時 ,再把這些塊裏的數據看成一段連續的 T。允許你取出普通的 Span<T>片段
。所以 BigArray<T>保持普通數組的限製 。
通常不太建議隨意使用巨大的數組 。避免每一次邏輯訪問都再走一次普通數組邊界檢查 。
BigSpan<T>是一個麵向超大連續區域的棧上視圖 :
public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}它的基本形狀和 Span<T>一樣:一個起始引用加一個長度
。最常見的一維
、
這就是 BigArray<T>的核心思路 。並且仍然用一個索引訪問。或者為每一個長度準備一個 struct 要容易維護得多。
byte來說,分配器來自一個針對塊長度的 switch。會在到達這條路徑之前失敗 。則可以盡量接近直接數組訪問的成本。再通過嵌套組合出其他長度。
Unsafe.Add(ref first, index)會移動 index個邏輯 T元素
。就會碰到 GC、而塊大小是 4,095
,因為 JIT 隻會編譯實際創建出來的 lambda 背後的方法 。為了覆蓋 1 到 65,535 之間需要的塊長度,它們的數組長度相同 ,BigArray
有了塊機製之後,但能不能分配到需要的內存更重要 。
它隻保存兩個東西:
internal readonly Array _storage;internal readonly nint _length;普通長度下
,nint本身無法表示更大的索引空間,和那些期待連續內存區域的 API 配合起來也很別扭
。分配路徑會先計算 T對應的合法塊長度,是為每一種塊長度都定義一個類型:
[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; }這顯然不現實,確定這個值之後 ,object這樣的引用類型就不適合這個方向。可以寫成:
ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<byte>>>>因為:
3 * 5 * 17 * 257 = 65535因此,剩下的部分都空著
。底層仍然是一個托管數組
,隻是在同一段數組數據區裏繼續往前走 。這裏我們不需要在每次訪問時都除以塊大小 。最後隻需要 85 個基礎塊類型:從 ElementChunk2<T>到 ElementChunk8191<T>。但 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了,所以我也提供了對應的 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);這樣你可以控製分配是否清零、
BigArray<byte> buffer = new((nint)Array.MaxLength + 1024);BigSpan<byte> span = buffer.AsBigSpan();span[Array.MaxLength] = 42;BigMemory<T>和 BigReadOnlyMemory<T>則是可以保存起來的視圖 。然後用普通的引用偏移往後移動
。而不是元素背後的字節數
。對於 byte
功成身退網