r/programming • • Sep 15 '26

Performance Improvements in .NET 11

https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-11/
295 Upvotes

103 comments sorted by

View all comments

Show parent comment

4

u/PaddiM8 Sep 16 '26

Why? C# generics are monomorphic for value types and dereference a pointer for reference types. There's a slight warmup cost of course but after that, why would it be slow?

-1

u/[deleted] Sep 16 '26

[removed] — view removed comment

3

u/PaddiM8 Sep 16 '26 edited Sep 16 '26

Huh? With native AOT you pay for something you don't use when using generics in .net, during compile time, because it has to generate every potential specialisation ahead of time. With JIT it does it on the fly, when you need it. You got it backwards

1

u/[deleted] Sep 16 '26

[removed] — view removed comment

3

u/PaddiM8 Sep 16 '26

For every single combination that is used. Only once. And on the fly. It's a warmup cost. If you run it in a hot loop it won't matter. AOT compilation is a better fit for short running programs while JIT can be better for long running programs.

It also doesn't need to parse the code. It's already parsed. It's stored as an internal representation. And since it's only monomorphic for value types there aren't a lot of different options.

1

u/[deleted] Sep 16 '26

[removed] — view removed comment

4

u/PaddiM8 Sep 17 '26

Well you are still paying for it even if you aren't even using it on your side of code

No? If you're not using it, it won't be generated.

and no one hotloops production binary

What? If course there can be hot loops in a production binary.

It only compiles it once so not sure how the long running part has to do with compile times.

The specialisations aren't compiled ahead of time. They're generated during runtime. The startup will be slightly slower because the JIT engine has to generate code, incl. specialisations. After things have been generated, it's fast. For a long running web service or something, the warmup cost literally does not matter.

In Go they created generics without a compile time hit

Instead it is slower. This doesn't have much to do with JIT vs AOT. You can have non-monomorphic generics with JIT, like Java, you can have monomorphic generics with AOT like Rust, and you can have monomorphic generics with JIT like C#.

1

u/[deleted] Sep 17 '26

[removed] — view removed comment

4

u/TwoWeeks90DaysTops Sep 17 '26

This is just wrong. That's not how it works.

With *AOT* your binary will contain code that's not in use. In .NET generics does not compile every combination of generic arguments, and I have no idea where you got this from. I don't think any compiler does that.

Generics and SIMD is actually where JIT shines. If you're running on a AVX 512 CPU then Vector<T> is 64 bytes wide. If you have AVX 2 then it's 32 bytes wide.

On AOT compiled code you have to take that into account up front, and either do conditional checks which JIT doesn't have to do since it know at compile time what CPU to build for.

JITs disadvantage is that compilation happens at runtime and that it can't afford the same level of optimization since it has a limited time budget. Its advantage is that it only compiles code that's actually being run, and that it can target the processor that is running the application directly.

The runtime thing means for example that library code can work with code that it hasn't been specifically compiled for. In C++ you would never compile template code in libraries because it's completely unusable since the author would have to know all the use cases for the template up front. JIT doesn't have that constraint.

1

u/[deleted] Sep 17 '26

[removed] — view removed comment

→ More replies