3

Considering that:

Microsoft Specific

The __fastcall calling convention specifies that arguments to functions are to be passed in registers, when possible. The following list shows the implementation of this calling convention.

And that the read/write time in a register is way faster than in a stack, do we have any __fastcall equivalent in C#?

Edward
  • 131
  • 2
  • 11
  • 2
    FYI: [Is fastcall faster?](http://stackoverflow.com/questions/2188680/is-fastcall-really-faster) –  Feb 06 '11 at 23:14

3 Answers3

5

Not directly, C# mostly uses what would be equivalent to MSVC++'s __stdcall convention. It can however be "fixed", though in a relatively dirty way (note that example is for __cdecl).

It's probably best this way, though. In a high-level language like C# (heck, even in most C++ programs) this is an optimization best left for the compiler. A forced calling convention can often make things worse. And even when it helps, it usually doesn't buy you much, at least in the C and C++ programs where I have used it.

3

__fastcall is used automatically but only in certain conditions. Here is an interesting article about this subject :

2.Not more than seven parameters should be there in a method.

The reason behind it is that in .net the first two parameters are faster than the last two parameters.Let me explain it more clearly. In C# whenever a method is called the parameters are pushed into the stack , which are then used by the method. Now Microsoft’s compilers(in X86) have an advanced optimization technique called the __FASTCALL, wherein the first two parameters are sent across as registers. These are now said to have become enregistered. Well after registration ,the variable or parameter has fast track promotion with exclusive privilege of being stored in the processor’s fastest cache. Do note this is usually done to the variable “i” we use during looping or iteration, due to which its access and usage become really fast indeed. Thus, during compilation the method are compiled into native code by the .Net runtime with __FASTCALL action so a method with less number of parameters is much more optimized than that with too many parameters.

Source

Dalmas
  • 26,409
  • 9
  • 67
  • 80
  • Excelent explanation Lada. Let me add one point: "a method with less number of parameters is much more optimized than that with too many". Is there any restriction on the type of parameters? I mean, does it works only with value types? Considering that reference types are stored in the stack. Or is there any chance that we can send an class instance to the register? (Sorry for editing, mistyped) – Edward Feb 06 '11 at 23:09
  • Reference types are stored in the heap (but their memory addresses are on the stack). Passing a reference type to a function basically means passing his memory address, it's like passing a pointer to a c++ function and it's just a 32 or 64bit integer (depending on the platform), so no there are no restrictions. – Dalmas Feb 06 '11 at 23:53
  • 1
    Well I think there is one restriction, if you pass a huge value type to a function, it's too big to be stored in a register... but I don't know enough to tell you more how the compiler deals about this. – Dalmas Feb 06 '11 at 23:57
  • +1 for the 2nd excellent explanation, i got your point. Thanks Lada – Edward Feb 07 '11 at 00:28
0

LadaRaider, on 32-bit Arch which means "Maximum size of the biggest registers is 4 Bytes" if u pass an "Long Long" which takes 8 Bytes it will use 2 registers of 4 Bytes, that's how the compiler deals with it. Let's say u get to use only 3 registers of 4 Bytes, so, u can't pass 2 "Long Long" variables for example... Some data will have to go into the memory which is a lot more slower.