Is there a reason why C # doesn't support manual inline methods? What about additional parameters?

Is there any design reason for this (like the reason they gave up on multiple inheritance)?

or was it just not important enough?

and the same question applies for optional parameters in methods ... this was already in the first version of vb.net ... so there is definitely no laziness that forces MS to not allow additional parameters, maybe architecture solution .. and it looks like , they changed their mind about it because C # 4 will include this.

Does anyone know what the solution was and why they gave up?

EDIT:

Maybe you didn't quite understand me. Recently I have been working on a calculation program (support numbers of any size, up to the last digit) in which some methods are used literally millions of times per second.

Let's say I have an Add (int num) method and this method is used very quietly with 1 as parameter ( Add(1);

), I found it faster to implement a custom method especially for one. And I don't mean overloading - writing a new method called AddOne and literally copying the Add method into it, except that instead of using it, num

I write 1

. This may sound terribly odd to you, but it is actually faster.

(how ugly it is)

This made me wonder why C # doesn't support the guide, which can be surprisingly helpful here.

thank. (and why did you vote for me: S)

Edit 2:

I asked myself if it was worth adding this. I am very familiar with the weirdness (and disadvantages) of choosing a platform such as a net point for a project like this, but I think pure point optimization is more important than you think ... especially features like Any CPU, etc.

+2


source to share


7 replies


To answer part of your question, see Eric Gunnerson's blog post: Why doesn't C # have the 'inline' keyword?

Quoting from his post:



For C #, nesting happens at a level JIT, and JIT usually makes a decent decision.

EDIT: I'm not sure why the support for additional parameters was delayed, however, saying they "gave up" sounds like they should have implemented it based on our expectations of what other languages ​​offer. I guess it wasn't high on the priority list and they had deadlines to get certain features in the door for each release. It probably hasn't been raised so far, especially since method overloading was an available alternative. In the meantime, we got generics (2.0) and functions that make LINQ possible, etc. (3.0). I'm happy with the progression of the language; the above features are more important to me than the early support for additional parameters.

+6


source


Manual insertion will be almost useless. The JIT compiler embeds the methods inline compilation of the code where needed, and I think in almost all cases the JIT compiler is better at guessing when it is appropriate than the programmer.

As for the additional parameters, I don't know why there weren't any in previous versions. However, I don't like to have them there in C # 4 because I find them somewhat harmful because the parameter is baked into the consumption assembly and you need to recompile it if you change the default values ​​in the DLL and want to consumer to use new ones.

EDIT:



More information on inlining. While you cannot force the JIT compiler to inline a method call, you can force it to NOT inline a method call. You use System.Runtime.CompilerServices.MethodImplAttribute for this, for example:

internal static class MyClass
{
    [System.Runtime.CompilerServices.MethodImplAttribute(MethodImplOptions.NoInlining)]
    private static void MyMethod()
    {
        //Powerful, magical code
    }

    //Other code
}

      

+3


source


My educated guess: The reason earlier versions of C # didn't have optional parameters is because of bad experience with them in C ++. At first glance, they look straightforward, but there are some nasty corner cases. I think one of Herb Sutter's books describes this in more detail; in general it has to do with method overriding virtual

. Maximilian mentioned one of the .NET cases in his answer.

You can also pretty much deal with them by manually creating multiple overloads; this may not be very pleasant for the class author, but clients are unlikely to notice the difference between overloads and optional parameters.

So after all these years without them, why did C # 4.0 add them? 1) improved parity with VB.NET and 2) easier interoperability with COM.

+1


source


Recently I have been working on a calculation program (support numbers of any size, up to the last digit) in which some methods are used literally millions of times per second.

Then you chose the wrong language. I assume you have really profiled your code (right?) And you know there is nothing but micro-optimization that can help you. Also, you are using the high performance native bigint library and are not writing your own, are you?

If that's true, don't use .NET. If you think you can get some speed from partial specialization, go to Haskell, C, Fortran, or any other language that either does it automatically or can provide you with inlay to do it manually.

If it Add(1)

really matters to you, heap allocations are also important.

However, you should really see what the profiler can tell you ...

+1


source


C # added them in 4.0: http://msdn.microsoft.com/en-us/library/dd264739(VS.100).aspx

As for why they weren't done in the beginning, it's most likely because they felt that method overloads offered a lot of flexibility. With overloading, you can specify multiple "defaults" based on other parameters you accept. It's also not much more syntax.

0


source


Even in languages ​​like C ++, nesting something doesn't guarantee it will happen; this is a compiler hint. The compiler can either take a hint or do its thing .

C # is another step removed from the generated assembly code (via IL + JIT), so it becomes even more difficult to ensure that something is inlined. Also, you have problems similar to x86 + x64 JIT implementations with different behavior.

0


source


Java does not include the inline keyword . The best Java JITs can inline even virtual methods, and using keywords like private or final doesn't make any difference (it used to be used, but now it's ancient history).

0


source







All Articles