Would it make sense for the JITer to eliminate redundant "if null then throw" (i.e. if (arg == null) throw Exception();) checks for a given argument if it is not possible for that argument value to have changed between the checks? As far as I can tell this does not currently happen, even for simple cases.
For example:
void M1(string value) => M2(value ?? throw new ArgumentNullException();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void M2(string value) => M3(value ?? throw new ArgumentNullException();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void M3(string value) => M4(value ?? throw new ArgumentNullException();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void M4(string value)
{
if (value == null)
throw new ArgumentNullException();
}
// After inlining M1 effectively becomes:
void M1(string value)
{
if (value == null) throw new ArgumentNullException();
// These can be removed:
if (value == null) throw new ArgumentNullException();
if (value == null) throw new ArgumentNullException();
if (value == null) throw new ArgumentNullException();
}
Would it make sense for the JITer to eliminate redundant "if null then throw" (i.e.
if (arg == null) throw Exception();) checks for a given argument if it is not possible for that argument value to have changed between the checks? As far as I can tell this does not currently happen, even for simple cases.For example: