托管數建超大在 組 上構
大約是上数组 Array.MaxLength * 65535;對 64 位運行時上的 long或對象引用來說,
寫在最後
有了 BigArray<T>、构建類型係統
、托管避免每一次邏輯訪問都再走一次普通數組邊界檢查 。上数组最大長度會隨塊大小增長 。构建然後實現使用引用偏移,托管想要直接放寬這個限製,上数组
[MethodImpl(MethodImplOptions.NoInlining)]private static Array AllocateArray<TElement>(int chunks,构建 bool pinned, bool uninitialized){ return uninitialized ? GC.AllocateUninitializedArray<TElement>(chunks, pinned) : GC.AllocateArray<TElement>(chunks, pinned);}這裏強行要求間接調用很關鍵
。一個 FourElements<T>數組的托管每個物理元素
,
有了這些塊類型之後
,上数组但最後以 "won't fix" 關閉,构建它仍然是托管一個托管數組對象
,我們可以隻保留一組質數長度的上数组基礎塊類型,但仍然不少 。构建BigArray<T>另外記錄真實的托管邏輯長度
,如果物理數組本身可以有接近 20 億個塊,如果一個方法裏引用了很多已經構造好的泛型數組類型,你需要管理每個內部數組的大小 ,而不用把每個字段都手寫出來 。
常見的解決辦法大概有兩類
:一類是分配非托管內存,我們還會用 Span<T>、真正的邏輯終點由 _length記錄 。
在 64 位運行時上,因為 JIT 隻會編譯實際創建出來的 lambda 背後的方法。複製、底層仍然是一個托管數組 ,性能很重要 ,和 BigArray<T>暴露出來的邏輯長度不同 。每個分支都返回一個靜態 lambda,它可以被放進字段或從方法返回 ,布局基本上接近帶了一層包裝的普通 T[]。而且對任意 T來說也不一定合法。而元素又內聯保存在這些塊裏
,它給你一個大索引視圖
,也就是 6 個邏輯 T。再把這些塊裏的數據看成一段連續的 T
。允許你取出普通的 Span<T>片段。訪問時要處理跨段邊界,
但這個限製針對的是數組的元素個數,也可能是一個塊類型
。byte能使用的最大塊長度,排序、也可能是 ElementChunk8191<T>[],
[InlineArray(2)]struct ElementChunk2<T>{ private T _first;}[InlineArray(3)]struct ElementChunk3<T>{ private T _first;}ElementChunk2<ElementChunk3<T>>表示 2 個包含 3 個值的塊 ,可以存下 40 億個字節。隻是每個元素變成了一小塊
。
using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}它有一個很方便的地方 :InlineArray也能用於引用類型
。再通過嵌套組合出其他長度。因此代碼隻需要拿到第一個邏輯 T的引用 ,這時最後一個塊隻使用 1 個字節,它們記錄底層托管數組
、或者是 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[]這樣的組合塊類型。
它隻保存兩個東西:
internal readonly Array _storage;internal readonly nint _length;普通長度下,大小為 32 字節的類型可以使用 2,047。更大的長度下,像 string 、仍然可能碰到非法組合。
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,這樣塊類型數量從 65,535 降到了 510,分配路徑會先計算 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表示真實托管數組的長度
,公共 API 仍然是安全的;對實現來說 ,所以 BigArray<T>保持普通數組的限製
。搜索、和 Span<T>一樣 ,它可以防止未選中的塊數組類型被提前加載
。從零開始的數組是 SZArray
,實現內部如果需要調用隻接受 Span<T>或 ReadOnlySpan<T>的 BCL API
,尤其是在大分配的情況下
。集合、
BigSpan 和 BigMemory
隻有持有存儲的類型還不夠 。也就是 65,535,ToBigArray以及隻讀轉換
。
struct TwoBytes{ public byte A; public byte B;}一個包含 20 億個 TwoBytes的數組,
數據引用是通過把數組數據開頭重新解釋為 T得到的
:
private static ref T GetDataReference(Array storage){ return ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(storage));}這就是為什麽連續存儲這個特性很重要。因為它包含 65,535 個 object 引用,隻有和當前 Unsafe.SizeOf<T>()匹配的塊形狀會真正實例化,由於 BigMemory<T>把底層托管數組保存在 _storage裏,length 或 slice 超出合法範圍,大約是 Array.MaxLength * 8191。
BigSpan<T>並不指望讓所有現有 API 都接受超過 int.MaxValue個元素 。但能不能分配到需要的內存更重要
。如果連內存都分配不出來 ,
[InlineArray(4)]struct FourStrings{ private string _first;}它也能用於泛型:
[InlineArray(4)]struct FourElements<T>{ private T _first;}這樣一來,會在到達這條路徑之前失敗。而且它更適合非托管數據。為了覆蓋 1 到 65,535 之間需要的塊長度,這裏我們不需要在每次訪問時都除以塊大小 。JIT 、即使真正想分配的是另一個塊形狀 :
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];}解決辦法是把真正的分配延遲到選中分支之後。但 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了,對某個 T來說
,不需要清零的性能敏感場景,而且塊大小是 65,535。
nint offset = (nint)5_000_000_000L;Span<byte> window = buffer.AsSpan(offset, length: 4096);分配 API
最簡單的分配方式自然是調用構造函數:
nint length = (nint)10_000_000_000L;BigArray<byte> buffer = new(length);不過 .NET 的數組也有顯式的 GC 分配輔助方法,它會讓 GC 壓力更大,
這就是 BigArray<T>的核心思路 。它不擁有內存,ReadOnlySpan<T>、
string和 object之類的引用類型。基本思路
在 .NET 中,反射和基礎類庫等很多地方。JIT 和類型加載器在導入或編譯方法時,
ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<byte>>>>因為 :
3 * 5 * 17 * 257 = 65535因此,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,長度是 nint,但非常小。最後隻需要 85 個基礎塊類型:從 ElementChunk2<T>到 ElementChunk8191<T>。
nint本身無法表示更大的索引空間,更進一步 ,
BigSpan<T>是一個麵向超大連續區域的棧上視圖:
public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}它的基本形狀和 Span<T>一樣:一個起始引用加一個長度。
類型加載
現在假設 T是 64 位運行時上的 object。而不是元素背後的字節數。最常見的一維、就把數據拆成能放進 int的片段來處理
。
BigArray
有了塊機製之後,這意味著它理論上可以表示接近 128 TiB 的數組,
BigMemory<byte> page = buffer.AsBigMemory(1024, 4096);page.Span.Fill(0);API 的設計則盡量沿用了普通 Span/Memory 的習慣
:切片、也就是 T[] 。隻是查看由別的對象保持存活的內存
,
這也是為什麽 _storage的類型是 Array:實際運行時類型取決於 T