Search code examples
c#.netperformance.net-standardintermediate-language

Will assembly which targets .netstandard2.0 but runs on .NET 7 get all the performance boost from the latest runtime?


I have a choice for the assembly to target .netstandard2.0 or .net7.0. If I do not need latest features of C#, will it eventually make any performance difference when running my application on .NET ?

AFAIK, JIT and types from BCL are provided by the runtime, so improvements in that area should have a positive impact on performance.

In theory C# compiler can emit IL which lacks some new instructions supported by newer runtime, so IL code can be slightly less optimal compared to what it would be, if .NET7 was targeted.

Am I missing anything what can hinder performance?


Solution

  • Yes, your netstandard2.0 library benefits from .Net 7 performance improvements when ran from a .Net 7 application.

    To test this, I wrote some benchmark code (using Linq, which was significantly improved in .Net 7) in a netstandard2.0 library which I then referenced from 2 separate console apps (one targeting net6.0 and another net7.0). The performance differences are similar to the ones described here even though the Linq code was built into a separate netstandard2.0 dll.

    Also, when I change the target of the library from netstandard2.0 to net7.0, the benchmarks yield similar results. (Some tests were better, others were worst, so I think it's within margin of error.) It's not clear to me if one is better than the other so I would personally choose the target based on the cross-platform targeting guidance rather than performance.