Puis-je avoir un extrait de code concis qui entraînerait une mise en ligne JIT, s'il vous plaît ?

StackOverflow https://stackoverflow.com//questions/12686850

  •  12-12-2019
  •  | 
  •  

Question

J'essaie de produire un extrait de code C# de taille "Hello World" qui entraînerait une inlining JIT.Pour l'instant j'ai ceci :

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine( GetAssembly().FullName );
        Console.ReadLine();
    }

    static Assembly GetAssembly()
    {
        return System.Reflection.Assembly.GetCallingAssembly();
    }
}

que je compile sous "Release" - "Any CPU" et "Exécuter sans débogage" à partir de Visual Studio.Il affiche si clairement le nom de mon exemple d’assembly de programme GetAssembly() n'est pas intégré dans Main(), sinon il afficherait mscorlib nom de l’assembly.

Comment puis-je composer un extrait de code C# qui entraînerait une mise en ligne JIT ?

Était-ce utile?

La solution

Bien sûr, voici un exemple :

using System;

class Test
{
    static void Main()
    {
        CallThrow();
    }

    static void CallThrow()
    {
        Throw();
    }

    static void Throw()
    {
        // Add a condition to try to disuade the JIT
        // compiler from inlining *this* method. Could
        // do this with attributes...
        if (DateTime.Today.Year > 1000)
        {
            throw new Exception();
        }
    }
}

Compilez dans un mode de type release :

csc /o+ /debug- Test.cs

Courir:

c:\Users\Jon\Test>test

Unhandled Exception: System.Exception: Exception of type 'System.Exception' was
thrown.
   at Test.Throw()
   at Test.Main()

Notez la trace de la pile - on dirait que Throw a été appelé directement par Main, parce que le code pour CallThrow était en ligne.

Autres conseils

Votre compréhension de l'inline semble incorrecte :Si GetAssembly était intégré, il afficherait toujours le nom de votre programme.

Inline signifie :"Utiliser le corps de la fonction à l'endroit de l'appel de fonction".Inline GetAssembly conduirait à un code équivalent à ceci :

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(System.Reflection.Assembly.GetCallingAssembly()
                                                    .FullName);
        Console.ReadLine();
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top