Why are C # built-in functions with structure parameters?

At http://blogs.msdn.com/ericgu/archive/2004/01/29/64717.aspx we find out that C # will not inline methods with struct as formal parameters. This is due to potential stack dependency, for example. for recursion? If so, can I potentially benefit by turning the structure parameters into ref parameters like this?

public int Sum(int i)
{
  return array1[i] + array2[i];
}

turns into:

public int Sum(ref int i)
{
  return array1[i] + array2[i];
}

Edit: I went to try the test, but I cannot get anything on the line. Here is what I tried:

class Program
{
  private static string result;
  static void Main(string[] args)
  {
    Console.WriteLine(MethodBase.GetCurrentMethod().Name);
    Console.WriteLine();
    m1();
    Console.WriteLine(result);
  }
  private static void m1()
  {
    result = MethodBase.GetCurrentMethod().Name;
  }
}

It prints "m1" as the second line, which indicates that it did not hit the line. I built the Release assembly and ran it with Ctrl-F5 (so as not to attach the debugger). Any ideas?

+5
5

, . , :

using System;
using System.Runtime.CompilerServices;

struct MyStruct
{
   public MyStruct(int p)
   {
      X = p;
   }
   public int X;

   // prevents optimization of the whole thing to a constant.
   [MethodImpl(MethodImplOptions.NoInlining)]
   static int GetSomeNumber()
   {
       return new Random().Next();
   }

   static void Main(string[] args)
   {
      MyStruct x = new MyStruct(GetSomeNumber());
      // the following line is to prevent further optimization:
      for (int i = inlinetest(x); i != 100 ; i /= 2) ; 
   }

   static int inlinetest(MyStruct x)
   {
      return x.X + 1;
   }
}

inlinetest .

:

; set up the stack frame:
00000000  push        ebp
00000001  mov         ebp,esp 

; calls GetSomeNumber:
00000003  call        dword ptr ds:[005132D8h] 

; inlined function:
00000009  inc         eax  

; the dummy for loop:
0000000a  cmp         eax,64h 
0000000d  je          0000001B 
0000000f  sar         eax,1 
00000011  jns         00000016 
00000013  adc         eax,0 
00000016  cmp         eax,64h 
00000019  jne         0000000F 
0000001b  pop         ebp  
0000001c  ret 

x86.NET Framework 3.5 1 Windows 7 x64 RC.

, , inlining struct. , JIT .

+4

, , . MS connect , (FWIW)

+2

. , # . , JITer .

, JITER struct , .
, , , .

0

, , . () ( ), structs footprams footprint.

0

, . , value-type (say, int), . , , , .

, . ! , .

, , , inlining ref-ref - , .

, , - , "", . .

By the way, you can always declare the int module level and go around all the arguments together :) Much uglier, but you will end up with this function.

Whatever you decide to do, good luck!

EDIT: I just remembered that there was an ancient C compiler for Irix (SGI operating system) that actually had a compiler option for this, allowing you to "force" inlining. So it can be done, but I agree with the choice made here when choosing a less acceptable default value.

-3
source

All Articles