ToArray、上数组是构建否允許未初始化、然後實現使用引用偏移
,托管同時仍然讓這段存儲對 GC 可見。上数组訪問時要處理跨段邊界
,构建[InlineArray(2)]struct ElementChunk2<T>{ private T _first;}[InlineArray(3)]struct ElementChunk3<T>{ private T _first;}ElementChunk2<ElementChunk3<T>>表示 2 個包含 3 個值的托管塊,
struct TwoBytes{ public byte A; public byte B;}一個包含 20 億個 TwoBytes的上数组數組 ,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 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 分配輔助方法 ,
上数组或者為每一個長度準備一個 struct 要容易維護得多。构建會在到達這條路徑之前失敗。托管最後隻需要 85 個基礎塊類型:從ElementChunk2<T>到 ElementChunk8191<T>。上数组準確地說是构建 127.998 TiB 。Unsafe.Add(ref first,托管 index)會移動 index個邏輯 T元素。通常是 BigArray<T>或 BigMemory<T>。反射和基礎類庫等很多地方。我們還會用 Span<T>、但最後以 "won't fix" 關閉,再把這些塊裏的數據看成一段連續的 T
。因為它包含 65,535 個 object 引用,[InlineArray(4)]struct FourStrings{ private string _first;}它也能用於泛型 :
[InlineArray(4)]struct FourElements<T>{ private T _first;}這樣一來,length 或 slice 超出合法範圍 ,集合、
手動管理內存很容易出錯,
這種做法會不會多分配一些沒有用到的空間?答案是會,
BigArray
有了塊機製之後,最後一個塊隻用到一部分
,隻是在同一段數組數據區裏繼續往前走。但數組元素類型不一定是 T本身,但本質上仍然是一組數組。所以 BigArray<T>保持普通數組的限製
。BigSpan<T>和 BigMemory<T>
,BigArray<T>不需要像交錯數組包裝器那樣在每次訪問時都做除法和取餘;它隻是把一個托管數組對象視作一段更大的邏輯序列。但能不能分配到需要的內存更重要。
源代碼已開源在 GitHub,數組數據區裏連續排列著塊結構體,pinned適合需要把指針傳給非托管代碼的互操作場景;未初始化分配適合那種馬上會覆蓋整塊內存、它會讓 GC 壓力更大
,
更進一步
,int[1024]存 4096 字節。由於 BigMemory<T>把底層托管數組保存在 _storage裏
,最大長度會隨塊大小增長。如果物理數組本身可以有接近 20 億個塊,我們可以隻保留一組質數長度的基礎塊類型
,分配選中的塊數組,然後從 switch 裏拿到這個塊長度對應的分配器,
這比手寫幾萬個字段 ,機器仍然需要真的有足夠的內存。為了覆蓋 1 到 65,535 之間需要的塊長度,反射以及大量現有代碼。但最重要的是它的實現:真正的分配藏在 lambda 後麵 ,它們記錄底層托管數組
、否則運行時在創建數組時會拋出 TypeLoadException。64 位係統上可以支持更大的範圍。實現內部如果需要調用隻接受 Span<T>或 ReadOnlySpan<T>的 BCL API,更大的長度下,
這就是 BigArray<T>的核心思路。再用一個類包起來;另一類是用交錯數組模擬一個更大的數組。它們的 Span屬性會生成 BigSpan<T>或 BigReadOnlySpan<T>。
常見的解決辦法大概有兩類
:一類是分配非托管內存
,就可以容納四個邏輯上的 T
。並且仍然用一個索引訪問。其他長度都可以由這些基礎長度相乘得到。那麽實現會分配 3 個物理塊。拿到第一個數據引用之後,如果隻是想使用的話可以從 NuGet 引用包來使用。對 byte來說
,就把數據拆成能放進 int的片段來處理 。尤其是在大分配的情況下
。大小為 8 字節的類型可以使用 8,191。
數據引用是通過把數組數據開頭重新解釋為 T得到的:
private static ref T GetDataReference(Array storage){ return ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(storage));}這就是為什麽連續存儲這個特性很重要
。它可以讓一個 struct 表示固定數量的重複字段,Memory<T>和 ReadOnlyMemory<T>來傳遞視圖
。類型係統
、如果 index、
麻煩的地方在於,確定這個值之後,而元素又內聯保存在這些塊裏 ,
這也是為什麽 _storage的類型是 Array
:實際運行時類型取決於 T。不需要清零的性能敏感場景,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表示真實托管數組的長度
,
類型加載
現在假設 T是 64 位運行時上的 object
。後麵的優化也談不上。對於 byte
,
於是我決定自己做一個方案 :
- 能容納超過 20 億個元素,因為 JIT 隻會編譯實際創建出來的 lambda 背後的方法。
BigMemory<byte> page = buffer.AsBigMemory(1024, 4096);page.Span.Fill(0);API 的設計則盡量沿用了普通 Span/Memory 的習慣:切片、你需要管理每個內部數組的大小 ,性能很重要,
nint本身無法表示更大的索引空間 ,也可能是ElementChunk8191<T>[],而不用把每個字段都手寫出來。因為這件事會牽涉到運行時、想要直接放寬這個限製,構建塊類型
最直觀的實現 ,
分配器來自一個針對塊長度的 switch。隨機訪問模式也可能比小數組慢。
寫在最後
有了
BigArray<T>