473,703 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 15 '05 #1
19 4487
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"
<ch***@etriptra der.com> wrote:
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


Nov 15 '05 #2
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" <th************ ***********@hot mail.com> wrote in message
news:67******** *************** *********@4ax.c om...
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"
<ch***@etriptra der.com> wrote:
Is it possible to use 'inline' MSIL (Intermediate Language) similar to howyou can use inline assembly in C/C++? If not, how can I include some codewritten in IL in my project? Thanks,

Chris LaJoie

Nov 15 '05 #3
"Chris LaJoie" <ch***@etriptra der.com> wrote in message
news:eW******** ********@TK2MSF TNGP09.phx.gbl. ..
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,


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
http://www.mag37.com/csharp/
mi**@mag37.com
Nov 15 '05 #4
Michael Mayer <mi**@mag37.com > wrote:
"Chris LaJoie" <ch***@etriptra der.com> wrote:
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,


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?


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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5
> 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


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
Nov 15 '05 #6
> 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/

Nov 15 '05 #7
> by the current design/implementation, it is for 99%
the job of the JITer to optimize code.


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
Nov 15 '05 #8
Chris LaJoie <ch***@etriptra der.com> wrote:
by the current design/implementation, it is for 99%
the job of the JITer to optimize code.
That doesn't make sense at all. Why would Microsoft choose to have their
code optimized at runtime rather than at compile time?


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.
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:
<snip>
that's a ratio of 8 lines vs. my 4 lines. Which one will be faster?


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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #9
Chris LaJoie <ch***@etriptra der.com> wrote:
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.


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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4578
by: Raymond Lewallen | last post by:
Is there some article out there that helps in reading MSIL? I've been looking but can't find anything, and I'm probably just looking in the wrong spots for the wrong keywords. I understand mov, call, jmp, ptr, dword and some of the other obvious pieces. I'm looking to understand some of the less obvious pieces of MSIL so I can better understand how my code is being constructed in release mode. TIA, Raymond Lewallen
7
1749
by: Raymond Lewallen | last post by:
Microsoft, Can I make a request for a newsgroup specific to MSIL? I have taken great interest in MSIL and have done a lot of reading into MSIL, and written, compiled and tested with success a few il programs. I'm sure this is a topic of interest to many people, as it is at the heart of .Net. If any of you readers/posters are also interested in an intermediate language specific newsgroup, please respond.
6
2949
by: TrickyDicky | last post by:
Hi all, I'm just starting to experiment with MSIL but am getting confused about c# and MSIL integration. I understand that c# is compiled into MSIL before it is JIT'ed and then executed. I also understand that it is possible for me to generate my own MSIL using the System.Reflection.Emit namespace. However, is it possible for my c# code to call into my pre-MSIL-compiled code, passing and returning values?
6
2002
by: Pawel | last post by:
Ary you know tools to convert MSIL code (Assembly) to Win32 (not Just In Time)?
6
2418
by: ThomasR | last post by:
I was wondering on the breadth of optimization with .NET JIT compiler. Let's presume I have an assembly of static classes - mostly helper functions. Some of these functions may be very small (small enough that the optimizing compiler would inline them). If I have another assembly that references this helper libary assembly, could .NET's JIT compiler inline compile the usage of these small helper functions?
14
2681
by: Teis Draiby | last post by:
I am about to move my C++ application to C#. Parts of my previous application consisted of inline SSE2 assembly code optimized for the P4 processor. What options do I have to integrate that part into my C# application? There are no such things as intrinsics support in C#, right? Thank you very much! regards, Teis
4
19126
by: James dean | last post by:
My understanding is the MSIL runs on the CLR and the CLR is basically the JIT compiler plus Garbage collection. This part "MSIL runs on the CLR" is a bit vague to me can anyone give me a clearer definition and explain how these 3 work together. *** Sent via Developersdex http://www.developersdex.com ***
3
3389
by: NigelW | last post by:
Clarification needed please. If I compile a C++ program with the /clr option inpsection of the resulting assembly with ILDASM shows MSIL even for methods in classes for which I have not specified garbage collection with __gc. Am I correct in thinking there is really two kinds of managed (i.e. MSIL) code from C++, that which uses garbage collection and that which does not?
1
1819
by: John Doe | last post by:
Hi all, I have a lot of confusion about what this runtime environment is. When I write an application with unmanaged code and unmanaged data, can I compile it to the MSIL, or it will compile mandatorily to the x86 instruction set?
0
9089
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8941
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6575
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5910
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4412
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2037
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.