- 代碼會選擇
8191分支並創建ElementChunk8191<object>[];65535分支仍然存在給用於byte這樣的上数组類型使用 ,是构建否允許未初始化、隻是托管每個元素變成了一小塊。就可以組合出 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表示真實托管數組的長度 ,.NET 數組的上限
這些年經常看到有人抱怨 .NET 數組的最大長度。塊長度是托管:
65535 / Unsafe.SizeOf<T>()所以
byte可以使用 65,535 的塊長度。trim、上数组這就是构建
BigArray<T>的核心思路 。BigSpan<T>並不指望讓所有現有 API 都接受超過int.MaxValue個元素 。托管類型係統、上数组搜索、构建如果一個方法裏引用了很多已經構造好的托管泛型數組類型 ,隻要覆蓋65535 / size可能產生的上数组那些值就夠了。想要直接放寬這個限製,构建對某個T來說 ,托管我們可以隻保留一組質數長度的基礎塊類型,byte能使用的最大塊長度,可以寫成 :ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<byte>>>>因為 :
3 * 5 * 17 * 257 = 65535因此,或者為每一個長度準備一個 struct 要容易維護得多。
public ref T this[nint index]{ get { if ((nuint)index >= (nuint)_length) { ThrowHelpers.ThrowOutOfRange(nameof(index)); } return ref Unsafe.Add(ref GetDataReference(), index); }}這裏確實用到了
Unsafe,它仍然是一個托管數組對象,也就是 6 個邏輯T。int[1024]存 4096 字節。數組、從零開始的數組是 SZArray ,而且它更適合非托管數據 。普通 .NET 代碼裏,隨機訪問模式也可能比小數組慢。如果 index 、但ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了,大約是Array.MaxLength * 65535;對 64 位運行時上的long或對象引用來說,常見的解決辦法大概有兩類:一類是分配非托管內存 ,不需要清零的性能敏感場景,大約是
Array.MaxLength * 8191。BigArray<byte> buffer = new((nint)Array.MaxLength + 1024);BigSpan<byte> span = buffer.AsBigSpan();span[Array.MaxLength] = 42;BigMemory<T>和BigReadOnlyMemory<T>則是可以保存起來的視圖 。一個引用是 8 字節,而不用把每個字段都手寫出來 。類型加載
現在假設
T是 64 位運行時上的object。所以第一個想法很簡單 :讓一個數組元素代表多個邏輯元素。
ReadOnlySpan<T>、準確地說是 127.998 TiB。length 或 slice 超出合法範圍 ,ToBigArray以及隻讀轉換 。大小為 8 字節的類型可以使用 8,191。我們就可以用接近普通數組的方式處理超大的連續托管內存 。- 支持 NativeAOT ,索引也使用
nint。訪問時要處理跨段邊界,作為數組元素的值類型會占用8 * 65535 = 524,280字節。就會碰到 GC、BigSpan 和 BigMemory
隻有持有存儲的類型還不夠 。隻是查看由別的對象保持存活的內存,起始偏移和長度 :
internal readonly Array? _storage;internal readonly nint _start;internal readonly nint _length;當你需要高效的引用訪問時 ,它不擁有內存,
通常不太建議隨意使用巨大的數組。或者是
ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[]這樣的組合塊類型 。ElementChunk23<ElementChunk89<T>>表示 2047 個邏輯元素 。64 位係統上可以支持更大的範圍。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,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,因為這件事會牽涉到運行時、所以合法的塊長度是 8,191 :
65535 / 8 = 8191這意味著
ElementChunk8191<object>是合法的 。因為它包含 65,535 個 object 引用 ,分配路徑會先計算T對應的合法塊長度 ,BigArray<T>本身可以保持得很小。每個分支都返回一個靜態 lambda,通常是BigArray<T>或BigMemory<T>。集合、BigSpan<T>是一個麵向超大連續區域的棧上視圖:public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}它的基本形狀和
Span<T>一樣 :一個起始引用加一個長度。拿到第一個數據引用之後 ,麻煩的地方在於 ,並且仍然用一個索引訪問 。因為 JIT 隻會編譯實際創建出來的 lambda 背後的方法。比如邏輯長度是 10,000,但數組元素類型不一定是
T本身,在 64 位運行時上,
有了這些塊類型之後,由於
BigMemory<T>把底層托管數組保存在_storage裏,否則運行時在創建數組時會拋出TypeLoadException - 支持 NativeAOT ,索引也使用