By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,465 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,465 IT Pros & Developers. It's quick & easy.

Inlining of C# functions

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a

"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

P: n/a

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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.