Inline MSIL?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris LaJoie

    #1

    Inline MSIL?

    Is it possible to use 'inline' MSIL (Intermediate Language) similar to how
    you can use inline assembly in C/C++? If not, how can I include some code
    written in IL in my project? Thanks,

    Chris LaJoie


  • Austin Ehlers

    #2
    Re: Inline MSIL?

    You should be able to just use ilasm.exe and make a DLL from the MSIL,
    and then just add a reference to it from your project.

    Austin Ehlers

    On Sat, 6 Sep 2003 21:09:07 -0600, "Chris LaJoie"
    <chris@etriptra der.com> wrote:
    [color=blue]
    >Is it possible to use 'inline' MSIL (Intermediate Language) similar to how
    >you can use inline assembly in C/C++? If not, how can I include some code
    >written in IL in my project? Thanks,
    >
    >Chris LaJoie
    >[/color]

    Comment

    • Chris LaJoie

      #3
      Re: Inline MSIL?

      Thats what I'm trying to avoid. I'd like to keep classes written in C# and
      classes written in IL in the same assembly.

      Chris LaJoie



      "Austin Ehlers" <the*remove*bor ed*me*guy16@hot mail.com> wrote in message
      news:67dllvo2qb oftsetahvbohjs3 4c1kvfua9@4ax.c om...[color=blue]
      > You should be able to just use ilasm.exe and make a DLL from the MSIL,
      > and then just add a reference to it from your project.
      >
      > Austin Ehlers
      >
      > On Sat, 6 Sep 2003 21:09:07 -0600, "Chris LaJoie"
      > <chris@etriptra der.com> wrote:
      >[color=green]
      > >Is it possible to use 'inline' MSIL (Intermediate Language) similar to[/color][/color]
      how[color=blue][color=green]
      > >you can use inline assembly in C/C++? If not, how can I include some[/color][/color]
      code[color=blue][color=green]
      > >written in IL in my project? Thanks,
      > >
      > >Chris LaJoie
      > >[/color]
      >[/color]


      Comment

      • Michael Mayer

        #4
        Re: Inline MSIL?

        "Chris LaJoie" <chris@etriptra der.com> wrote in message
        news:eW%23so1Od DHA.2168@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > Is it possible to use 'inline' MSIL (Intermediate Language) similar to how
        > you can use inline assembly in C/C++? If not, how can I include some code
        > written in IL in my project? Thanks,[/color]

        I'm curious why you're writing in IL? Is it for some sort of performance
        boost (is there even one? - I'd be kindof surprised) or is it to do things
        that the framework supports but maybe C# doesn't allow you to do?

        Don't read my post as anything negative - just curious what motivates a
        person to write in IL.

        --
        Mike Mayer

        mike@mag37.com


        Comment

        • Jon Skeet

          #5
          Re: Inline MSIL?

          Michael Mayer <mike@mag37.com > wrote:[color=blue]
          > "Chris LaJoie" <chris@etriptra der.com> wrote:[color=green]
          > > Is it possible to use 'inline' MSIL (Intermediate Language) similar to how
          > > you can use inline assembly in C/C++? If not, how can I include some code
          > > written in IL in my project? Thanks,[/color]
          >
          > I'm curious why you're writing in IL? Is it for some sort of performance
          > boost (is there even one? - I'd be kindof surprised) or is it to do things
          > that the framework supports but maybe C# doesn't allow you to do?[/color]

          I can think of one thing offhand where the latter might be useful. IL
          allows you to unbox a reference without copying the value - you can
          use/modify the value directly. Suppose you were counting word
          occurences, and you were using a hashtable mapping from the word to the
          number of occurences so far. In IL, you could unbox the value and
          increment it directly, without occurring the copy and the subsequent
          boxing cost. In C# you'd have to unbox and copy, increment, then rebox
          and then change the value in the hashtable to the new reference, which
          is much more expensive.

          Of course, in C# you could define a MutableIntegerR eference class
          instead, but that defeats the point of the example :)

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Chris LaJoie

            #6
            Re: Inline MSIL?

            > I'm curious why you're writing in IL? Is it for some sort of performance[color=blue]
            > boost (is there even one? - I'd be kindof surprised) or is it to do things
            > that the framework supports but maybe C# doesn't allow you to do?
            >
            > Don't read my post as anything negative - just curious what motivates a
            > person to write in IL.
            >
            > --
            > Mike Mayer[/color]

            Thats a good question. I have several methods that must loop through
            thousands of objects in an array, and manipulate each of them. When I
            disassemble it, I find that C# simply isn't doing it in the most efficient
            way, so I'd like to write 3 methods of my class in IL, and the rest in C#.
            This apparently isn't possible.

            Chris LaJoie


            Comment

            • Thomas Scheidegger [MVP]

              #7
              Re: Inline MSIL?

              > I find that C# simply isn't doing it in the most efficient way,

              by the current design/implementation, it is for 99%
              the job of the JITer to optimize code.
              [and BTW, the JITer does it extremely well, mostly]

              The C# compiler does only minimal 'cleanup'.

              It has many advantages to do optimization
              at JIT/runtime:

              - JITer can produce code for the actual CPU
              (instruction set, or even 64-Bit architecture in future)
              - adapt to other contexts like OS, RAM.
              - method inlining even for libraries
              - ...
              - the optimizer works for ALL .NET languages!


              --
              Thomas Scheidegger - MVP .NET - 'NETMaster'
              http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/



              Comment

              • Chris LaJoie

                #8
                Re: Inline MSIL?

                > by the current design/implementation, it is for 99%[color=blue]
                > the job of the JITer to optimize code.[/color]

                That doesn't make sense at all. Why would Microsoft choose to have their
                code optimized at runtime rather than at compile time?


                In my case, the C# compiler is making IL for a single function that spans
                ~70 lines. I have rewritten that same function myself in IL in 30 lines.
                My problem is more complex, but to give you a basic idea of what the C#
                compiler makes vs. what I can code myself I'll give you an example:

                here is the C# code for an 'Add' function:

                private static int Add(int op1, int op2) {
                return (op1 + op2);
                }

                when disassembled, that function looks like this:

                ..method private hidebysig static int32 Add(int32 op1,int32 op2)
                {
                .locals init (int32 retval)
                IL_0000: ldarg.0
                IL_0001: ldarg.1
                IL_0002: add
                IL_0003: stloc.0
                IL_0004: br.s IL_0006

                IL_0006: ldloc.0
                IL_0007: ret
                }

                now here is some code I wrote, which does exactly the same thing:

                ..method private hidebysig static int32 Add(int32 op1,int32 op2)
                {
                ldarg.0 // push op1 on stack
                ldarg.1 // push op2 on stack
                add // add the 2 ops
                ret // return
                }

                that's a ratio of 8 lines vs. my 4 lines. Which one will be faster?

                Chris LaJoie


                Comment

                • Jon Skeet

                  #9
                  Re: Inline MSIL?

                  Chris LaJoie <chris@etriptra der.com> wrote:[color=blue][color=green]
                  > > by the current design/implementation, it is for 99%
                  > > the job of the JITer to optimize code.[/color]
                  >
                  > That doesn't make sense at all. Why would Microsoft choose to have their
                  > code optimized at runtime rather than at compile time?[/color]

                  For one thing, it means that the JIT can act differently depending on
                  the *exact* system it's running on.

                  For another, it means the code can be portable across different
                  platform architectures.

                  For a third, it means that potentially the runtime can recompile parts
                  depending on actual usage - the compiler isn't going to know at
                  compile-time which branch is mostly likely to be taken for any given
                  "if" statement, but a runtime could gather that information and
                  recompile appropriately.
                  [color=blue]
                  > In my case, the C# compiler is making IL for a single function that spans
                  > ~70 lines. I have rewritten that same function myself in IL in 30 lines.
                  > My problem is more complex, but to give you a basic idea of what the C#
                  > compiler makes vs. what I can code myself I'll give you an example:
                  >
                  > here is the C# code for an 'Add' function:[/color]

                  <snip>
                  [color=blue]
                  > that's a ratio of 8 lines vs. my 4 lines. Which one will be faster?[/color]

                  I would expect there to be no difference at runtime, because of the JIT
                  being smart. Have you benchmarked it to show that your version actually
                  *is* faster, as you're implying?

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Jon Skeet

                    #10
                    Re: Inline MSIL?

                    Chris LaJoie <chris@etriptra der.com> wrote:[color=blue]
                    > Thats a good question. I have several methods that must loop through
                    > thousands of objects in an array, and manipulate each of them. When I
                    > disassemble it, I find that C# simply isn't doing it in the most efficient
                    > way, so I'd like to write 3 methods of my class in IL, and the rest in C#.
                    > This apparently isn't possible.[/color]

                    Have you tried putting those other methods in a different assembly just
                    to make sure that your version actually *is* more efficient at runtime?

                    Is this definitely your performance bottleneck in the first place?

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Mattias Sjögren

                      #11
                      Re: Inline MSIL?

                      Chris,
                      [color=blue]
                      >.method private hidebysig static int32 Add(int32 op1,int32 op2)
                      >{
                      > .locals init (int32 retval)
                      > IL_0000: ldarg.0
                      > IL_0001: ldarg.1
                      > IL_0002: add
                      > IL_0003: stloc.0
                      > IL_0004: br.s IL_0006
                      >
                      > IL_0006: ldloc.0
                      > IL_0007: ret
                      >}[/color]

                      Surely you're looking at a non-optimized debug build? Compile with /o
                      and look again, the C# compiler can definitely do better than this.



                      Mattias

                      --
                      Mattias Sjögren [MVP] mattias @ mvps.org

                      Please reply only to the newsgroup.

                      Comment

                      • Lloyd Dupont

                        #12
                        Re: Inline MSIL?

                        BTW it doesn't compile at runtime everytime.
                        there is different policy on when a given method or assembly or method
                        should be compiled (you could choose but I don't remember how) however each
                        method is compiled only once and cached.

                        "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                        news:uu$TZQTdDH A.1656@TK2MSFTN GP10.phx.gbl...[color=blue]
                        > Chris,
                        >[color=green]
                        > >.method private hidebysig static int32 Add(int32 op1,int32 op2)
                        > >{
                        > > .locals init (int32 retval)
                        > > IL_0000: ldarg.0
                        > > IL_0001: ldarg.1
                        > > IL_0002: add
                        > > IL_0003: stloc.0
                        > > IL_0004: br.s IL_0006
                        > >
                        > > IL_0006: ldloc.0
                        > > IL_0007: ret
                        > >}[/color]
                        >
                        > Surely you're looking at a non-optimized debug build? Compile with /o
                        > and look again, the C# compiler can definitely do better than this.
                        >
                        >
                        >
                        > Mattias
                        >
                        > --
                        > Mattias Sjögren [MVP] mattias @ mvps.org
                        > http://www.msjogren.net/dotnet/
                        > Please reply only to the newsgroup.[/color]


                        Comment

                        • Jon Skeet

                          #13
                          Re: Inline MSIL?

                          Lloyd Dupont <net.galador@ld > wrote:[color=blue]
                          > BTW it doesn't compile at runtime everytime.
                          > there is different policy on when a given method or assembly or method
                          > should be compiled (you could choose but I don't remember how) however each
                          > method is compiled only once and cached.[/color]

                          I don't believe that's true. I believe that unless you specifically
                          ngen the assembly, the JITted code won't be stored on disk anywhere.
                          (It's cached within the lifetime of the app, of course, but that's a
                          different matter.)

                          In a future version it *could* cache the code, of course, but I don't
                          believe it does at the moment.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                          If replying to the group, please do not mail me too

                          Comment

                          • Jon Skeet

                            #14
                            Re: Inline MSIL?

                            Chris LaJoie <chris@etriptra der.com> wrote:

                            <snip>
                            [color=blue]
                            > that's a ratio of 8 lines vs. my 4 lines. Which one will be faster?[/color]

                            Just for fun, I *did* actually benchmark this - and to my surprise,
                            your version *was* faster (over 1000000000 iterations, it took about
                            3.5s as opposed to 6.3 for the original). I'm very surprised that the
                            JITter couldn't figure the two out to be identical.

                            However, I got almost exactly the same benefit by moving the hand-
                            optimized code into a separate DLL. It means it needs to be public,
                            which isn't terribly pleasant, but it's unlikely that there will be
                            many routines which actually *do* benefit significantly from this kind
                            of approach (and only those which actually *are* bottlenecks in the
                            app) so this shouldn't be needed very often.

                            --
                            Jon Skeet - <skeet@pobox.co m>
                            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                            If replying to the group, please do not mail me too

                            Comment

                            • Willy Denoyette [MVP]

                              #15
                              Re: Inline MSIL?

                              Jon,
                              See inline ****

                              Willy.

                              "Jon Skeet" <skeet@pobox.co m> wrote in message news:MPG.19c64a c6a48a36d998a4c a@news.microsof t.com...[color=blue]
                              > Lloyd Dupont <net.galador@ld > wrote:[color=green]
                              > > BTW it doesn't compile at runtime everytime.
                              > > there is different policy on when a given method or assembly or method
                              > > should be compiled (you could choose but I don't remember how) however each
                              > > method is compiled only once and cached.[/color]
                              >[/color]

                              *** I guess OP meant, cached in memory which is correct.
                              [color=blue]
                              > I don't believe that's true. I believe that unless you specifically
                              > ngen the assembly, the JITted code won't be stored on disk anywhere.
                              > (It's cached within the lifetime of the app, of course, but that's a
                              > different matter.)
                              >
                              > In a future version it *could* cache the code, of course, but I don't
                              > believe it does at the moment.
                              >
                              > --
                              > Jon Skeet - <skeet@pobox.co m>
                              > http://www.pobox.com/~skeet/
                              > If replying to the group, please do not mail me too[/color]


                              Comment

                              Working...