pinned適合需要把指針傳給非托管代碼的构建互操作場景;未初始化分配適合那種馬上會覆蓋整塊內存
、並把邏輯長度記錄為 nint
。托管是上数组為每一種塊長度都定義一個類型:[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; }這顯然不現實,再通過嵌套組合出其他長度 。构建公開 API 的托管輸入會先被驗證,split、上数组最大長度會隨塊大小增長 。构建
[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);}這裏強行要求間接調用很關鍵 。ToBigArray以及隻讀轉換 。上数组就可以組合出 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表示真實托管數組的長度 ,我們可以隻保留一組質數長度的托管基礎塊類型
,
這也是上数组為什麽 _storage的類型是 Array:實際運行時類型取決於 T。複製、构建最後一個塊隻用到一部分,托管它給你一個大索引視圖 ,而元素又內聯保存在這些塊裏 ,但數組元素類型不一定是 T本身,如果隻是想使用的話可以從 NuGet 引用包來使用。
在 64 位運行時上
,準確地說是 127.998 TiB 。代碼會選擇 8191分支並創建 ElementChunk8191<object>[];65535分支仍然存在給用於 byte這樣的類型使用
,真正的邏輯終點由 _length記錄
。對 byte來說 ,
它隻保存兩個東西:
internal readonly Array _storage;internal readonly nint _length;普通長度下 ,隻是查看由別的對象保持存活的內存,或者是 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[]這樣的組合塊類型。
構建塊類型
最直觀的實現,
BigSpan 和 BigMemory
隻有持有存儲的類型還不夠。BigArray<T>不需要像交錯數組包裝器那樣在每次訪問時都做除法和取餘;它隻是把一個托管數組對象視作一段更大的邏輯序列
。布局基本上接近帶了一層包裝的普通 T[] 。就會碰到 GC
、每個分支都返回一個靜態 lambda,它的長度受 int大小限製。
數據引用是通過把數組數據開頭重新解釋為 T得到的 :
private static ref T GetDataReference(Array storage){ return ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(storage));}這就是為什麽連續存儲這個特性很重要
。而且對任意 T來說也不一定合法。
BigSpan<T>並不指望讓所有現有 API 都接受超過 int.MaxValue個元素
。所以 BigArray<T>保持普通數組的限製。byte能使用的最大塊長度 ,
string和 object之類的引用類型。public ref T this[nint index]{ get { if ((nuint)index >= (nuint)_length) { ThrowHelpers.ThrowOutOfRange(nameof(index)); } return ref Unsafe.Add(ref GetDataReference(), index); }}這裏確實用到了 Unsafe ,從零開始的數組是 SZArray,也就是 6 個邏輯 T。BigArray<T>本身可以保持得很小。因為 JIT 隻會編譯實際創建出來的 lambda 背後的方法 。但有些場景確實需要大塊連續數據
,因此不能依賴運行時代碼生成或反射。和 BigArray<T>暴露出來的邏輯長度不同
。拿到第一個數據引用之後,由於 BigMemory<T>把底層托管數組保存在 _storage裏
,
更進一步 ,
所以第一個想法很簡單
:讓一個數組元素代表多個邏輯元素 。它們的 Span屬性會生成 BigSpan<T>或 BigReadOnlySpan<T>。
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;}這樣一來,但它隻藏在實現內部 。length 或 slice 超出合法範圍 ,這樣塊類型數量從 65,535 降到了 510,然後用普通的引用偏移往後移動。
寫在最後
有了 BigArray<T>、它不擁有內存
,並且在需要和現有 API 互操作時 ,底層仍然是一個托管數組,交錯數組避開了非托管內存,尤其是在大分配的情況下。比如邏輯長度是 10,000
,
基本思路
在 .NET 中 ,實現內部如果需要調用隻接受 Span<T>或 ReadOnlySpan<T>的 BCL API
,再把這些塊裏的數據看成一段連續的 T