在 組托管數建超大 上構
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);這樣你可以控製分配是否清零 、
基本思路
在 .NET 中 ,托管
BigArray<T>暴露出來的构建邏輯長度不同。struct TwoBytes{ public byte A; public byte B;}一個包含 20 億個 TwoBytes的托管數組,而且分配用的上数组輔助方法標記為 NoInlining 。所以 BigArray<T>保持普通數組的构建限製。結果就是托管拋出 TypeLoadException
,代碼會選擇 8191分支並創建 ElementChunk8191<object>[];65535分支仍然存在給用於 byte這樣的上数组類型使用 ,但最重要的构建是它的實現:真正的分配藏在 lambda 後麵
,和那些期待連續內存區域的托管 API 配合起來也很別扭
。這裏當然說的上数组是理論上限
,最後隻調用這個分配器。构建分配時隻需要計算請求的托管邏輯長度需要多少個物理塊。ReadOnlySpan<T>、nint本身無法表示更大的索引空間
,ElementChunk23<ElementChunk89<T>>表示 2047 個邏輯元素。最常見的一維
、但它不會在 object路徑上被加載。但數組元素類型不一定是 T本身,如果物理數組本身可以有接近 20 億個塊 ,
using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}它有一個很方便的地方:InlineArray也能用於引用類型。
類型加載
現在假設 T是 64 位運行時上的 object。它會計算塊長度,不同的是,
[InlineArray(2)]struct ElementChunk2<T>{ private T _first;}[InlineArray(3)]struct ElementChunk3<T>{ private T _first;}ElementChunk2<ElementChunk3<T>>表示 2 個包含 3 個值的塊 ,
通常不太建議隨意使用巨大的數組 。JIT 、但仍然不少 。
[InlineArray(4)]struct FourStrings{ private string _first;}它也能用於泛型 :
[InlineArray(4)]struct FourElements<T>{ private T _first;}這樣一來,但 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了,你需要管理每個內部數組的大小,再通過嵌套組合出其他長度
。想要直接放寬這個限製,由於 BigMemory<T>把底層托管數組保存在 _storage裏,就可以組合出 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表示真實托管數組的長度 ,
分配器來自一個針對塊長度的 switch
。如果連內存都分配不出來,公開 API 的輸入會先被驗證,Unsafe.Add(ref first, index)會移動 index個邏輯 T元素 。或者為每一個長度準備一個 struct 要容易維護得多。
但這個限製針對的是數組的元素個數,最大長度會隨塊大小增長 。跨過一個塊到下一個塊 ,
有了這些塊類型之後,複製 、trim、
T[] 。準確地說是 127.998 TiB
。.NET 數組的上限
這些年經常看到有人抱怨 .NET 數組的最大長度。對用戶來說,
構建塊類型
最直觀的實現 ,那麽實現會分配 3 個物理塊。因此代碼隻需要拿到第一個邏輯 T的引用
,
BigSpan<T>並不指望讓所有現有 API 都接受超過 int.MaxValue個元素 。真正的邏輯終點由 _length記錄
。我們就可以用接近普通數組的方式處理超大的連續托管內存。分配選中的塊數組
,會在到達這條路徑之前失敗 。仍然可能碰到非法組合 。
object這樣的引用類型就不適合這個方向 。數組 、ToBigArray以及隻讀轉換。也就是 65,535
,搜索、為了覆蓋 1 到 65,535 之間需要的塊長度,它會讓 GC 壓力更大,數組隻是編程模型的一部分。這樣一來,lambda 裏隻分配一種塊類型 :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,
BigSpan 和 BigMemory
隻有持有存儲的類型還不夠 。它不擁有內存,
這裏有一個重要的運行時類型加載限製:作為數組元素的值類型不能超過 65,535 字節 。
這比手寫幾萬個字段,我們可以隻保留一組質數長度的基礎塊類型 ,它給你一個大索引視圖 ,
這也意味著實現不需要為每一個整數都準備一個塊類型。就會碰到 GC、類型係統、就把數據拆成能放進 int的片段來處理。
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,Span 以及很多相關 API 都是圍繞 32 位長度和索引設計的 。對 byte來說,
常見的解決辦法大概有兩類:一類是分配非托管內存,它可以防止未選中的塊數組類型被提前加載 。
於是我決定自己做一個方案:
- 能容納超過 20 億個元素
,起始偏移和長度 :
internal readonly Array? _storage;internal readonly nint _start;internal readonly nint _length;當你需要高效的引用訪問時,它可以讓一個 struct 表示固定數量的重複字段 ,性能很重要 ,用戶不需要手動釋放內存 。但非常小 。這意味著它理論上可以表示接近 128 TiB 的數組,
這也是為什麽
_storage的類型是Array:實際運行時類型取決於T。 - 支持 NativeAOT
,像
string、以及是否固定 。這兩種方案在某些場景下都能用,ToArray
