Faster .NET in version 7? 500 performance improvements detailed

Faster .NET in version 7? 500 performance improvements detailed

Microsoft’s Stephen Toub, a partner software engineer for .NET at Microsoft, has written a massive post detailing performance improvements in the forthcoming .NET 7, for which release is expected around November this year.

Toub’s book-length article (over 75,000 words) is based on “almost 1000 performance-impacting PRs [pull requests]”, of which he describes around 500, concluding that “it’s the fastest .NET ever.” The .NET Runtime supports Windows, Linux and macOS; Toub states that his results are recorded on Windows 11 64-bit but “aren’t Windows-specific.”

The question of how much faster is slippery. A big speed-up in some corner of the .NET just-in-time (JIT) compiler, for example, might make little difference to most applications but a big difference for code that happens to stress that part of its functionality. One of the things Toub discusses is bounds-check elimination, meaning cases where the compiler can maintain memory safety but improve performance by skipping checks for accessing memory out of the bounds of a data structure, by proving that a check is unnecessary. A performance test showed a four-times speed-up in some array operations.

Another eye-catching example is speeding performance by vectorizing data, where Toub shows example code that gives a 24x speed-up. .NET 7 has added “a very large set of cross-platform operations” based on generic Vector types, which enable this.

Faster code from vectorization in .NET 7

That raises the question: do developers need to refactor their code to get better performance, or simply upgrade from .NET 6 (or lower) to .NET 7? The answer is a bit of both. Toub gives this as a reason for writing at such length. “I want developers to learn from our own learnings and find ways to apply this new-found knowledge to their own codebases,” he said. Another factor is that typical .NET code depends on Microsoft frameworks like ASP.NET and/or Entity Framework, so if these perform better then applications based on them also benefit.

String operations are important in many operations and often relatively slow. Developers will be pleased to see that a commonly used method like IndexOf has a substantial performance increase (20x in one example), and StartsWith up to 5x faster.

Toub also covers performance on Arm64, which is increasingly important now that this processor architecture is common on the server as well as on desktop via Apple Silicon and Windows on Arm. “A huge amount of effort in .NET 7 went into making code gen for Arm64 as good or better than its x64 counterpart,” he claims.

How fast is .NET relative to other runtimes like Java, Python or JavaScript? Interested parties may consult something like the benchmarks game which has plenty of statistics. A quick glance shows that system languages like C, C++ and Rust are the performance champions, as one would expect, but C# often compares well to other high-level and memory-safe languages like Java, while languages like PHP, Ruby and Python trail behind.

The performance of real-world applications though depends more on the skill of developers than algorithm benchmarks. Still, getting more speed simply by upgrading the runtime is always an attractive prospect, though note that .NET 7 is not a long-term support release so many will wait for .NET 8 next year to put these changes into production.