Description
EDITED 03/06/2021 by @stephentoub to add revised proposal:
public class ArgumentNullException
{
+ public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression("argument")] string? argumentName = null);
}
Background and Motivation
The .NET ecosystem uses extensive argument checking to improve code reliability and predictability. These checks have a substantial impact on code size and often dominate the code for small functions and property setters. This in consumes more RAM, takes more time to JIT, prevents inlining, and most important it causes substantial instruction cache pollution. Ultimately, the presence of these checks slows code down.
Many libraries, including the framework libraries themselves, implement exception throwing helpers to compensate for this bloat. These simple static functions centralize the exception creation and throwing logic. Why not enshrine this pattern in the exception API surface to encourage smaller/faster code, and avoid library authors having to create these stubs themselves?
Proposed API
I propose that the core framework ArgumentXXXException classes be augmented with static functions responsible for both allocating and throwing the exceptions:
public class ArgumentNullException : ArgumentException
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static void Throw(string paramName) =>
throw new ArgumentNullException(nameof(paramName));
}
There would be one static method corresponding to each constructor signature of the exception type.
This pattern would be warranted for any exception type that has a sufficiently large usage footprint. Certainly the ArgumentXXXException types would qualify for this, perhaps a few others.
Usage Examples
With such functions, the following code
public void DoSomething(Foo foo)
{
if (foo == null) throw new ArgumentNullException(nameof(foo));
}
would become
public void DoSomething(Foo foo)
{
if (foo == null) ArgumentNullException.Throw(nameof(foo));
}
Analyzer and Fixer or Compiler Voodoo
It would be trivial to include an analyzer and associated fixer to upgrade a code base to the new approach, thus encouraging a rapid migration.
Alternatively, the C# compiler could potentially be upgraded to automatically replace canonical uses into calls to the static method which wouldn't require any code changes to yield the perf benefits.
Generated Code
The code required to create and throw an exception costs > 70 bytes of instructions. Here is an example null check compiled in release mode for .NET 5:
00007FFADA6D510B 48837D1800 cmp qword ptr [rbp+18h],0
00007FFADA6D5110 7541 jne short LBL_0
00007FFADA6D5112 48B980C974DAFA7F0000 mov rcx,offset methodtable(System.ArgumentNullException)
00007FFADA6D511C E83F26B25F call CORINFO_HELP_NEWSFAST
00007FFADA6D5121 488945A0 mov [rbp-60h],rax
00007FFADA6D5125 B901000000 mov ecx,1
00007FFADA6D512A 48BA70DB88DAFA7F0000 mov rdx,7FFADA88DB70h
00007FFADA6D5134 E8C7B3C45F call CORINFO_HELP_STRCNS
00007FFADA6D5139 48894598 mov [rbp-68h],rax
00007FFADA6D513D 488B5598 mov rdx,[rbp-68h]
00007FFADA6D5141 488B4DA0 mov rcx,[rbp-60h]
00007FFADA6D5145 E8D61BFEFF call System.ArgumentNullException..ctor(System.String)
00007FFADA6D514A 488B4DA0 mov rcx,[rbp-60h]
00007FFADA6D514E E87D62AE5F call CORINFO_HELP_THROW
LBL_0:
00007FFADA6D5153 488B5510 mov rdx,[rbp+10h]