Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Performance / February 2004

Tip: Looking for answers? Try searching our database.

why won't JIT inline this?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fuzzy - 05 Feb 2004 14:31 GMT
I have written a custom replacement for library function
Math.Pow(double x, double y) for the specific condition of y being
integer and >=0.  It is twice as fast as Math.Pow(), but I cannot get
the compiler to inline compile it. I've tried many different
approaches, but nothing seems to work.   I am debugging in native
mode, and I do see property get's inlined as I would expect.

Any ideas?

C# code, IL code, and the optimized disassembly follow:

C# code:
public static double IntPower(double x, int y)
{
  double r=1;
  while (y>0)
  {
     r*=x;
     y--;
  }
  return r;
}

ILDASM:
.method public hidebysig static float64  IntPower(float64 x,
                                                 int32 y) cil managed
{
 // Code size       27 (0x1b)
 .maxstack  2
 .locals init (float64 V_0)
 IL_0000:  ldc.r8     1.
 IL_0009:  stloc.0
 IL_000a:  br.s       IL_0015
 IL_000c:  ldloc.0
 IL_000d:  ldarg.0
 IL_000e:  mul
 IL_000f:  stloc.0
 IL_0010:  ldarg.1
 IL_0011:  ldc.i4.1
 IL_0012:  sub
 IL_0013:  starg.s    y
 IL_0015:  ldarg.1
 IL_0016:  ldc.i4.0
 IL_0017:  bgt.s      IL_000c
 IL_0019:  ldloc.0
 IL_001a:  ret
} // end of method EMath::IntPower

disassembly:
set and call IntPower() function:
02FD04E5  push        40080000h
02FD04EA  push        0    
02FD04EC  mov         ecx,2
02FD04F1  call        dword ptr ds:[3E6854h] ...

IntPower function:
02FD07B0  fld1            
02FD07B2  cmp         ecx,0
02FD07B5  jle         02FD07C3
02FD07B7  fld         qword ptr [esp+4]
02FD07BB  fmulp       st(1),st
02FD07BD  dec         ecx  
02FD07BE  cmp         ecx,0
02FD07C1  jg          02FD07B7
02FD07C3  ret         8
Jon Skeet [C# MVP] - 05 Feb 2004 15:00 GMT
> I have written a custom replacement for library function
> Math.Pow(double x, double y) for the specific condition of y being
[quoted text clipped - 4 lines]
>
> Any ideas?

I believe the JIT compiler doesn't inline anything involving loops.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Fuzzy - 05 Feb 2004 20:59 GMT
<clipped>

> I believe the JIT compiler doesn't inline anything involving loops.

That's really disappointing.  This loop is so small that it could
easily be inlined.  I'll manually inline it to see how the performance
changes; and also see at what exponent value the calculation time
overwhelms the function call overhead.

Thanks for the response.
Willy Denoyette [MVP] - 05 Feb 2004 16:03 GMT
>I have written a custom replacement for library function
> Math.Pow(double x, double y) for the specific condition of y being
[quoted text clipped - 4 lines]
>
> Any ideas?

The JITter will not inline complex flow controls, complex here means
anything else then if/then/else (in your case  while).

Willy.
Fuzzy - 06 Feb 2004 00:59 GMT
> >I have written a custom replacement for library function
> > Math.Pow(double x, double y) for the specific condition of y being
[quoted text clipped - 9 lines]
>
> Willy.

SO, you think a 'goto' would be inlined?  Something like:

IntPower(double x, int y)
{  r=1;
start:
  if (y>1)
  {   r*=x;
      y--;
      goto start;
  }
  return r;
Jerry III - 06 Feb 2004 05:13 GMT
I think it was said clearly, any flow control other than if/then/else won't
be inlined. Goto is not one of if/then/else.

Jerry

> > >I have written a custom replacement for library function
> > > Math.Pow(double x, double y) for the specific condition of y being
[quoted text clipped - 21 lines]
>    }
>    return r;
Willy Denoyette [MVP] - 06 Feb 2004 12:35 GMT
No, as I said only If/then/else control constructs, nothing that would
involve conditional looping.
Note that in your sample the call overhead is very small compared to the
execution time of the loop, especially when y is large.
Willy.

>> >I have written a custom replacement for library function
>> > Math.Pow(double x, double y) for the specific condition of y being
[quoted text clipped - 21 lines]
>   }
>   return r;
cody - 06 Feb 2004 18:16 GMT
> No, as I said only If/then/else control constructs, nothing that would
> involve conditional looping.

And what about switch-statements?

Signature

cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Willy Denoyette [MVP] - 07 Feb 2004 18:48 GMT
Nope.

Willy.

>> No, as I said only If/then/else control constructs, nothing that would
>> involve conditional looping.
>
> And what about switch-statements?
cody - 07 Feb 2004 23:49 GMT
> >> No, as I said only If/then/else control constructs, nothing that would
> >> involve conditional looping.
> >
> > And what about switch-statements?
>
> Nope.

Why not? a switch is in most cases much faster than if..else if..

Signature

cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk

Jerry III - 08 Feb 2004 04:23 GMT
But in most cases generates lot more code. JIT won't inline anything over 20
instructions.

Jerry

> > >> No, as I said only If/then/else control constructs, nothing that would
> > >> involve conditional looping.
[quoted text clipped - 10 lines]
> [Freeware, Games and Humor]
> www.deutronium.de.vu || www.deutronium.tk
James X. Li - 07 Feb 2004 17:13 GMT
> The JITter will not inline complex flow controls, complex here means
> anything else then if/then/else (in your case  while).
>
> Willy.
Also avoid using and object or class variables in methods you want to
inline.
The following code, for instance, can't be inlined:

class Optimizer {
       static int EnergyType = 1;
       static double Energy(double dx, double dy, double dz, double rp) {
           return   (EnergyType == 1)  ?  rp*Math.Sqrt(1.0/(dx + dy + dz))
: 1.0;
       }
}

CLR JIT can only inline very basic code. We generally inline all our
code by copy-and-paste, it is messy, but makes big difference in our case.

James
visumap.net
ThomasR - 08 Feb 2004 16:53 GMT
: Also avoid using and object or class variables in methods you want to
: inline.
[quoted text clipped - 10 lines]
: CLR JIT can only inline very basic code. We generally inline all our
: code by copy-and-paste, it is messy, but makes big difference in our case.

That's interesting that you can't get this code to inline.  I have had
similar code inlined (though not with static variables).

For example, this type of code inlines in framework 1.0 fine in my app:
(note this is a struct, not a class, if that might make some difference?)

struct XYZ
{
   private int mVal;
   static int GetValue()
   {
       if mVal < 100
           return mVal;
       else
           return SomeMoreComplicatedProcessingRequired();
   }
}
tr - 08 Feb 2004 17:00 GMT
Code in previous post was incorrect.  GetValue() IS NOT a static function.

struct XYZ
{
   private int mVal;
   public int GetValue()
   {
       if mVal < 100
           return mVal;
       else
           return SomeMoreComplicatedProcessingRequired();
   }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.