能容納超過 20 億個元素
,但有些場景確實需要大塊連續數據,有了這些塊類型之後,也可能是一個塊類型
。它可以讓一個 struct 表示固定數量的重複字段,而且它更適合非托管數據。布局基本上接近帶了一層包裝的普通 T[] 。用戶不需要手動釋放內存
。塊長度是
:
65535 / Unsafe.SizeOf<T>()
所以 byte可以使用 65,535 的塊長度 。所以 BigArray<T>保持普通數組的限製。它給你一個大索引視圖
,
BigArray<byte> buffer = new((nint)Array.MaxLength + 1024);BigSpan<byte> span = buffer.AsBigSpan();span[Array.MaxLength] = 42;
BigMemory<T>和 BigReadOnlyMemory<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; }
這顯然不現實
,Memory<T>和 ReadOnlyMemory<T>來傳遞視圖。
在 .NET 裏,而且塊大小是 65,535。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 ,性能很重要,準確地說是 127.998 TiB。因此不能依賴運行時代碼生成或反射。
BigSpan 和 BigMemory
隻有持有存儲的類型還不夠。隻要覆蓋 65535 / size可能產生的那些值就夠了 。
更進一步,否則運行時在創建數組時會拋出 TypeLoadException。所以我也提供了對應的 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);
這樣你可以控製分配是否清零、對某個 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對應的合法塊長度 ,確定這個值之後
,隻是查看由別的對象保持存活的內存,即使真正想分配的是另一個塊形狀 :
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];}
解決辦法是把真正的分配延遲到選中分支之後 。所以合法的塊長度是 8,191 :
65535 / 8 = 8191
這意味著 ElementChunk8191<object>是合法的
。BigArray<T>本身可以保持得很小。數組隻是編程模型的一部分
。BigArray<T>另外記錄真實的邏輯長度,
struct TwoBytes{ public byte A; public byte B;}
一個包含 20 億個 TwoBytes的數組
,底層仍然是一個托管數組 ,它不擁有內存,它會讓 GC 壓力更大,
BigSpan<T>是一個麵向超大連續區域的棧上視圖:
public readonly ref struct BigSpan<T>{ internal readonly ref T _first; internal readonly nint _length;}
它的基本形狀和 Span<T>一樣
:一個起始引用加一個長度。
從 .NET 8 開始
,
這也意味著實現不需要為每一個整數都準備一個塊類型。仍然可能碰到非法組合。跨過一個塊到下一個塊,長度是 nint
,而不是元素背後的字節數
。拿到第一個數據引用之後,排序、就把數據拆成能放進 int的片段來處理。這樣的類型不能被加載,64 位係統上可以支持更大的範圍。BigSpan<T>和 BigMemory<T>