0

I have been using an Extension function to make life easier for checking null parameters and throwing subsequent ArgumentNullExceptions. The function will also return the value being checked, allowing for some slick chaining.

I am now concerned that this extension function (which is used a lot) may not be as memory performant as it could be due to the possible lack of inlining.

Questions

  1. Is this function inlined, and how is that determined?
  2. If not inlined, then what are the ramifications? Consider this function is already used hundreds of times in just the base code set.

Extension Function

public static class ValueExtensions
{
    public static T CheckNull<T>([NotNull] this T? argument, [CallerArgumentExpression("argument")] string? paramName = null)
    {
        if (argument is null) {
            throw new ArgumentNullException(paramName);
        }

        return argument;
    }
}

Use Case

public class MyClass
{
    public void SomeFunction(OtherClass arg1) => arg1.CheckNull().DoSomething();
}

Vs

public class MyClass
{
    public void SomeFunction(OtherClass arg1)
    {
        arg1.CheckNull();
        arg1.DoSomething();
    }
}
heyufool1
  • 63
  • 7
  • You should use [`Benchmark.Net`](https://benchmarkdotnet.org/) to do some performance testing. – Matthew Watson Jul 31 '23 at 14:32
  • Fair enough, can give that whirl – heyufool1 Jul 31 '23 at 14:38
  • Check out [the code on SharpLab.IO](https://sharplab.io/#v2:C4LglgNgPgAgTARgLACgYAYAEMEDoAiYAhgOYB2A9gM7BgDGVuAwhQCYCmAgmURAJ5UwVANyoM2PACUArmVoBbdswryADpHYAnAMpaAbvXYjUYgMzY4mJpgDeqTA+zmYAFkwBZABQBKW/ccAvqhBKGYSAGwWmABqvNLsAKIAHsDsZIIU6ah2KI5OEZgAKlYAFux0ANYActIQEAA8hQB8ngDaVRTANXUAupjAJUJFAPyYRJok0opyADSYrUy8EFqcE1NpwMmqmkYZZJ4AROOT08AH3n046KOq40TyVffsmAC8mGS1EN7+Djl5eWAAGaYTzHdZyTBDD51Xx/f7wgaaCgAd3e7FRqxOG26EGSdHYqlomU8t0090eim8oly8MwIR+/xgAHYxmtTtS8iEQkA=) - Scroll down to the implementation of `CheckNull()` and it looks like it's doing a cheeky wee `Box !!T` which seems not so good. – Matthew Watson Jul 31 '23 at 14:47
  • I think the ASM is eliding that though. – Matthew Watson Jul 31 '23 at 14:52
  • That boxing is good enough, especially considering this a helper function that's in just about every constructor and function... Where do you see what the ASM is doing? – heyufool1 Jul 31 '23 at 15:58
  • You can select the `JIT ASM` option from the right-hand drop-list at the top, but looking at it, it seems that it can't show the assembler for `CheckNull()` – Matthew Watson Jul 31 '23 at 16:23
  • Ok yeah that's what I was seeing too. Tried forcing a generic "object" but it also can't handle reference types. With that said though, the frequent boxing is already a bit "scary" to have littered everywhere – heyufool1 Jul 31 '23 at 16:50

1 Answers1

0

According to this source: https://medium.com/@martinstm/performance-wars-null-check-c-affdd096813e

arg == null is the fastest for strings.

while:

arg is null is the fastest for objects

  • 1
    That's interesting to see but makes sense as "is" is shorthand for Equals whereas == is more primitive. But, this doesn't relate to my question of whether the Extension function is inlined or not and the performance implications. – heyufool1 Jul 31 '23 at 14:35
  • Thanks for the polite response so I looked it up and i don't think you can choose if it's inlined or not "the one who decides on inlining isn't VS compiler that takes you code and converts it into IL, but JIT compiler that takes IL and converts it to machine code. This is because only the JIT compiler knows enough about the processor architecture to decide if putting a method inline is appropriate as it’s a tradeoff between instruction pipelining and cache size." https://stackoverflow.com/a/616815/17595716 – Ibraheem Khazbak Jul 31 '23 at 14:51