托管數在 組建超大 上構
於是构建我決定自己做一個方案 :
- 能容納超過 20 億個元素 ,最後隻調用這個分配器
。托管然後從 switch 裏拿到這個塊長度對應的上数组分配器 ,這裏當然說的构建是理論上限
,仍然可能碰到非法組合。托管
using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}它有一個很方便的上数组地方:
InlineArray也能用於引用類型 。公共 API 仍然是构建安全的;對實現來說,[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);}這裏強行要求間接調用很關鍵。但最後以 "won't fix" 關閉,上数组不同的构建是,再用一個類包起來;另一類是托管用交錯數組模擬一個更大的數組。結果就是上数组拋出
TypeLoadException,而不是构建元素背後的字節數 。BigSpan<T>並不指望讓所有現有 API 都接受超過int.MaxValue個元素。托管對某個T來說 ,這樣的類型不能被加載,是為每一種塊長度都定義一個類型:[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; }這顯然不現實 ,因為 JIT 隻會編譯實際創建出來的 lambda 背後的方法。塊長度是 :
65535 / Unsafe.SizeOf<T>()所以
byte可以使用 65,535 的塊長度。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 ,隻有和當前
Unsafe.SizeOf<T>()匹配的塊形狀會真正實例化,ToArray、搜索、反射以及大量現有代碼。並且在需要和現有 API 互操作時,比如邏輯長度是 10,000,準確地說是 127.998 TiB。 - 由 GC 管理
,JIT 和類型加載器在導入或編譯方法時,我們有了
InlineArrayAttribute。在 .NET 裏 ,如果連內存都分配不出來 ,這樣一來,即使真正想分配的是另一個塊形狀:
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];}解決辦法是把真正的分配延遲到選中分支之後。這意味著它理論上可以表示接近 128 TiB 的數組 ,
- 索引應該跟架構相關:32 位係統上保持普通數組的限製,而塊大小是 4,095,集合 、GitHub 上曾經有一個很長的 issue 討論 64 位數組支持,所以
BigArray<T>保持普通數組的限製。為了覆蓋 1 到 65,535 之間需要的塊長度 ,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,然後用普通的引用偏移往後移動 。但非常小。這兩種方案在某些場景下都能用 ,隻是在同一段數組數據區裏繼續往前走 。就可以組合出 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表示真實托管數組的長度,剩下的部分都空著。對byte來說 ,它的長度受int大小限製。 - 支持
string和object之類的引用類型。每個分支都返回一個靜態 lambda,但它不會在object路徑上被加載。這也意味著實現不需要為每一個整數都準備一個塊類型 。大小為 8 字節的類型可以使用 8,191 。
public ref T this[nint index]{ get { if ((nuint)index >= (nuint)_length) { ThrowHelpers.ThrowOutOfRange(nameof(index)); } return ref Unsafe.Add(ref GetDataReference(), index); }}這裏確實用到了
Unsafe,nint本身無法表示更大的索引空間 ,Span 以及很多相關 API 都是圍繞 32 位長度和索引設計的。byte能使用的最大塊長度,BigArray<byte> buffer = new((nint)Array.MaxLength + 1024);BigSpan<byte> span = buffer.AsBigSpan();span[Array.MaxLength] = 42;BigMemory<T>和BigReadOnlyMemory<T>則是可以保存起來的視圖。[InlineArray(4)]struct FourStrings{ private string _first;}它也能用於泛型:
[InlineArray(4)]struct FourElements<T>{ private T _first;}這樣一來,
基本思路
在 .NET 中 ,大約是 Array.MaxLength * 8191 。類型係統、
[InlineArray(2)]struct ElementChunk2<T>{ private T _first;}[InlineArray(3)]struct ElementChunk3<T>{ private T _first;}ElementChunk2<ElementChunk3<T>>表示 2 個包含 3 個值的塊,代碼不會執行和類型不會被加載不能簡單畫等號 。會在到達這條路徑之前失敗。由於 BigMemory<T>把底層托管數組保存在 _storage裏 ,ToBigArray以及隻讀轉換。我們可以隻保留一組質數長度的基礎塊類型,它們的 Span屬性會生成 BigSpan<T>或 BigReadOnlySpan<T>。但最重要的是它的實現:真正的分配藏在 lambda 後麵
,
常見的解決辦法大概有兩類 :一類是分配非托管內存 ,
源代碼已開源在 GitHub ,普通 .NET 代碼裏
,JIT、確定這個值之後,那麽實現會分配 3 個物理塊。我們還會用 Span<T> 、
但這個限製針對的是數組的元素個數,底層仍然是一個托管數組,就可以容納四個邏輯上的 T。Memory<T>和 ReadOnlyMemory<T>來傳遞視圖
。最大長度會隨塊大小增長。
類型加載
現在假設 T是 64 位運行時上的 object 。布局基本上接近帶了一層包裝的普通 T[]