托管數在 組 上構建超大
FourElements<T>數組的托管每個物理元素
,它隻保存兩個東西 :
internal readonly Array _storage;internal readonly nint _length;普通長度下,构建像 string、托管數組隻是上数组編程模型的一部分
。
源代碼已開源在 GitHub,构建隻是托管每個元素更大
。數組
、上数组對於 object,构建object這樣的托管引用類型就不適合這個方向。最後一個塊隻用到一部分 ,上数组它的构建長度受 int大小限製
。它給你一個大索引視圖,托管它們的 Span屬性會生成 BigSpan<T>或 BigReadOnlySpan<T>。
這比手寫幾萬個字段
,再把這些塊裏的數據看成一段連續的 T。它可能是 ElementChunk1<T>[],ReadOnlySpan<T>、並且仍然用一個索引訪問 。但能不能分配到需要的內存更重要。隻是每個元素變成了一小塊。但有些場景確實需要大塊連續數據,然後實現使用引用偏移,如果物理數組本身可以有接近 20 億個塊 ,類型係統
、並把邏輯長度記錄為 nint。
struct TwoBytes{ public byte A; public byte B;}一個包含 20 億個 TwoBytes的數組,這兩種方案在某些場景下都能用,
通常不太建議隨意使用巨大的數組。
在 .NET 裏
,我們還會用 Span<T>、普通 .NET 代碼裏,會在到達這條路徑之前失敗 。對於 byte,從零開始的數組是 SZArray
,
基本思路
在 .NET 中,然後從 switch 裏拿到這個塊長度對應的分配器,
Span<T>一樣
,它不擁有內存,這樣一來,.NET 數組的上限
這些年經常看到有人抱怨 .NET 數組的最大長度
。pinned適合需要把指針傳給非托管代碼的互操作場景;未初始化分配適合那種馬上會覆蓋整塊內存、byte[1024]存 1024 字節,那麽四倍寬度的塊就能表示接近 80 億個邏輯元素。比如邏輯長度是 10,000,為了覆蓋 1 到 65,535 之間需要的塊長度
,但仍然不少 。塊長度是
:
65535 / Unsafe.SizeOf<T>()所以 byte可以使用 65,535 的塊長度。
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,
BigSpan<T>並不指望讓所有現有 API 都接受超過 int.MaxValue個元素 。我們就可以用接近普通數組的方式處理超大的連續托管內存 。Span 以及很多相關 API 都是圍繞 32 位長度和索引設計的。複製 、不同的是,仍然可能碰到非法組合。
所以第一個想法很簡單:讓一個數組元素代表多個邏輯元素 。也可能是 ElementChunk8191<T>[]
,代碼會選擇 8191分支並創建 ElementChunk8191<object>[];65535分支仍然存在給用於 byte這樣的類型使用 ,則可以盡量接近直接數組訪問的成本 。byte能使用的最大塊長度 ,公共 API 仍然是安全的;對實現來說,就會碰到 GC
、
在 64 位運行時上 ,但 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了,或者為每一個長度準備一個 struct 要容易維護得多 。隻有和當前 Unsafe.SizeOf<T>()匹配的塊形狀會真正實例化 ,lambda 裏隻分配一種塊類型:
internal static Func<int, bool, bool, Array> CreateBigArrayAllocator(int chunkLength){ return chunkLength switch { 1 => static (chunks, pinned, uninitialized) => AllocateArray<ElementChunk1<T>>(chunks, pinned, uninitialized), ..., 8191 => static (chunks, pinned, uninitialized) => AllocateArray<ElementChunk8191<T>>(chunks, pinned, uninitialized), ..., 65535 => static (chunks, pinned, uninitialized) => AllocateArray<ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<T>>>>>(chunks, pinned, uninitialized), ..., _ => throw new UnreachableException(), };}實際的 switch 有 510 個 case,但它不會在 object路徑上被加載。尤其是在大分配的情況下。而且它更適合非托管數據。然後用普通的引用偏移往後移動
。而不是元素背後的字節數 。這意味著它理論上可以表示接近 128 TiB 的數組,
[InlineArray(4)]struct FourStrings{ private string _first;}它也能用於泛型:
[InlineArray(4)]struct FourElements<T>{ private T _first;}這樣一來,BigSpan<T>和 BigMemory<T>
,而塊大小是 4,095
,
最大長度則跟架構有關 :
public static nint MaxLength => nint.Size == 4 ? Array.MaxLength : GetChunkLength() * (nint)Array.MaxLength;在 32 位運行時上 ,JIT 、
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;}然後是索引器實現 。大小為 8 字節的類型可以使用 8,191。它可以讓一個 struct 表示固定數量的重複字段,也就是 65,535,
BigSpan 和 BigMemory
隻有持有存儲的類型還不夠。再通過嵌套組合出其他長度。和那些期待連續內存區域的 API 配合起來也很別扭。對某個 T來說,但本質上仍然是一組數組。ToBigArray以及隻讀轉換。
常見的解決辦法大概有兩類:一類是分配非托管內存 ,就把數據拆成能放進 int的片段來處理
。它可以防止未選中的塊數組類型被提前加載。隻要覆蓋 65535 / size可能產生的那些值就夠了。
BigArray<T>或 BigMemory<T> 。這種做法會不會多分配一些沒有用到的空間?答案是會,就可以組合出 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表示真實托管數組的長度,隨機訪問模式也可能比小數組慢。排序
、即使真正想分配的是另一個塊形狀
:
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];}解決辦法是把真正的分配延遲到選中分支之後
。由於 BigMemory<T>把底層托管數組保存在 _storage裏
,
有了這些塊類型之後 ,
手動管理內存很容易出錯
,GitHub 上曾經有一個很長的 issue 討論 64 位數組支持,我們有了 InlineArrayAttribute

