473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

C# and compiler optimizer.

I am trying to understand the IL assembler created by C# but as far as I can
see, there is no optimizing done by the C# compiler.
Optimizing is done by the JIT, but it can only go so far.

In C++ you can put the 'inline' keyword for properties and methods that
could be inserted into the generated code so you avoid a call and thus the
overhead, but as far as I can see, C# does not seem to have this
functionality.

Or is it because I use the VC# 2003 standard edition that this optimizing is
not done?
Nov 17 '05 #1
9 3743
I am trying to understand the IL assembler created by C# but as far as I can
see, there is no optimizing done by the C# compiler.
Optimizing is done by the JIT, but it can only go so far.
The C# compiler does some optimizations itself if you compile with
/o+, but you're right that most of it is done by the JIT compiler.

In C++ you can put the 'inline' keyword
And the compiler is free to ignore it.

but as far as I can see, C# does not seem to have this
functionality.
Right, but the JIT compiler will inline some method calls.

Or is it because I use the VC# 2003 standard edition that this optimizing is
not done?


No.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2
Olaf Baeyens wrote:
I am trying to understand the IL assembler created by C# but as far as I can
see, there is no optimizing done by the C# compiler.
Optimizing is done by the JIT, but it can only go so far.
<theory>
A JIT can just start by running the optimizations a traditional compiler
would do. Offline compilers can never be more "optimizing" than JIT.
</theory>
In C++ you can put the 'inline' keyword for properties and methods that
could be inserted into the generated code so you avoid a call and thus the
overhead, but as far as I can see, C# does not seem to have this
functionality.


Even in C++, "inline" is a linkage-specifier, not a specification of
inling. The c++ compiler can choose to emit it as a separate function
and call-instructions, but the function must have internal linkage.

You can easily verify this by compiling a recursive function:

inline int f(int x) { return f(x+1); }
int main(int argc, string** argv) { return f(0); }

Compilers (and some JIT's) today have *very* advanced analysis of when
inlining should be applied and when it should not.

Which specific performance problem do you have which can be solved by
letting you specify inlining?

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3
> >I am trying to understand the IL assembler created by C# but as far as I
can
see, there is no optimizing done by the C# compiler.
Optimizing is done by the JIT, but it can only go so far.


The C# compiler does some optimizations itself if you compile with
/o+, but you're right that most of it is done by the JIT compiler.

In C++ you can put the 'inline' keyword


And the compiler is free to ignore it.

True in a lot of cases, the C++ compiler just doesn't inline this part if I
look at my code.
but as far as I can see, C# does not seem to have this
functionality.


Right, but the JIT compiler will inline some method calls.

I did not notice this before, but it would be nice. :-)
Nov 17 '05 #4
> Compilers (and some JIT's) today have *very* advanced analysis of when
inlining should be applied and when it should not.

Which specific performance problem do you have which can be solved by
letting you specify inlining?
Doing vector calculation, a few millions of them in one pass. :-)
Those vectors have public properties X, Y and Z that (looking at IL asm)
appears not to be inline even though they are just stupid float without any
additional coding.
But for the moment I have nothing to compare to yet. I am still developing
the code. But it would be nice to know these things before I go too deep in
the creation of the code and discover that is slows down too much. Then I am
going to look at the generated x86 code to see what the JIT did do.
<theory>
A JIT can just start by running the optimizations a traditional compiler
would do. Offline compilers can never be more "optimizing" than JIT.
</theory>
Yes assuming they have tons of time to analyze your program running and
optimize most used functions even further dynamically.
Somehow it would be nice if a second compiled copy of your program could be
stored on the harddisk. And every time it gets loaded, it gets optimized
even further, so the more you use the program the faster it becomes. But I
guess that is not for tomorrow. :-)
Compilers (and some JIT's) today have *very* advanced analysis of when
inlining should be applied and when it should not.


One of the things I am wondering is does you get a performance loss if you
use a class in one assembly that is defined in another assembly?
The assemblies are dll's, and I do know that dll's creates overhead in the
transition from one executable into your dll function.
Nov 17 '05 #5
Olaf Baeyens wrote:
Which specific performance problem do you have which can be solved by
letting you specify inlining?
Doing vector calculation, a few millions of them in one pass. :-)
Aaah, you're actully doing something -- not just reacting to GUI ;)

Try to do some timings on it, it may be as fast as native, or it may not...

If it is slow, you could try to factor that part of the code out, so you
can reimplement it natively when performance starts rearing it's ugly head.
Those vectors have public properties X, Y and Z that (looking at IL asm)
appears not to be inline even though they are just stupid float without any
additional coding.
Why are you using properties? are you implementing/inheriting an
interface/class?

The exported code has to have the public properties, for other code to
be able to acces them, they are public :)

The JIT might inline the access while running, if it's called a lot.
the creation of the code and discover that is slows down too much. Then I am
going to look at the generated x86 code to see what the JIT did do.
Generally, doing a test with timings is a very informative and cheap way
to evaluate performance.
<theory>
A JIT can just start by running the optimizations a traditional compiler
would do. Offline compilers can never be more "optimizing" than JIT.
</theory>

Yes assuming they have tons of time to analyze your program running and


notice the "<theory/>" ;)
optimize most used functions even further dynamically.
Compilers (and some JIT's) today have *very* advanced analysis of when
inlining should be applied and when it should not.

One of the things I am wondering is does you get a performance loss if you
use a class in one assembly that is defined in another assembly?
Try to make some timings, it won't take long :) I wouldn't expect any
difference.
The assemblies are dll's, and I do know that dll's creates overhead in the
transition from one executable into your dll function.


But this is JIT, and the resolution of the just-address-resolution
*could* be done just once, when the code is jit-compiled and inserted
literally into the compiled version of the code. Try and time it.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #6
> >>Which specific performance problem do you have which can be solved by
letting you specify inlining?

Doing vector calculation, a few millions of them in one pass. :-)


Aaah, you're actully doing something -- not just reacting to GUI ;)

I am implementing a scene library in OpenGL. Pretty complicated stuf to be
used for visualizing CT images.
It works darn good, only users don't want to install the .NET framework, so
I will probably end up having to do it the unmanaged C++ way. But right now
I am using the C# way because I speed up development, and porting to C++ is
pretty easy to do, and I still have time left then I would have done it in
C++ directly.

I use the NUnit for testing my mathematics things for accuracy. I love it
very much, since it speeds up testing dramatically. :-)
Nov 17 '05 #7
On Fri, 15 Apr 2005 09:25:15 +0200, "Olaf Baeyens"
<ol**********@skyscan.be> wrote:
Doing vector calculation, a few millions of them in one pass. :-)
Those vectors have public properties X, Y and Z that (looking at IL asm)
appears not to be inline even though they are just stupid float without any
additional coding.


Inlining of trivial property getters/setters is done by the JIT
compiler, so that's actually not a problem.

However, your project sounds as if it could profit from some
higher-level optimizations which the C#/VB and JIT compilers aren't
doing, neither in this nor in the next version -- the MS teams are too
busy implementing new features, I suppose.

That leaves the C++ compiler which is the only one that emits
optimized IL code. Unfortunately the current version of C++ for the
..NET Framework (called "Managed C++") is pretty ugly but .NET 2.0 will
ship with a much improved version called "C++/CLI".
--
http://www.kynosarges.de
Nov 17 '05 #8
> However, your project sounds as if it could profit from some
higher-level optimizations which the C#/VB and JIT compilers aren't
doing, neither in this nor in the next version -- the MS teams are too
busy implementing new features, I suppose.

That leaves the C++ compiler which is the only one that emits
optimized IL code. Unfortunately the current version of C++ for the
.NET Framework (called "Managed C++") is pretty ugly but .NET 2.0 will
ship with a much improved version called "C++/CLI".

I am already using mixed managed/unmanaged C++ :-)
But the code I create now is pure C#, because I can test it faster (much
faster compiler) and create a fully functional version. So I can port it to
unmanaged C++ without losing the time to test too deep. Only testing if I
get the same results, but functional testing is already done ine C#.

At the same time I have already code available to fully managed and create
one executable that will both run on 32 and 64 bit while taking the full
advantage of 64 bit when The VS 2005 comes available.

So it is a strategy to lower the development cycle by prototyping in C#, and
preparing the code for the future at the same time. :-)
Nov 17 '05 #9
> However, your project sounds as if it could profit from some
higher-level optimizations which the C#/VB and JIT compilers aren't
doing, neither in this nor in the next version -- the MS teams are too
busy implementing new features, I suppose.


The current scuttlebutt is an IL optimizer instead of individual
optimizations in the compilers, saving everyone time. No clue if it'll
actually happen or not though, or what kind of context would be lost.
Nov 17 '05 #10

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

Similar topics

34
by: Nikola Skoric | last post by:
Is the a PHP compiler? A program that compiles PHP code to executable which doesn't need php interpreter to execute... -- Pozdrav/Regards, Nikola Skoric. "...Usne, tice-rugalice - a u oku...
9
by: hemal | last post by:
I came across a very strange situation at work. There is an order of magnitude difference in execution time for the following two queries (10 v/s ~130 msec): select count(*) from table_name...
6
by: J.D. Herron via .NET 247 | last post by:
Just wanted to throw out there a VC7.1 compiler bug when usingwarning level 4. The following code will demonstrate theproblem: class TestClass { public: // Initialized float member,...
44
by: Don Kim | last post by:
Ok, so I posted a rant earlier about the lack of marketing for C++/CLI, and it forked over into another rant about which was the faster compiler. Some said C# was just as fast as C++/CLI, whereas...
30
by: lovecreatesbea... | last post by:
K&R says the following in the preface to the first edition, "... the C compiler, and ... are written in C." I'm wondering, does it say even the first / original C compiler was written in C?
3
by: aj | last post by:
DB2 LUW v8.2 FP 14 RHAS 2.1 Sorry if these are newbie questions. Optimizer stuff is black magic to me. For both of these, assume stats are current and an even distribution of data....
42
by: jacob navia | last post by:
http://slashdot.org/ "The leaner, lighter, faster, and most importantly, BSD Licensed, Compiler PCC has been imported into OpenBSD's CVS and NetBSD's pkgsrc. The compiler is based on the...
29
by: sammy | last post by:
Word up! If there are any gcc users here, maybe you could help me out. I have a program and I've tried compiling it with -O2 and -O3 optimization settings. The wired thing is that it actually...
11
by: Pablo Suarez | last post by:
In all my previous C++ projects I used the gcc as compiler. For some reasons I want to try out the Sun compiler instead. How can I install and use this compiler instead? Is the Sun C++...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.