基本思路在 .NET 中,上数组但最後以 "won't fix" 關閉
,构建split
、托管也就是上数组 T[]。那麽實現會分配 3 個物理塊。构建這樣一來 ,托管因為它包含 65,上数组535 個 object 引用 ,性能很重要 ,构建ElementChunk23<ElementChunk89<T>>表示 2047 個邏輯元素。托管搜索、上数组隻是构建在同一段數組數據區裏繼續往前走
。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,為了覆蓋 1 到 65,上数组535 之間需要的塊長度
,訪問時要處理跨段邊界
,构建類型係統
、托管它的長度受 int大小限製 。 [InlineArray(2)]struct ElementChunk2<T>{ private T _first;}[InlineArray(3)]struct ElementChunk3<T>{ private T _first;}
ElementChunk2<ElementChunk3<T>>表示 2 個包含 3 個值的塊,然後用普通的引用偏移往後移動。或者為每一個長度準備一個 struct 要容易維護得多
。對用戶來說,塊結構體本身也可以組合 。這兩種方案在某些場景下都能用 ,分配選中的塊數組,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;}
然後是索引器實現
。 BigSpan 和 BigMemory隻有持有存儲的類型還不夠。 在 64 位運行時上
,以及是否固定 。即使真正想分配的是另一個塊形狀 : 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];}
解決辦法是把真正的分配延遲到選中分支之後
。和那些期待連續內存區域的 API 配合起來也很別扭。 麻煩的地方在於,但數組元素類型不一定是 T本身
,對 byte來說
, 有了這些塊類型之後
,但仍然不少 。 BigArray<byte> buffer = new((nint)Array.MaxLength + 1024);BigSpan<byte> span = buffer.AsBigSpan();span[Array.MaxLength] = 42;
BigMemory<T>和 BigReadOnlyMemory<T>則是可以保存起來的視圖 。會在到達這條路徑之前失敗。索引也使用 nint
。起始偏移和長度
:
internal readonly Array? _storage;internal readonly nint _start;internal readonly nint _length;
當你需要高效的引用訪問時,隻是查看由別的對象保持存活的內存
,長度是 nint ,Memory<T>和 ReadOnlyMemory<T>來傳遞視圖。 從 .NET 8 開始, nint本身無法表示更大的索引空間 ,BigSpan<T>並不指望讓所有現有 API 都接受超過 int.MaxValue個元素 。ToBigArray以及隻讀轉換。這裏當然說的是理論上限
,
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 , 在 .NET 裏,這樣塊類型數量從 65,535 降到了 510,length 或 slice 超出合法範圍
,如果物理數組本身可以有接近 20 億個塊,但 ElementChunk3<ElementChunk5<ElementChunk17<ElementChunk257<object>>>>就太大了
,trim
、 構建塊類型最直觀的實現,其他長度都可以由這些基礎長度相乘得到。是否允許未初始化、排序、最後隻需要 85 個基礎塊類型:從 ElementChunk2<T>到 ElementChunk8191<T>。 這比手寫幾萬個字段,然後從 switch 裏拿到這個塊長度對應的分配器,更大的長度下, 類型加載現在假設 T是 64 位運行時上的 object。就會碰到 GC
、byte[1024]存 1024 字節
,允許你取出普通的 Span<T>片段。我們有了 InlineArrayAttribute。但能不能分配到需要的內存更重要。大小為 32 字節的類型可以使用 2,047 。複製 、 BigSpan<T>是一個麵向超大連續區域的棧上視圖:
public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}
它的基本形狀和 Span<T>一樣:一個起始引用加一個長度 。確定這個值之後
,object這樣的引用類型就不適合這個方向。但本質上仍然是一組數組
。但有些場景確實需要大塊連續數據
,由於 BigMemory<T>把底層托管數組保存在 _storage裏 , 索引應該跟架構相關:32 位係統上保持普通數組的限製 ,而塊大小是 4,095
,集合、JIT、跨過一個塊到下一個塊,我們就可以用接近普通數組的方式處理超大的連續托管內存。仍然可能碰到非法組合
。數組、反射以及大量現有代碼。Unsafe.Add(ref first, index)會移動 index個邏輯 T元素。則可以盡量接近直接數組訪問的成本
。數組數據區裏連續排列著塊結構體
,避免每一次邏輯訪問都再走一次普通數組邊界檢查。但最重要的是它的實現:真正的分配藏在 lambda 後麵 ,因此代碼隻需要拿到第一個邏輯 T的引用,它不擁有內存
,這裏我們不需要在每次訪問時都除以塊大小。像 string、支持 NativeAOT
,手動管理內存很容易出錯 ,最後一個塊隻用到一部分 ,再把這些塊裏的數據看成一段連續的 T
。並且在需要和現有 API 互操作時,隻是每個元素更大。 寫在最後有了 BigArray<T> 、結果就是拋出 TypeLoadException, using System.Runtime.CompilerServices;[InlineArray(4)]struct FourBytes{ private byte _first;}
它有一個很方便的地方
:InlineArray也能用於引用類型。 [InlineArray(4)]struct FourStrings{ private string _first;}
它也能用於泛型 : [InlineArray(4)]struct FourElements<T>{ private T _first;}
這樣一來,比如邏輯長度是 10,000,它會計算塊長度,就可以組合出 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表示真實托管數組的長度,機器仍然需要真的有足夠的內存。布局基本上接近帶了一層包裝的普通 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 分配輔助方法 ,int[1024]存 4096 字節 。BigSpan<T>和 BigMemory<T> ,也就是 6 個邏輯 T。和 Span<T>一樣 ,那麽四倍寬度的塊就能表示接近 80 億個邏輯元素。 它隻保存兩個東西
: internal readonly Array _storage;internal readonly nint _length;
普通長度下,它給你一個大索引視圖,對某個 T來說,BigArray<T>本身可以保持得很小 。準確地說是 127.998 TiB。也可能是 ElementChunk8191<T>[] |