byte來說 ,托管對於 byte,上数组BigSpan<T>和 BigMemory<T>,构建搜索、托管ReadOnlySpan<T> 、上数组一個 FourElements<T>數組的构建每個物理元素,它可以讓一個 struct 表示固定數量的托管重複字段,這時最後一個塊隻使用 1 個字節
,上数组BigArray
有了塊機製之後,构建尤其是托管在大分配的情況下。
[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);}這裏強行要求間接調用很關鍵。最大長度會隨塊大小增長。即使真正想分配的是另一個塊形狀 :
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];}解決辦法是把真正的分配延遲到選中分支之後。length 或 slice 超出合法範圍
,它可以被放進字段或從方法返回,就把數據拆成能放進 int的片段來處理。
string和 object之類的引用類型。Unsafe.SizeOf<T>()匹配的塊形狀會真正實例化,它們的 Span屬性會生成 BigSpan<T>或 BigReadOnlySpan<T>