Monthly Archives: November 2017

.NET Bio is Significantly Faster on .Net Core 2.0

Summary: With the release of .NET Core 2.0, .NET Bio is able to run significantly faster (~2X) on Mac OSX due to better compilation and memory mangement.

The .NET Bio library contains libraries for genomic data processing tasks like parsing, alignment, etc. that are too computationally intense to be undertaken with interpreted languages like Python or R, but can be efficiently performed in code that is typed and compiled, like Java and C#, without suffering the productivity burden of coding in C/C++.

Historically, .NET Bio ran on two different frameworks.  On windows one could leverage all the substantial engineering Microsoft invested in their CLR to create fast and stable programs.  On Mac or Linux, one could use the independently implemented Mono Framework, which was a solid effort by a significantly less resourced team that performed well, but never really came close to the quality of the Microsoft implementation.  In practice, as bioinformaticians are weened on the Linux/GCC toolchain and infrastructure, Mono was the frequently used option.  This led to performance and reliability issues.

All of this changed when Microsoft decided to open source .NET , releasing a version of their framework for Mac, Linux and Windows called .NET Core.  This was a watershed moment, as it allows open source writers to take advantage of all the advanced memory management and compilation techniques Microsoft developed.  Better Garbage collection, advanced pre-compilation and vector instructions are all allowing higher level languages like C# to perform nearly as well as aggressively optimized C/C++ for many practical applications.

To test out the new possibilites, I compared the time it takes to parse all the reads in a 1.2 GB BAM file using either the old Mono runtime or the new dotnet core runtime on my MacBook computer.  The test code was simple counting of reads after they were deserialized from the compressed format and converted into objects.  

And the test case was timed on each platform as follows, the times were taken in triplicate and random order and the results are shown below.  I also benchmarked an equivalent task using the industry standard samtools available from htslib, a C library for the same task that uses tons of bit operations, and is about as performant as one can get 1)This is for methods that fully parse the BAM read, PySAM, which lazily decodes the data, can finish a simple counting task in about 15 seconds, but this isn’t representative of a workflow which would require full decoding..

Profiling Each Platform

Mono: The Mono platform allows us to easily profile the code using instruments on Mac OSX.  Examining the profile of the running code, we can see where the bottlenecks are.  A portion of time is spent in libz doing decompression of the underlying gzipped data, but it appears that much is also spent on garbage collection associated events, e.g. preparing for memory allocation with mono_handle_new.

.NET Core 2.0:  Being newly available on multiple platforms, there is no good way to profile .NET Core 2.0 on Mac OSX.  Although typical profilers cannot understand the C# JITted code, we can look at the compiled C/C++ code and here we see that .NET is spending twice as much time in libz as a percentage than Mono is.  Since much of this task is simply decompressing data, more time in the decompression library means the rest of the code is running faster, and is a good sign.   

To further profile the managed code, I wrote a custom MacOS Profiler that changed how each method was compiled to insert a hook that allowed me to track the enter and exit time of each function with a P/Invoke call to C++ code at the entrance and exit of each managed method frame, and so calculated the inclusive time spent in each method.  Although the addition of this instrumentation is expected to greatly skew the results, we saw that the majority of the managed code is spent parsing the data, and much of that is spent doing validations!

Method NameInclusive Time (s)Call Count
Bio.IO.BAM.BAMParser.GetAlignedSequence2.0123565255
Bio.Util.Helper.StringContainsIllegalCharacters0.670411487805
Bio.IO.SAM.SAMOptionalField.set_Tag0.551263711275
Bio.IO.SAM.SAMOptionalField.set_VType0.549131711275
Bio.IO.SAM.SAMOptionalField.IsValidTag0.547594711275
Bio.IO.SAM.SAMOptionalField.IsValidVType0.54615711275
Bio.IO.SAM.SAMOptionalField.set_Value0.543942711275
Bio.IO.SAM.SAMOptionalField.IsValidValue0.543379711275
SAMTools Continuing the trend that the more performant library spends more time in libz, samtools spends half of it’s time there, and the other half basically doing all the decompression operations to convert the binary data into usable data (in sam_format1). SAMTools is optimized C code with minimal error checking, so is a rough upper bound on the speed that could be obtained.

Conclusions

The C# code was written in a manner free of performance concerns and has tons of validation checks and unnecessary copies. Yet .NET core performed only 2X slower than high performance C code.  This is due to the fact that thanks to the engineering in .NET core, the code can now run twice as fast on Mac OSX as it used to.

Although C# is still slower than C, it is the clear winner here.  The SAMTools code bottlenecks at a method that although heavily optimized is horrifically difficult to read.  In contrast, the .NET code bottlenecks at a method that is filled with nearly gratuitous memory allocations, and a plethora of checks where useful exceptions could be thrown and meaningful error messages delivered if assumptions aren’t met.  Plus, it has all the advantages of memory safety and quality tooling we’ve come to expect in 2017.  It’s intriguing to ponder how the benchmark would look if the C# code had been written in a style as performance based as the C code, by avoiding unnecessary allocations, using pointers, or using the new ref returns available in C# 7.0. I’ve shown previously such changes s could significantly increase the parsers performance.

The .NET ecosystem is moving along well for meaningful scientific computing. Importantly, although time-spent C/C++ code will always be more performant, as .NET Core is now open source, we can embed any time-critical functionality in the runtime itself now, allowing us to have things like ludicrously performant suffix-arrays interacting with parsers.  .NET Core 2.0 is a big step forward.

References   [ + ]

1. This is for methods that fully parse the BAM read, PySAM, which lazily decodes the data, can finish a simple counting task in about 15 seconds, but this isn’t representative of a workflow which would require full decoding.