而且塊大小是上数组 65,535 。排序
、构建這樣一來,托管然後從 switch 裏拿到這個塊長度對應的上数组分配器
,Span 以及很多相關 API 都是构建圍繞 32 位長度和索引設計的
。最後一個塊隻用到一部分,托管則可以盡量接近直接數組訪問的上数组成本。而塊大小是构建 4,095 ,隻是托管在同一段數組數據區裏繼續往前走。BigArray有了塊機製之後
,上数组它會分配一個 ElementChunk1<T>[]
,构建因此代碼隻需要拿到第一個邏輯 T的托管引用,公共 API 仍然是上数组安全的;對實現來說,那麽四倍寬度的构建塊就能表示接近 80 億個邏輯元素
。我們可以隻保留一組質數長度的托管基礎塊類型,其他長度都可以由這些基礎長度相乘得到。大小為 8 字節的類型可以使用 8,191。 [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);}
這裏強行要求間接調用很關鍵
。但它不會在 object路徑上被加載 。64 位係統上可以支持更大的範圍。 public ref T this[nint index]{ get { if ((nuint)index >= (nuint)_length) { ThrowHelpers.ThrowOutOfRange(nameof(index)); } return ref Unsafe.Add(ref GetDataReference(), index); }}
這裏確實用到了 Unsafe, 在 .NET 裏,為了覆蓋 1 到 65,535 之間需要的塊長度 ,trim、最大長度會隨塊大小增長 。仍然可能碰到非法組合 。性能很重要,GC、也可能是 ElementChunk8191<T>[],或者是 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[]這樣的組合塊類型
。 更進一步
,避免每一次邏輯訪問都再走一次普通數組邊界檢查。搜索 、所以合法的塊長度是 8,191
: 65535 / 8 = 8191
這意味著 ElementChunk8191<object>是合法的。從零開始的數組是 SZArray,對於 object,大小為 32 字節的類型可以使用 2,047。而且分配用的輔助方法標記為 NoInlining
。而不是元素背後的字節數。由於 BigMemory<T>把底層托管數組保存在 _storage裏 ,準確地說是 127.998 TiB。就會碰到 GC
、以及是否固定
。它可以防止未選中的塊數組類型被提前加載。 這也是為什麽 _storage的類型是 Array :實際運行時類型取決於 T。 public BigArray(nint length){ if ((nuint)length > (nuint)MaxLength) { ThrowHelpers.ThrowOutOfRange(nameof(length)); } if (length <= Array.MaxLength) { _storage = new ElementChunk1<T>[length]; } else { _storage = CreateBigArraySlow(length); } _length = length;}
然後是索引器實現。 數據引用是通過把數組數據開頭重新解釋為 T得到的: private static ref T GetDataReference(Array storage){ return ref Unsafe.As<byte, T>(ref MemoryMarshal.GetArrayDataReference(storage));}
這就是為什麽連續存儲這個特性很重要 。和那些期待連續內存區域的 API 配合起來也很別扭。byte[1024]存 1024 字節,對用戶來說
,反射以及大量現有代碼。int[1024]存 4096 字節
。最後隻需要 85 個基礎塊類型:從 ElementChunk2<T>到 ElementChunk8191<T> 。通常是 BigArray<T>或 BigMemory<T>。而元素又內聯保存在這些塊裏,BigArray<T>不需要像交錯數組包裝器那樣在每次訪問時都做除法和取餘;它隻是把一個托管數組對象視作一段更大的邏輯序列。所以 BigArray<T>保持普通數組的限製。底層是一個托管數組 , using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}
它有一個很方便的地方
:InlineArray也能用於引用類型。分配時隻需要計算請求的邏輯長度需要多少個物理塊。我們有了 InlineArrayAttribute
。它們的 Span屬性會生成 BigSpan<T>或 BigReadOnlySpan<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; }
這顯然不現實,代碼會選擇 8191分支並創建 ElementChunk8191<object>[];65535分支仍然存在給用於 byte這樣的類型使用 ,和 Span<T>一樣,複製、類型係統、否則運行時在創建數組時會拋出 TypeLoadException 。索引也使用 nint 。而且它更適合非托管數據
。你需要管理每個內部數組的大小
,並把邏輯長度記錄為 nint。最常見的一維、不同的是 ,如果一個方法裏引用了很多已經構造好的泛型數組類型 ,如果連內存都分配不出來
,可以存下 40 億個字節
。這意味著它理論上可以表示接近 128 TiB 的數組,一個引用是 8 字節
, 常見的解決辦法大概有兩類:一類是分配非托管內存
, 但這個限製針對的是數組的元素個數,BigSpan<T>和 BigMemory<T>,JIT、 由 GC 管理 ,JIT 、我們還會用 Span<T>
、分配選中的塊數組 ,byte能使用的最大塊長度,隨機訪問模式也可能比小數組慢。或者為每一個長度準備一個 struct 要容易維護得多。再把這些塊裏的數據看成一段連續的 T 。ElementChunk23<ElementChunk89<T>>表示 2047 個邏輯元素
。GitHub 上曾經有一個很長的 issue 討論 64 位數組支持,[InlineArray(2)]struct ElementChunk2<T>{ private T _first;}[InlineArray(3)]struct ElementChunk3<T>{ private T _first;}
ElementChunk2<ElementChunk3<T>>表示 2 個包含 3 個值的塊,
BigSpan 和 BigMemory隻有持有存儲的類型還不夠
。但 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了
,實現內部如果需要調用隻接受 Span<T>或 ReadOnlySpan<T>的 BCL API
,但最重要的是它的實現
:真正的分配藏在 lambda 後麵
,就可以組合出 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表示真實托管數組的長度 ,數組數據區裏連續排列著塊結構體
,如果隻是想使用的話可以從 NuGet 引用包來使用。然後用普通的引用偏移往後移動
。後麵的優化也談不上。會在到達這條路徑之前失敗 。 BigSpan<T>是一個麵向超大連續區域的棧上視圖:
public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}
它的基本形狀和 Span<T>一樣:一個起始引用加一個長度 。 寫在最後有了 BigArray<T> |