1

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?

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
  • 1
    Not directly -- no new IL instructions have been added in a very long time, for example. You will miss out on tools which are in newer runtime versions which will let *you* write faster code (if you wanted to do that), such as ref types, ref returns, etc. You will also miss out on newer language features, unless you polyfill parts of the runtime which the newer compiler versions depend on (which is unsupported, but works well in practice) – canton7 Nov 22 '22 at 17:17
  • 1
    No difference, the jitter only sees .net7 assemblies after the CLR has resolved the references. There are no "new instructions". – Hans Passant Nov 22 '22 at 17:17
  • @HansPassant, indeed, I thought they added something new for covariant returns in virtual methods, but nope. – Pavel Voronin Nov 22 '22 at 17:43
  • How can netstqandard2.0 run on .Net7? I think it is impossible with all the Core changes. If you target netstandard 2.0 it will run on Core 2.0, not Core 7.0. – jdweng Nov 22 '22 at 17:56
  • @jdweng .netstandard is not about runtime, it's about api capabilities. You cannot run such assemblies by themselves, but you can reference them from assemblies which target real runtime. – Pavel Voronin Nov 22 '22 at 19:00
  • @PavelVoronin : You need a version of Net/Core that is capable of running the code install on machine. If you are running on same machine as built than there is not an issue. But if you are deploying on a different machine you have to install. Read the heading. You cannot target netstandard 2.0 and then run in Net 7. – jdweng Nov 22 '22 at 21:06
  • 1
    @jdweng You very much can run an assembly which targets .net standard on the .net 7 runtime. That's the entire point of .net standard. – canton7 Nov 23 '22 at 09:06
  • @canton7 : Then you are not targeting netstandard 2. – jdweng Nov 23 '22 at 09:34
  • 1
    @jdweng ... yes you are. Applications can't target .net standard of course, but libraries can. And a library which targets .net standard can be loaded by an application running on .net 7 – canton7 Nov 23 '22 at 10:03
  • @canton7 : You are not running netstandard 2, you are then running Net 7. When targetting not all source code will compile without errors. Just because you target doesn't mean the code will compile. Microsoft has NOT made the libraries forward/backwards compatible. Yes some source code will compile when you target both NetStandard 2 and Net 7. But the compiled code may be very different and use different low level methods. The upgrade from Net to Core was done so same code would run on linux and windows. So the low level interface to OS was completely changed in Core. – jdweng Nov 23 '22 at 10:48
  • 2
    @jdweng You can't "run on" .net standard. .net standard is an api specification. Libraries can target the .net standard, which means that they will run on any runtime which implements that version of the .net standard (including Mono, Xamarin, etc). [See the documentation](https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-1-0). This is very basic stuff. – canton7 Nov 23 '22 at 12:59
  • Getting back to original question. Net 7 is upgraded for Net Standard 2.1. If performance boost changes are in Net Standard 2.0 then you will get performance improvements. if boost changes were made after netstandad 2.0 than you will not get performance improvements. – jdweng Nov 23 '22 at 13:15
  • "If performance boost changes are in Net Standard 2.0" - .NET Standard is just declarations, basically - not an implementation. So there's no such thing as a performance boost in it. But yes, if you run an application in .NET 7.0 that uses a library targeting .NET Standard 2.0, you *will* get the performance of .NET 7.0. – Jon Skeet Jul 21 '23 at 15:16
  • 2
    @jdweng you are absolutely wrong on this; please accept this - it is trivial to simply target ns2.0 and run on net7. You will get every single runtime (JIT, GC, etc) and BC (library implementations etc) performance improvement. The only thing you won't get to use is new APIs **in your code** that don't exist in ns2.0, which is **only** the API definitions (not implementations). It is basically a header file. The BCL, however, can use all of those APIs (and it will) - because it is the net7 BCL. There is no such thing as a ns2.0 runtime/BCL – Marc Gravell Jul 21 '23 at 16:28

1 Answers1

2

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.

Batesias
  • 1,914
  • 1
  • 12
  • 22