473,473 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inlining of C# functions

Hello,

Can anybody tell me if the C# compiler will automatically inline simple
functions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call?

Also, am I correct to assume that 180.0/Math.PI will be evaluated at compile
time rather than run time?

Thanks.
Nov 15 '05 #1
10 2331

"Sheila Jones" <sh**********@btopenworld.com> wrote in message
news:Ou*************@TK2MSFTNGP11.phx.gbl...
Hello,

Can anybody tell me if the C# compiler will automatically inline simple
functions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call? It will be compiled as a function call, but the JIT is free to inline the
code it generates. It probably will considering the simplicitly of method.
Also, am I correct to assume that 180.0/Math.PI will be evaluated at compile time rather than run time? I don't know, off hand, although I would suspect so. I'd suggest taking a
trip through ILDasm with a bit of code that does it and see.
Thanks.

Nov 15 '05 #2

Yes, the JIT compiler will inline your method as it sees
fit to inline it UNLESS your class, or one of its base
classes, descends from the System.MarshalByReference
class. The System.MarshalByReference class suppresses all
inlining for descendants...

--Richard
-----Original Message-----
Hello,

Can anybody tell me if the C# compiler will automatically inline simplefunctions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call?

Also, am I correct to assume that 180.0/Math.PI will be evaluated at compiletime rather than run time?

Thanks.
.

Nov 15 '05 #3

"Richard" <an*******@discussions.microsoft.com> wrote in message
news:e4****************************@phx.gbl...

Yes, the JIT compiler will inline your method as it sees
fit to inline it UNLESS your class, or one of its base
classes, descends from the System.MarshalByReference
class. The System.MarshalByReference class suppresses all
inlining for descendants...
That would actually be System.MarshalByRefObject, ;). --Richard
-----Original Message-----
Hello,

Can anybody tell me if the C# compiler will automatically

inline simple
functions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call?

Also, am I correct to assume that 180.0/Math.PI will be

evaluated at compile
time rather than run time?

Thanks.
.

Nov 15 '05 #4
So there is definitely no C# equivalent to the C++ 'inline' (or even
'__forceinline') specifier, to be absolutely sure it's inlined? I don't want
the overhead of a function call just to do a multiplication. The only reason
I've written it as a function is to make the code a bit more understandable.

Could you also clarify another point for me? Does code in a CLR program get
re-compiled each time the program is run, or just the first time?

Thanks.
"Richard" <an*******@discussions.microsoft.com> wrote in message
news:e4****************************@phx.gbl...

Yes, the JIT compiler will inline your method as it sees
fit to inline it UNLESS your class, or one of its base
classes, descends from the System.MarshalByReference
class. The System.MarshalByReference class suppresses all
inlining for descendants...

--Richard
-----Original Message-----
Hello,

Can anybody tell me if the C# compiler will automatically

inline simple
functions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call?

Also, am I correct to assume that 180.0/Math.PI will be

evaluated at compile
time rather than run time?

Thanks.
.

Nov 15 '05 #5

"Sheila Jones" <sh**********@btopenworld.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
So there is definitely no C# equivalent to the C++ 'inline' (or even
'__forceinline') specifier, to be absolutely sure it's inlined? I don't want the overhead of a function call just to do a multiplication. The only reason I've written it as a function is to make the code a bit more understandable. No, there is no such keyword. However, if your method is under 32bytes(I
think) of IL, doesn't use complex flow control(anything other than if), and
sealed\non-virtual, chances are really rather high(possibly guarenteed?)
that the method will be inlined. . Could you also clarify another point for me? Does code in a CLR program get re-compiled each time the program is run, or just the first time?
They are usually JIT compiled on each run, you can run ngen.exe to
pre-compile it, but they pre-compiled image may be thrown out under certain
circumstances. Thanks.
"Richard" <an*******@discussions.microsoft.com> wrote in message
news:e4****************************@phx.gbl...

Yes, the JIT compiler will inline your method as it sees
fit to inline it UNLESS your class, or one of its base
classes, descends from the System.MarshalByReference
class. The System.MarshalByReference class suppresses all
inlining for descendants...

--Richard
-----Original Message-----
Hello,

Can anybody tell me if the C# compiler will automatically

inline simple
functions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call?

Also, am I correct to assume that 180.0/Math.PI will be

evaluated at compile
time rather than run time?

Thanks.
.


Nov 15 '05 #6
Thank you for your reply. It sounds like my function will almost certainly
be inlined.

32 bytes doesn't sound very much though, but I suppose IL is quite compact.
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP11.phx.gbl...

"Sheila Jones" <sh**********@btopenworld.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
So there is definitely no C# equivalent to the C++ 'inline' (or even
'__forceinline') specifier, to be absolutely sure it's inlined? I don't want
the overhead of a function call just to do a multiplication. The only

reason
I've written it as a function is to make the code a bit more

understandable.

No, there is no such keyword. However, if your method is under 32bytes(I
think) of IL, doesn't use complex flow control(anything other than if),

and sealed\non-virtual, chances are really rather high(possibly guarenteed?)
that the method will be inlined. .
Could you also clarify another point for me? Does code in a CLR program get
re-compiled each time the program is run, or just the first time?

They are usually JIT compiled on each run, you can run ngen.exe to
pre-compile it, but they pre-compiled image may be thrown out under

certain circumstances.
Thanks.
"Richard" <an*******@discussions.microsoft.com> wrote in message
news:e4****************************@phx.gbl...

Yes, the JIT compiler will inline your method as it sees
fit to inline it UNLESS your class, or one of its base
classes, descends from the System.MarshalByReference
class. The System.MarshalByReference class suppresses all
inlining for descendants...

--Richard

>-----Original Message-----
>Hello,
>
>Can anybody tell me if the C# compiler will automatically
inline simple
>functions? To be specific, given:
>
>double RadiansToDegrees(double degrees) {
> return degrees*180.0/Math.PI;
>}
>
>will something like
> y = RadiansToDegrees(x);
>be compiled as
> y = x*180.0/Math.PI;
>rather than a function call?
>
>Also, am I correct to assume that 180.0/Math.PI will be
evaluated at compile
>time rather than run time?
>
>Thanks.
>
>
>.
>



Nov 15 '05 #7
Sheila,

This should help:

http://weblogs.asp.net/ricom/archive.../14/58703.aspx

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Sheila Jones" <sh**********@btopenworld.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
So there is definitely no C# equivalent to the C++ 'inline' (or even
'__forceinline') specifier, to be absolutely sure it's inlined? I don't want the overhead of a function call just to do a multiplication. The only reason I've written it as a function is to make the code a bit more understandable.
Could you also clarify another point for me? Does code in a CLR program get re-compiled each time the program is run, or just the first time?

Thanks.
"Richard" <an*******@discussions.microsoft.com> wrote in message
news:e4****************************@phx.gbl...

Yes, the JIT compiler will inline your method as it sees
fit to inline it UNLESS your class, or one of its base
classes, descends from the System.MarshalByReference
class. The System.MarshalByReference class suppresses all
inlining for descendants...

--Richard
-----Original Message-----
Hello,

Can anybody tell me if the C# compiler will automatically

inline simple
functions? To be specific, given:

double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}

will something like
y = RadiansToDegrees(x);
be compiled as
y = x*180.0/Math.PI;
rather than a function call?

Also, am I correct to assume that 180.0/Math.PI will be

evaluated at compile
time rather than run time?

Thanks.
.


Nov 15 '05 #8

"Sheila Jones" <sh**********@btopenworld.com> wrote in message
news:en**************@TK2MSFTNGP09.phx.gbl...
Thank you for your reply. It sounds like my function will almost certainly
be inlined.

32 bytes doesn't sound very much though, but I suppose IL is quite compact. I could be wrong on the exact number, but IL is rather compact. Inlining is
designed for small methods however, basically things that are small enough
that they don't exceed(significantly) the cost of the call. Most likely if
your method is complicated enough that it exceeds 32 bytes, chances are its
too expensive for the method call to matter.

However, I would like to see the JIT be able to inline method calls that are
virtual when possible, I imagine that may come eventually(some Java vm's do
it as I understand).
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP11.phx.gbl...

"Sheila Jones" <sh**********@btopenworld.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
So there is definitely no C# equivalent to the C++ 'inline' (or even
'__forceinline') specifier, to be absolutely sure it's inlined? I
don't want
the overhead of a function call just to do a multiplication. The only

reason
I've written it as a function is to make the code a bit more

understandable.

No, there is no such keyword. However, if your method is under 32bytes(I
think) of IL, doesn't use complex flow control(anything other than if),

and
sealed\non-virtual, chances are really rather high(possibly guarenteed?)
that the method will be inlined. .
Could you also clarify another point for me? Does code in a CLR
program get
re-compiled each time the program is run, or just the first time?

They are usually JIT compiled on each run, you can run ngen.exe to
pre-compile it, but they pre-compiled image may be thrown out under

certain
circumstances.
Thanks.
"Richard" <an*******@discussions.microsoft.com> wrote in message
news:e4****************************@phx.gbl...
>
> Yes, the JIT compiler will inline your method as it sees
> fit to inline it UNLESS your class, or one of its base
> classes, descends from the System.MarshalByReference
> class. The System.MarshalByReference class suppresses all
> inlining for descendants...
>
> --Richard
>
> >-----Original Message-----
> >Hello,
> >
> >Can anybody tell me if the C# compiler will automatically
> inline simple
> >functions? To be specific, given:
> >
> >double RadiansToDegrees(double degrees) {
> > return degrees*180.0/Math.PI;
> >}
> >
> >will something like
> > y = RadiansToDegrees(x);
> >be compiled as
> > y = x*180.0/Math.PI;
> >rather than a function call?
> >
> >Also, am I correct to assume that 180.0/Math.PI will be
> evaluated at compile
> >time rather than run time?
> >
> >Thanks.
> >
> >
> >.
> >



Nov 15 '05 #9
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
However, I would like to see the JIT be able to inline method calls that are
virtual when possible, I imagine that may come eventually(some Java vm's do
it as I understand).


The Hotspot JVM is able to do so, but only because it made the decision
to be able to recompile when situations change (usually when the code
is run frequently, so it's worth applying more aggressive
optimisations, but also when assumptions are invalidated so some
optimisation has to be *undone*).

The current CLR JIT is a "one time" JIT - once the code is generated,
it's never changed as far as I'm aware. This has benefits and
drawbacks, of course, but I suspect it's unlikely to change in the near
future.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
However, I would like to see the JIT be able to inline method calls that are virtual when possible, I imagine that may come eventually(some Java vm's do it as I understand).
The Hotspot JVM is able to do so, but only because it made the decision
to be able to recompile when situations change (usually when the code
is run frequently, so it's worth applying more aggressive
optimisations, but also when assumptions are invalidated so some
optimisation has to be *undone*).

The current CLR JIT is a "one time" JIT - once the code is generated,
it's never changed as far as I'm aware. This has benefits and
drawbacks, of course, but I suspect it's unlikely to change in the near
future.

I'll admit it seems unlikely in the foreseeable future(but with luck .NET
will survive, in some form, as long as java has), but projects like mono and
rotor may produce worthy ideas as well. I doubt that the Hotspot method is
the only way, even if its the only way thats currently practical. Of course,
this will only matter if the .NET market opens up with more vendors than
Mono and MS, its pretty unlikely that a mono change will ever make it up
into the MS.NET market and fairly unlikely that mono will ever be a leader
in functionalitity, however it does give bored developers somethign to play
with.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #11

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

Similar topics

17
by: Tony Vitonis | last post by:
From what I can tell, VB.NET doesn't allow programmers to say explicitly that they want a function to be inlined. Now, I'm a big fan of factoring out duplicate code, but I don't want to do too...
6
by: glen_stark | last post by:
Hi. I'm just curious if there any warnings or caveats or whatever to be aware of when inlining function object calls? If the answer is no, they inline just like everything else, that's good...
55
by: ben | last post by:
is it true that a function without an inline keyword never get inlined? If not true when is it inlined or not? ben
5
by: Raphael | last post by:
Hello, I have 2 files A.c : Defining a bunch of usefull functions B.c : Defining a main that uses these ones. I need to improve perfs : I know that, if the caller and the functions called...
11
by: Elpoca | last post by:
Hi: What rules govern the inlining of templated functions and templated class methods? It has always been my understanding that both templated functions and templated class methods were...
12
by: Tomás | last post by:
The common persuasion is: Big function -- leave it outline. Small function -- make it inline. In code I'm writing at the moment, I have a fairly long function, so it wouldn't cross my...
21
by: LuB | last post by:
How judicious ought one be when inlining small methods. I once read that in general, most compiles will only inline 'one' level. IE: if all the following methods were declared/defined as...
21
by: Michael Hull | last post by:
Hi, I remember from somewhere reading that inlining constructors is a 'BadThing', but now can't seem to find the original source. I can't however thing of a reason why it would be for simple...
8
by: Yakov | last post by:
I'd like a tool that performed inlining of function bodies of which do not appear in the .h file. Really. gcc on Linux. Yakov
5
by: copx | last post by:
Do compilers inline functions even if the programmer could not do it manually because the needed information is hidden at the language level? I am talking about stuff like incomplete types and...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.