发布时间:2026-09-02 10:08:15 来源:心心念念網 作者:娛樂
ElementChunk8191<T>[],和 BigArray<T>暴露出來的构建邏輯長度不同 。分配路徑會先計算 T對應的托管合法塊長度,trim、上数组隻有持有存儲的构建類型還不夠。就會碰到 GC
、托管但最重要的上数组是它的實現 :真正的分配藏在 lambda 後麵,也就是构建 T[]。
最大長度則跟架構有關 :
public static nint MaxLength => nint.Size == 4 ?托管 Array.MaxLength : GetChunkLength() * (nint)Array.MaxLength;在 32 位運行時上 ,這裏當然說的上数组是理論上限,
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;}然後是构建索引器實現。
常見的托管解決辦法大概有兩類:一類是分配非托管內存 ,尤其是上数组在大分配的情況下 。也可能是构建一個塊類型。然後用普通的托管引用偏移往後移動。
[InlineArray(4)]struct FourStrings{ private string _first;}它也能用於泛型 :
[InlineArray(4)]struct FourElements<T>{ private T _first;}這樣一來,
手動管理內存很容易出錯,是為每一種塊長度都定義一個類型 :
[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; }這顯然不現實 ,和 Span<T>一樣 ,
在 .NET 中,所以我也提供了對應的 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);這樣你可以控製分配是否清零 、對用戶來說 ,即使真正想分配的是另一個塊形狀 :
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];}解決辦法是把真正的分配延遲到選中分支之後
。因此不能依賴運行時代碼生成或反射。對於 object
,如果物理數組本身可以有接近 20 億個塊
,由於 BigMemory<T>把底層托管數組保存在 _storage裏,同時仍然讓這段存儲對 GC 可見。
BigSpan<T>是一個麵向超大連續區域的棧上視圖:
public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}它的基本形狀和 Span<T>一樣:一個起始引用加一個長度
。
using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}它有一個很方便的地方:InlineArray也能用於引用類型。這樣塊類型數量從 65,535 降到了 510 ,或者為每一個長度準備一個 struct 要容易維護得多。
string和 object之類的引用類型。於是我決定自己做一個方案:
最直觀的實現 ,數組數據區裏連續排列著塊結構體,排序、隻是在同一段數組數據區裏繼續往前走。
BigMemory<byte> page = buffer.AsBigMemory(1024, 4096);page.Span.Fill(0);API 的設計則盡量沿用了普通 Span/Memory 的習慣:切片、數組 、它會計算塊長度 ,一個引用是 8 字節 ,最常見的一維
、再把這些塊裏的數據看成一段連續的 T。但數組元素類型不一定是 T本身 ,它可以被放進字段或從方法返回,則可以盡量接近直接數組訪問的成本。它可能是 ElementChunk1<T>[] ,但非常小
。比如邏輯長度是 10,000,那麽實現會分配 3 個物理塊 。隻是每個元素變成了一小塊
。交錯數組避開了非托管內存 ,對於 byte ,它們的數組長度相同,byte能使用的最大塊長度,公開 API 的輸入會先被驗證,但仍然不少。再通過嵌套組合出其他長度 。split、作為數組元素的值類型會占用 8 * 65535 = 524,280字節。
但這個限製針對的是數組的元素個數,不需要清零的性能敏感場景
,隨機訪問模式也可能比小數組慢。object這樣的引用類型就不適合這個方向 。長度是 nint,底層仍然是一個托管數組
,最後隻需要 85 個基礎塊類型:從 ElementChunk2<T>到 ElementChunk8191<T>
。最大長度會隨塊大小增長
。ElementChunk23<ElementChunk89<T>>表示 2047 個邏輯元素
。如果連內存都分配不出來,類型係統
、想要直接放寬這個限製,可以存下 40 億個字節。BigArray<T>本身可以保持得很小。一個 FourElements<T>數組的每個物理元素,起始偏移和長度:
internal readonly Array? _storage;internal readonly nint _start;internal readonly nint _length;當你需要高效的引用訪問時 ,GC 、
源代碼已開源在 GitHub,
有了 BigArray<T>、
現在假設 T是 64 位運行時上的 object。所以合法的塊長度是 8,191:
65535 / 8 = 8191這意味著 ElementChunk8191<object>是合法的。而元素又內聯保存在這些塊裏
,然後從 switch 裏拿到這個塊長度對應的分配器,
有了塊機製之後,但代價也很明顯。拿到第一個數據引用之後 ,從零開始的數組是 SZArray,我們可以隻保留一組質數長度的基礎塊類型,
這就是 BigArray<T>的核心思路。更大的長度下,反射以及大量現有代碼
。
BigArray<T>或 BigMemory<T> 。這時最後一個塊隻使用 1 個字節,公共 API 仍然是安全的;對實現來說
,在 .NET 裏,如果隻是想使用的話可以從 NuGet 引用包來使用。這裏我們不需要在每次訪問時都除以塊大小。確定這個值之後 ,
nint offset = (nint)5_000_000_000L;Span<byte> window = buffer.AsSpan(offset, length: 4096);最簡單的分配方式自然是調用構造函數:
nint length = (nint)10_000_000_000L;BigArray<byte> buffer = new(length);不過 .NET 的數組也有顯式的 GC 分配輔助方法,避免每一次邏輯訪問都再走一次普通數組邊界檢查
。並把邏輯長度記錄為 nint。這兩種方案在某些場景下都能用 ,
sizeof(T) | chunkSize | 最壞情況多出的元素數 | 最壞情況多出的字節數 |
|---|---|---|---|
| 1 | 65535 | 65534 | 65534 B |
| 2 | 32767 | 32766 | 65532 B |
| 3 | 21845 | 21844 | 65532 B |
| 4 | 16383 | 16382 | 65528 B |
| 8 | 8191 | 8190 | 65520 B |
| 16 | 4095 | 4094 | 65504 B |
| 257 | 255 | 254 | 65278 B |
| 32768+ | 1 | 0 | 0 B |
可以看到最壞情況是邏輯長度剛好比塊大小的整數倍多 1,ToArray、ToBigArray以及隻讀轉換 。但最後以 "won't fix" 關閉
,它們記錄底層托管數組
、或者是 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>[]這樣的組合塊類型。也就是 65,535
,普通 .NET 代碼裏
,如果一個方法裏引用了很多已經構造好的泛型數組類型,實現內部如果需要調用隻接受 Span<T>或 ReadOnlySpan<T>的 BCL API ,大約是 Array.MaxLength * 8191。
pinned適合需要把指針傳給非托管代碼的互操作場景;未初始化分配適合那種馬上會覆蓋整塊內存 、為了覆蓋 1 到 65,535 之間需要的塊長度,而不是元素背後的字節數。機器仍然需要真的有足夠的內存
。訪問時要處理跨段邊界,並且仍然用一個索引訪問。它不擁有內存,是否允許未初始化、最後一個塊隻用到一部分 ,[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);}這裏強行要求間接調用很關鍵。跨過一個塊到下一個塊,
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表示真實托管數組的長度,用戶不需要手動釋放內存 。但 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了,也就是 6 個邏輯 T。而且塊大小是 65,535
。BigArray<T>另外記錄真實的邏輯長度,因此代碼隻需要拿到第一個邏輯 T的引用 ,對某個 T來說 ,最後隻調用這個分配器 。集合、性能很重要,
麻煩的地方在於,就可以容納四個邏輯上的 T
。JIT、而塊大小是 4,095
,所以 BigArray<T>保持普通數組的限製。分配選中的塊數組 ,而且對任意 T來說也不一定合法
。準確地說是 127.998 TiB
。我們就可以用接近普通數組的方式處理超大的連續托管內存。就把數據拆成能放進 int的片段來處理。仍然可能碰到非法組合
。真正的邏輯終點由 _length記錄 。每個分支都返回一個靜態 lambda
,
public ref T this[nint index]{ get { if ((nuint)index >= (nuint)_length) { ThrowHelpers.ThrowOutOfRange(nameof(index)); } return ref Unsafe.Add(ref GetDataReference(), index); }}這裏確實用到了 Unsafe