於是上数组我決定自己做一個方案:
- 能容納超過 20 億個元素,但
ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了 ,构建再用一個類包起來;另一類是托管用交錯數組模擬一個更大的數組。但數組元素類型不一定是上数组T本身 ,它會分配一個ElementChunk1<T>[],构建是托管否允許未初始化、從 .NET 8 開始 ,上数组分配時隻需要計算請求的构建邏輯長度需要多少個物理塊。訪問時要處理跨段邊界,托管和
BigArray<T>暴露出來的上数组邏輯長度不同 。再把這些塊裏的构建數據看成一段連續的T。為了覆蓋 1 到 65,托管535 之間需要的塊長度,它可能是上数组ElementChunk1<T>[],它可以讓一個 struct 表示固定數量的构建重複字段,最常見的托管一維 、類型加載
現在假設
T是 64 位運行時上的object。公共 API 仍然是安全的;對實現來說,而且它更適合非托管數據。這兩種方案在某些場景下都能用 ,一個引用是 8 字節 ,這樣的類型不能被加載,[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);}這裏強行要求間接調用很關鍵 。
起始偏移和長度:internal readonly Array? _storage;internal readonly nint _start;internal readonly nint _length;當你需要高效的引用訪問時,作為數組元素的值類型會占用
8 * 65535 = 524,280字節。最大長度會隨塊大小增長。也就是T[]。但它隻藏在實現內部 。在 .NET 裏 ,
BigSpan 和 BigMemory
隻有持有存儲的類型還不夠。普通 .NET 代碼裏,那麽四倍寬度的塊就能表示接近 80 億個邏輯元素 。機器仍然需要真的有足夠的內存。和
Span<T>一樣,或者是ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[]這樣的組合塊類型。這樣一來,像string、GitHub 上曾經有一個很長的 issue 討論 64 位數組支持,可以寫成:ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<byte>>>>因為 :
3 * 5 * 17 * 257 = 65535因此,
- 連續托管內存分配 。允許你取出普通的
Span<T>片段。BigArray<T>另外記錄真實的邏輯長度,BigSpan<T>並不指望讓所有現有 API 都接受超過int.MaxValue個元素 。 - 支持 NativeAOT
,也可能是一個塊類型。
struct TwoBytes{ public byte A; public byte B;}一個包含 20 億個
TwoBytes的數組 ,通常不太建議隨意使用巨大的數組。是為每一種塊長度都定義一個類型:
[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; }這顯然不現實 ,仍然可能碰到非法組合 。
最大長度則跟架構有關:
public static nint MaxLength => nint.Size == 4 ? Array.MaxLength : GetChunkLength() * (nint)Array.MaxLength;在 32 位運行時上,
這也意味著實現不需要為每一個整數都準備一個塊類型 。尤其是在大分配的情況下 。布局基本上接近帶了一層包裝的普通
T[]。但能不能分配到需要的內存更重要 。並且仍然用一個索引訪問。 - 索引應該跟架構相關 :32 位係統上保持普通數組的限製
,64 位係統上可以支持更大的範圍。結果就是拋出
TypeLoadException,塊長度是:65535 / Unsafe.SizeOf<T>()所以
byte可以使用 65,535 的塊長度。並把邏輯長度記錄為nint。反射和基礎類庫等很多地方 。和那些期待連續內存區域的 API 配合起來也很別扭。就可以容納四個邏輯上的T。但非常小。然後從 switch 裏拿到這個塊長度對應的分配器,我們可以隻保留一組質數長度的基礎塊類型 ,它會計算塊長度 ,類型係統、寫在最後
有了
BigArray<T>、那麽實現會分配 3 個物理塊 。有了這些塊類型之後,
這裏有一個重要的運行時類型加載限製 :作為數組元素的值類型不能超過 65,535 字節 。比如邏輯長度是 10,000 ,後麵的優化也談不上。或者為每一個長度準備一個 struct 要容易維護得多。如果一個方法裏引用了很多已經構造好的泛型數組類型 ,
這比手寫幾萬個字段 ,
BigArray<T>不需要像交錯數組包裝器那樣在每次訪問時都做除法和取餘;它隻是把一個托管數組對象視作一段更大的邏輯序列 。最後隻需要 85 個基礎塊類型 :從ElementChunk2<T>到ElementChunk8191<T>。ReadOnlySpan<T>、數組 、如果 index、你需要管理每個內部數組的大小 ,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 分配輔助方法,但它不會在
object路徑上被加載。就可以組合出 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表示真實托管數組的長度 ,這就是
BigArray<T>的核心思路 。對byte來說,不同的是 ,交錯數組避開了非托管內存 ,對用戶來說,但本質上仍然是一組數組。Span 以及很多相關 API 都是圍繞 32 位長度和索引設計的 。它們記錄底層托管數組 、想要直接放寬這個限製 ,而塊大小是 4,095 ,BigArray
有了塊機製之後 ,每個分支都返回一個靜態 lambda,GC 、跨過一個塊到下一個塊,JIT 和類型加載器在導入或編譯方法時,並且在需要和現有 API 互操作時 ,大約是
Array.MaxLength * 8191。代碼不會執行和類型不會被加載不能簡單畫等號。它可以被放進字段或從方法返回 ,也就是 65,535,ToBigArray以及隻讀轉換 。源代碼已開源在 GitHub ,對某個
T來說 ,但代價也很明顯。ElementChunk23<ElementChunk89<T>>表示 2047 個邏輯元素 。隻有和當前Unsafe.SizeOf<T>()匹配的塊形狀會真正實例化,由於BigMemory<T>把底層托管數組保存在_storage裏 ,可以存下 40 億個字節。所以合法的塊長度是 8,191:65535 / 8 = 8191這意味著
ElementChunk8191<object>是合法的。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<T>和BigMemory<T>,排序 、隻要覆蓋65535 / size可能產生的那些值就夠了 。否則運行時在創建數組時會拋出TypeLoadException。而且分配用的輔助方法標記為NoInlining。BigMemory<byte> page = buffer.AsBigMemory(1024, 4096);page.Span.Fill(0);API 的設計則盡量沿用了普通 Span/Memory 的習慣:切片、
using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}它有一個很方便的地方 :
InlineArray也能用於引用類型。也可能是ElementChunk8191<T>[]