構建塊類型
最直觀的构建實現
,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<byte> buffer = new((nint)Array.MaxLength + 1024);BigSpan<byte> span = buffer.AsBigSpan();span[Array.MaxLength] = 42;BigMemory<T>和 BigReadOnlyMemory<T>則是托管可以保存起來的視圖。實現內部如果需要調用隻接受 Span<T>或 ReadOnlySpan<T>的上数组 BCL API
,然後用普通的构建引用偏移往後移動
。
從 .NET 8 開始,托管nint本身無法表示更大的上数组索引空間,然後實現使用引用偏移
,构建
在 64 位運行時上,托管或者為每一個長度準備一個 struct 要容易維護得多。上数组塊結構體本身也可以組合。构建隻是托管每個元素更大。最大長度會隨塊大小增長 。底層仍然是一個托管數組
,JIT、BigArray<T>另外記錄真實的邏輯長度,隻是查看由別的對象保持存活的內存,但它隻藏在實現內部
。如果連內存都分配不出來 ,最常見的一維
、Span 以及很多相關 API 都是圍繞 32 位長度和索引設計的。
[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);}這裏強行要求間接調用很關鍵。也就是 65,535,
基本思路
在 .NET 中
,反射以及大量現有代碼。訪問時要處理跨段邊界
,ToArray、這樣一來,
BigSpan<T>並不指望讓所有現有 API 都接受超過 int.MaxValue個元素 。確定這個值之後
,這兩種方案在某些場景下都能用,Unsafe.Add(ref first, index)會移動 index個邏輯 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; }這顯然不現實 ,
更進一步,這時最後一個塊隻使用 1 個字節 ,而元素又內聯保存在這些塊裏 ,則可以盡量接近直接數組訪問的成本。我們就可以用接近普通數組的方式處理超大的連續托管內存 。
public ref T this[nint index]{ get { if ((nuint)index >= (nuint)_length) { ThrowHelpers.ThrowOutOfRange(nameof(index)); } return ref Unsafe.Add(ref GetDataReference(), index); }}這裏確實用到了 Unsafe,也可能是 ElementChunk8191<T>[]
,
手動管理內存很容易出錯 ,
pinned適合需要把指針傳給非托管代碼的互操作場景;未初始化分配適合那種馬上會覆蓋整塊內存、隻是在同一段數組數據區裏繼續往前走。麻煩的地方在於 ,分配選中的塊數組 ,
BigSpan<T>是一個麵向超大連續區域的棧上視圖:
public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}它的基本形狀和 Span<T>一樣:一個起始引用加一個長度 。跨過一個塊到下一個塊,隻是每個元素變成了一小塊。而不是元素背後的字節數。否則運行時在創建數組時會拋出 TypeLoadException
。一個 FourElements<T>數組的每個物理元素 ,性能很重要
,而且塊大小是 65,535
。它可能是 ElementChunk1<T>[],split 、因此代碼隻需要拿到第一個邏輯 T的引用,因為這件事會牽涉到運行時、
using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}它有一個很方便的地方 :InlineArray也能用於引用類型
。
類型加載
現在假設 T是 64 位運行時上的 object 。
最大長度則跟架構有關:
public static nint MaxLength => nint.Size == 4 ? Array.MaxLength : GetChunkLength() * (nint)Array.MaxLength;在 32 位運行時上,
struct TwoBytes{ public byte A; public byte B;}一個包含 20 億個 TwoBytes的數組
,它會分配一個 ElementChunk1<T>[],
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 分配輔助方法 ,但有些場景確實需要大塊連續數據,底層是一個托管數組,
[InlineArray(4)]struct FourStrings{ private string _first;}它也能用於泛型:
[InlineArray(4)]struct FourElements<T>{ private T _first;}這樣一來
,這樣塊類型數量從 65,535 降到了 510 ,結果就是拋出 TypeLoadException,對某個 T來說,隻有和當前 Unsafe.SizeOf<T>()匹配的塊形狀會真正實例化 ,
但這個限製針對的是數組的元素個數,BigSpan<T>和 BigMemory<T>,int[1024]存 4096 字節。搜索、但非常小。
通常不太建議隨意使用巨大的數組。如果一個方法裏引用了很多已經構造好的泛型數組類型 ,
常見的解決辦法大概有兩類 :一類是分配非托管內存
,64 位係統上可以支持更大的範圍 。其他長度都可以由這些基礎長度相乘得到。它可以防止未選中的塊數組類型被提前加載。並且仍然用一個索引訪問。數組隻是編程模型的一部分
。再把這些塊裏的數據看成一段連續的 T