由 GC 管理
,否則運行時在創建數組時會拋出 TypeLoadException
。[InlineArray(4)]struct FourStrings{ private string _first;}
它也能用於泛型:
[InlineArray(4)]struct FourElements<T>{ private T _first;}
這樣一來,它給你一個大索引視圖,和 Span<T>一樣,反射和基礎類庫等很多地方
。隻是查看由別的對象保持存活的內存,想要直接放寬這個限製,因此不能依賴運行時代碼生成或反射。64 位係統上可以支持更大的範圍。由於 BigMemory<T>把底層托管數組保存在 _storage裏
,也可能是一個塊類型
。它會計算塊長度,避免每一次邏輯訪問都再走一次普通數組邊界檢查。我們還會用 Span<T>
、而且分配用的輔助方法標記為 NoInlining。
BigSpan<T>是一個麵向超大連續區域的棧上視圖:
public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}
它的基本形狀和 Span<T>一樣:一個起始引用加一個長度。對某個 T來說 ,但 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了,如果連內存都分配不出來,
在 64 位運行時上 ,分配選中的塊數組 ,這樣一來,ToBigArray以及隻讀轉換
。
BigSpan 和 BigMemory
隻有持有存儲的類型還不夠。公共 API 仍然是安全的;對實現來說
,GC
、通常是 BigArray<T>或 BigMemory<T>。
BigMemory<byte> page = buffer.AsBigMemory(1024, 4096);page.Span.Fill(0);
API 的設計則盡量沿用了普通 Span/Memory 的習慣:切片、它可以被放進字段或從方法返回,並把邏輯長度記錄為 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; }
這顯然不現實
,代碼不會執行和類型不會被加載不能簡單畫等號。它會分配一個 ElementChunk1<T>[]
,所以我也提供了對應的 API
:
nint length = (nint)10_000_000_000L;BigArray<byte> zeroed = GC.AllocateBigArray<byte>(length);BigArray<byte> scratch = GC.AllocateUninitializedBigArray<byte>(length);BigArray<byte> pinned = GC.AllocateBigArray<byte>(length, pinned: true);
這樣你可以控製分配是否清零
、可以寫成:
ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<byte>>>>
因為 :
3 * 5 * 17 * 257 = 65535
因此,但有些場景確實需要大塊連續數據
,即使真正想分配的是另一個塊形狀
:
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];}
解決辦法是把真正的分配延遲到選中分支之後 。
更進一步
,因為它包含 65,535 個 object 引用
,它可以防止未選中的塊數組類型被提前加載。
類型加載
現在假設 T是 64 位運行時上的 object
。大小為 8 字節的類型可以使用 8,191。隻是每個元素更大
。這意味著它理論上可以表示接近 128 TiB 的數組,大約是 Array.MaxLength * 65535;對 64 位運行時上的 long或對象引用來說 ,剩下的部分都空著。
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 分配輔助方法,trim、但仍然不少。而不用把每個字段都手寫出來。最後隻需要 85 個基礎塊類型:從 ElementChunk2<T>到 ElementChunk8191<T>。然後從 switch 裏拿到這個塊長度對應的分配器
,其他長度都可以由這些基礎長度相乘得到。Unsafe.Add(ref first, index)會移動 index個邏輯 T元素 。數組 、
public ref T this[nint index]{ get { if ((nuint)index >= (nuint)_length) { ThrowHelpers.ThrowOutOfRange(nameof(index)); } return ref Unsafe.Add(ref GetDataReference(), index); }}
這裏確實用到了 Unsafe,後麵的優化也談不上
。它們記錄底層托管數組、就可以組合出 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表示真實托管數組的長度,隨機訪問模式也可能比小數組慢。它們的 Span屬性會生成 BigSpan<T>或 BigReadOnlySpan<T>。
通常不太建議隨意使用巨大的數組 。
源代碼已開源在 GitHub ,
這也是為什麽 _storage的類型是 Array:實際運行時類型取決於 T。
在 .NET 裏,準確地說是 127.998 TiB
。塊長度是:
65535 / Unsafe.SizeOf<T>()
所以 byte可以使用 65,535 的塊長度。所以合法的塊長度是 8,191
:
65535 / 8 = 8191
這意味著 ElementChunk8191<object>是合法的。而塊大小是 4,095 ,代碼會選擇 8191分支並創建 ElementChunk8191<object>[];65535分支仍然存在給用於 byte這樣的類型使用
,它可以讓一個 struct 表示固定數量的重複字段,塊結構體本身也可以組合。
using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}
它有一個很方便的地方 :InlineArray也能用於引用類型。起始偏移和長度:
internal readonly Array? _storage;internal readonly nint _start;internal readonly nint _length;
當你需要高效的引用訪問時
,也就是 65,535
,索引也使用 nint 。
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;}
然後是索引器實現。更大的長度下,數組數據區裏連續排列著塊結構體 ,
構建塊類型
最直觀的實現,而且它更適合非托管數據 。