byte能使用的上数组最大塊長度,有了 BigArray<T> 、构建反射以及大量現有代碼 。托管或者為每一個長度準備一個 struct 要容易維護得多
。上数组
BigSpan<T>並不指望讓所有現有 API 都接受超過 int.MaxValue個元素 。构建一個 FourElements<T>數組的托管每個物理元素
,它會計算塊長度,上数组排序
、构建
更進一步 ,托管但非常小
。上数组這樣塊類型數量從 65,构建535 降到了 510,split、托管我們就可以用接近普通數組的上数组方式處理超大的連續托管內存。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;}然後是托管索引器實現。
麻煩的地方在於
,機器仍然需要真的有足夠的內存。int[1024]存 4096 字節。其他長度都可以由這些基礎長度相乘得到。
nint offset = (nint)5_000_000_000L;Span<byte> window = buffer.AsSpan(offset, length: 4096);最簡單的分配方式自然是調用構造函數:
nint length = (nint)10_000_000_000L;BigArray<byte> buffer = new(length);不過 .NET 的數組也有顯式的 GC 分配輔助方法 ,
現在假設 T是 64 位運行時上的 object。作為數組元素的值類型會占用 8 * 65535 = 524,280字節。也可能是一個塊類型。對於 object
,比如邏輯長度是 10,000
,
using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}它有一個很方便的地方:InlineArray也能用於引用類型
。數組 、它會分配一個 ElementChunk1<T>[],最後隻需要 85 個基礎塊類型:從 ElementChunk2<T>到 ElementChunk8191<T> 。
Span<T>或 ReadOnlySpan<T>的 BCL API
,但仍然不少。GitHub 上曾經有一個很長的 issue 討論 64 位數組支持 ,GC
、byte[1024]存 1024 字節
,但最後以 "won't fix" 關閉
,它們記錄底層托管數組、而不用把每個字段都手寫出來。它隻保存兩個東西:
internal readonly Array _storage;internal readonly nint _length;普通長度下,
隻有持有存儲的類型還不夠 。不同的是,為了覆蓋 1 到 65,535 之間需要的塊長度,仍然可能碰到非法組合。這樣一來,和那些期待連續內存區域的 API 配合起來也很別扭 。就可以組合出 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。就會碰到 GC 、最後一個塊隻用到一部分,大約是 Array.MaxLength * 8191 。大小為 32 字節的類型可以使用 2,047。由於 BigMemory<T>把底層托管數組保存在 _storage裏,它給你一個大索引視圖
,分配選中的塊數組
,每個分支都返回一個靜態 lambda,但能不能分配到需要的內存更重要
。隻是在同一段數組數據區裏繼續往前走。底層仍然是一個托管數組,並把邏輯長度記錄為 nint
。大約是 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);}這裏強行要求間接調用很關鍵。最大長度會隨塊大小增長。隻是每個元素變成了一小塊。對用戶來說,這樣的類型不能被加載,因此不能依賴運行時代碼生成或反射。然後從 switch 裏拿到這個塊長度對應的分配器,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
,反射和基礎類庫等很多地方。底層是一個托管數組 ,和 BigArray<T>暴露出來的邏輯長度不同 。
[InlineArray(4)]struct FourStrings{ private string _first;}它也能用於泛型 :
[InlineArray(4)]struct FourElements<T>{ private T _first;}這樣一來
,隻是查看由別的對象保持存活的內存
,它仍然是一個托管數組對象,也就是 65,535
,或者是 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[]這樣的組合塊類型
。就可以容納四個邏輯上的 T 。如果連內存都分配不出來,準確地說是 127.998 TiB 。但它不會在 object路徑上被加載。可以寫成 :
ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<byte>>>>因為 :
3 * 5 * 17 * 257 = 65535因此,拿到第一個數據引用之後 ,類型係統 、搜索、隻有和當前 Unsafe.SizeOf<T>()匹配的塊形狀會真正實例化
,但本質上仍然是一組數組 。起始偏移和長度:
internal readonly Array? _storage;internal readonly nint _start;internal readonly nint _length;當你需要高效的引用訪問時,ElementChunk23<ElementChunk89<T>>表示 2047 個邏輯元素。這意味著它理論上可以表示接近 128 TiB 的數組
,
最大長度則跟架構有關 :
public static nint MaxLength => nint.Size == 4 ? Array.MaxLength : GetChunkLength() * (nint)Array.MaxLength;在 32 位運行時上,
常見的解決辦法大概有兩類:一類是分配非托管內存 ,而不是元素背後的字節數 。公共 API 仍然是安全的;對實現來說,分配路徑會先計算 T對應的合法塊長度,
但這個限製針對的是數組的元素個數 ,因為這件事會牽涉到運行時、
有了這些塊類型之後,分配時隻需要計算請求的邏輯長度需要多少個物理塊 。性能很重要,否則運行時在創建數組時會拋出 TypeLoadException