docs icon indicating copy to clipboard operation
docs copied to clipboard

Are throw helpers really more efficient?

Open deep-outcome opened this issue 1 year ago • 2 comments

Type of issue

Typo

Description

From https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8/sdk#code-analysis

Throw helpers are simpler and more efficient than an if block constructing a new exception instance.

Having look at https://github.com/dotnet/runtime/blob/5535e31a712343a63f5d7d796cd874e563e5ac14/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs, I see that in order to throw 2 methods are called and no optimization which could be more efficient than simple if-then construct.

Page URL

https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-8/sdk

Content source URL

https://github.com/dotnet/docs/blob/main/docs/core/whats-new/dotnet-8/sdk.md

Document Version Independent Id

2dd1bc19-cfa0-3d7d-4931-1f0f4fefac12

Article author

@gewarren

Metadata

  • ID: 0b25da3d-7bbf-d16d-3caf-e56923fe9716
  • Service: dotnet-fundamentals

Related Issues

deep-outcome avatar Oct 02 '24 13:10 deep-outcome

I see that in order to throw 2 methods are called

At runtime the JIT will inline the method. On the other side the JIT sees that the Throw-method does not return, so it won't inline that method too

Further on the plus-side is the use of CallerArgumentExpression which can be coded in other ways to achieve the same effect.

gfoidl avatar Oct 03 '24 11:10 gfoidl

Aha, I did not see inlining annotations. I believe what you say. Nonetheless in the end it is not more effecient, right?

deep-outcome avatar Oct 03 '24 16:10 deep-outcome