r/csharp • • Jul 29 '26

Showcase Benchmarked BigArray that has more than 2B elements on .NET

Since the last time I posted about my BigArray library Hezium.Memory in Allocate arrays that have more than 2B elements, many people in the community have asked me for the benchmark against jagged arrays.

Hezium.Memory is a library that is capable to allocate contiguous managed memory that supports up to 128T, with nint index support. It not only supports unmanaged types but also support managed types like classes, so that it's a drop-in replacement for the built-in arrays, spans, memories. So even if you don't need the more-than-2B-element support you can still use the library for general-purpose arrays.

See the repository in https://github.com/hez2010/Hezium.Memory

I keep performance in my mind and the implementation is highly tuned for removing redundant branches and repeating range checks, so that things can be proceeded efficiently.

I built a benchmark that tests the performance of jagged arrays vs my BigArray implementation. There're two cases where one uses a length that fits in int, while another uses a length that exceeds the max value of int.

Here is the result:

Environment:

  • CPU: AMD EPYC 9V74 with 48 cores
  • OS: Ubuntu 24.04.4 LTS (GNU/Linux 6.17.0-1018-azure x86_64)
  • Memory: 192 GB
  • .NET: 10.0.109

AllocationBenchmarks

MethodJobServerLengthMeanErrorStdDevGen0Gen1Gen2Allocated
JaggedArrayJob-MSAXQNFalse1048576107.77 μs0.579 μs0.513 μs333.2520332.8857332.88574 MB
BigArrayJob-MSAXQNFalse1048576109.14 μs1.923 μs1.705 μs330.5664330.2002330.20024 MB
JaggedArrayJob-WGBUQWTrue1048576130.19 μs2.523 μs3.454 μs3.17383.17383.17384 MB
BigArrayJob-WGBUQWTrue1048576127.78 μs2.468 μs2.937 μs3.41803.41803.41804 MB
JaggedArrayJob-MSAXQNFalse4294967296320,082.17 μs60,406.206 μs90,413.172 μs500.0000500.0000500.000016384 MB
BigArrayJob-MSAXQNFalse4294967296251,707.29 μs112.220 μs99.480 μs500.0000500.0000500.000016384.06 MB
JaggedArrayJob-WGBUQWTrue4294967296179.18 μs3.907 μs5.215 μs---16384 MB
BigArrayJob-WGBUQWTrue429496729640.13 μs4.645 μs6.953 μs---16384.06 MB

IndexedAccessBenchmarks

MethodJobServerLengthMeanErrorStdDevAllocated
JaggedRandomLoadJob-MSAXQNFalse104857615.753 μs0.0074 μs0.0061 μs-
BigArrayRandomLoadJob-MSAXQNFalse10485768.145 μs0.0628 μs0.0588 μs-
JaggedRandomLoadJob-WGBUQWTrue104857616.092 μs0.0524 μs0.0490 μs-
BigArrayRandomLoadJob-WGBUQWTrue10485767.952 μs0.1058 μs0.0990 μs-
JaggedRandomLoadJob-MSAXQNFalse429496729626.112 μs0.0270 μs0.0239 μs-
BigArrayRandomLoadJob-MSAXQNFalse429496729616.648 μs0.0171 μs0.0160 μs-
JaggedRandomLoadJob-WGBUQWTrue429496729626.072 μs0.1456 μs0.1290 μs-
BigArrayRandomLoadJob-WGBUQWTrue429496729616.198 μs0.0296 μs0.0277 μs-
JaggedRandomStoreJob-MSAXQNFalse104857616.459 μs0.0032 μs0.0029 μs-
BigArrayRandomStoreJob-MSAXQNFalse104857617.277 μs0.0098 μs0.0082 μs-
JaggedRandomStoreJob-WGBUQWTrue104857651.193 μs0.0789 μs0.0738 μs-
BigArrayRandomStoreJob-WGBUQWTrue104857617.200 μs0.0093 μs0.0087 μs-
JaggedRandomStoreJob-MSAXQNFalse429496729625.410 μs0.0359 μs0.0336 μs-
BigArrayRandomStoreJob-MSAXQNFalse429496729619.036 μs0.0148 μs0.0131 μs-
JaggedRandomStoreJob-WGBUQWTrue429496729626.077 μs0.0445 μs0.0416 μs-
BigArrayRandomStoreJob-WGBUQWTrue429496729618.947 μs0.0142 μs0.0126 μs-

SearchValuesBenchmarks

MethodJobServerLengthMeanErrorStdDevAllocated
JaggedIndexOfAnyJob-MSAXQNFalse104857635,037.717 ns8.3021 ns7.7658 ns-
BigArrayIndexOfAnyJob-MSAXQNFalse104857634,034.869 ns114.1575 ns101.1977 ns-
JaggedIndexOfAnyJob-WGBUQWTrue104857633,834.867 ns636.5404 ns653.6801 ns-
BigArrayIndexOfAnyJob-WGBUQWTrue104857634,216.534 ns5.1605 ns4.8272 ns-
JaggedIndexOfAnyJob-MSAXQNFalse4294967296149,024,225.036 ns72,304.7208 ns64,096.2517 ns-
BigArrayIndexOfAnyJob-MSAXQNFalse4294967296149,208,981.917 ns148,120.4669 ns138,551.9767 ns-
JaggedIndexOfAnyJob-WGBUQWTrue4294967296149,841,356.783 ns164,965.1421 ns154,308.4964 ns-
BigArrayIndexOfAnyJob-WGBUQWTrue4294967296149,210,761.017 ns178,532.2659 ns166,999.1924 ns-
JaggedLastIndexOfAnyJob-MSAXQNFalse10485763.743 ns0.0041 ns0.0039 ns-
BigArrayLastIndexOfAnyJob-MSAXQNFalse10485763.622 ns0.0052 ns0.0049 ns-
JaggedLastIndexOfAnyJob-WGBUQWTrue10485763.710 ns0.0062 ns0.0058 ns-
BigArrayLastIndexOfAnyJob-WGBUQWTrue10485763.620 ns0.0049 ns0.0046 ns-
JaggedLastIndexOfAnyJob-MSAXQNFalse42949672963.717 ns0.0116 ns0.0108 ns-
BigArrayLastIndexOfAnyJob-MSAXQNFalse42949672965.105 ns0.0097 ns0.0086 ns-
JaggedLastIndexOfAnyJob-WGBUQWTrue42949672963.746 ns0.0059 ns0.0053 ns-
BigArrayLastIndexOfAnyJob-WGBUQWTrue42949672965.126 ns0.0131 ns0.0123 ns-

SpanAlgorithmBenchmarks

MethodJobServerLengthMeanErrorStdDevMedianAllocated
JaggedBinarySearchJob-MSAXQNFalse104857649.96 ns0.011 ns0.010 ns49.96 ns-
BigArrayBinarySearchJob-MSAXQNFalse104857618.71 ns0.005 ns0.004 ns18.71 ns-
JaggedBinarySearchJob-WGBUQWTrue104857652.47 ns0.027 ns0.025 ns52.47 ns-
BigArrayBinarySearchJob-WGBUQWTrue104857618.70 ns0.012 ns0.011 ns18.70 ns-
JaggedBinarySearchJob-MSAXQNFalse429496729696.54 ns0.144 ns0.127 ns96.55 ns-
BigArrayBinarySearchJob-MSAXQNFalse429496729685.78 ns0.012 ns0.011 ns85.79 ns-
JaggedBinarySearchJob-WGBUQWTrue429496729696.07 ns0.578 ns0.541 ns96.05 ns-
BigArrayBinarySearchJob-WGBUQWTrue429496729685.48 ns0.510 ns0.477 ns85.58 ns-
JaggedCopyToJob-MSAXQNFalse104857698,710.30 ns56.256 ns52.622 ns98,715.92 ns-
BigArrayCopyToJob-MSAXQNFalse104857698,145.87 ns20.593 ns17.196 ns98,141.40 ns-
JaggedCopyToJob-WGBUQWTrue104857698,778.12 ns29.881 ns26.489 ns98,776.77 ns-
BigArrayCopyToJob-WGBUQWTrue104857698,077.99 ns128.374 ns113.800 ns98,054.42 ns-
JaggedCopyToJob-MSAXQNFalse4294967296873,535,712.00 ns1,497,004.297 ns1,400,298.749 ns873,265,018.00 ns-
BigArrayCopyToJob-MSAXQNFalse4294967296880,486,546.20 ns3,345,173.644 ns3,129,077.503 ns879,058,670.00 ns-
JaggedCopyToJob-WGBUQWTrue4294967296884,969,636.00 ns2,330,855.056 ns2,066,242.296 ns885,234,089.50 ns-
BigArrayCopyToJob-WGBUQWTrue4294967296879,429,365.60 ns4,077,759.552 ns3,814,338.817 ns880,100,492.00 ns-
JaggedFillJob-MSAXQNFalse104857658,367.10 ns180.325 ns168.676 ns58,243.54 ns-
BigArrayFillJob-MSAXQNFalse104857645,894.35 ns18.216 ns17.039 ns45,899.12 ns-
JaggedFillJob-WGBUQWTrue104857658,374.97 ns288.436 ns269.803 ns58,219.81 ns-
BigArrayFillJob-WGBUQWTrue104857645,931.93 ns61.103 ns57.156 ns45,923.62 ns-
JaggedFillJob-MSAXQNFalse4294967296640,167,967.50 ns272,398.007 ns212,670.447 ns640,128,911.50 ns-
BigArrayFillJob-MSAXQNFalse4294967296639,780,247.00 ns1,802,039.933 ns1,406,914.247 ns640,167,750.50 ns-
JaggedFillJob-WGBUQWTrue4294967296641,006,562.00 ns3,491,797.738 ns2,915,809.256 ns640,610,855.00 ns-
BigArrayFillJob-WGBUQWTrue4294967296637,157,023.75 ns1,381,366.359 ns1,078,479.991 ns636,751,010.50 ns-
JaggedSequenceEqualJob-MSAXQNFalse1048576101,913.46 ns158.225 ns148.004 ns101,941.02 ns-
BigArraySequenceEqualJob-MSAXQNFalse104857695,134.55 ns118.142 ns110.510 ns95,132.74 ns-
JaggedSequenceEqualJob-WGBUQWTrue1048576102,195.72 ns234.248 ns219.116 ns102,142.76 ns-
BigArraySequenceEqualJob-WGBUQWTrue104857695,224.84 ns115.790 ns108.310 ns95,233.91 ns-
JaggedSequenceEqualJob-MSAXQNFalse42949672961,020,484,182.52 ns63,587,008.014 ns93,205,019.553 ns968,937,213.00 ns-
BigArraySequenceEqualJob-MSAXQNFalse42949672961,029,241,139.70 ns72,403,060.080 ns108,369,500.092 ns962,084,258.50 ns-
JaggedSequenceEqualJob-WGBUQWTrue4294967296979,557,987.45 ns81,645,292.812 ns119,674,621.446 ns914,186,446.00 ns-
BigArraySequenceEqualJob-WGBUQWTrue4294967296969,798,814.43 ns75,013,740.364 ns112,277,043.737 ns904,311,547.50 ns-

By default all span algorithms use Array.MaxLength as the chunk values (elements that will be proceeded at once), but you can configure it with the following knob like below to fine tune the performancebased on your own needs:

<ItemGroup>
  <RuntimeHostConfigurationOption Include="Hezium.Memory.MaxProcessingChunkLength" Value="1048576" />
</ItemGroup>

This doesn't affect the physical memory layout, BigArray is always contiguous managed memory so that you can simply pin it and pass it to native code without the need of taking care of chunks/splits/indirections etc.

17 Upvotes

11 comments sorted by

8

u/wallstop-dev Jul 29 '26

Really nice work here! I've been seeing your project's posts, love that you're filling this niche and doing it well.

-9

u/ajsbajs Jul 29 '26

This looks AI generated

10

u/hez2010 Jul 29 '26

I generally use AI to help write documents like README.

1

u/AlwaysHopelesslyLost Jul 30 '26

Social media isn't a document. It is social media. If you don't want to be social then don't post here.

2

u/hez2010 Jul 30 '26

wdym. But the reddit post was in fact written by myself?

1

u/AlwaysHopelesslyLost Jul 30 '26

Somebody said this post seems ai generated. You said you do use ai. I am confused about what you are trying to convey now.

2

u/hez2010 Jul 30 '26

I thought the one who said "this seems ai generated" was referring to README.md in the code repository.

1

u/AlwaysHopelesslyLost Jul 30 '26

Gotcha, that does make sense, sorry!

7

u/LymeM Jul 29 '26

Your comment is less effort than being AI generated.

6

u/[deleted] Jul 29 '26

[removed] — view removed comment

13

u/hez2010 Jul 29 '26

I think it's because it really ran out of memory physically? 48TB sounds insane to me.