.NETGURU
JIT compiler optimizations (inlining threshold too low?)
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.performance.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Ruediger Klaehn
Hi

I noticed that the performance of the JIT optimizer has not improved as much
as I had hoped since 1.1. Delegates are much faster, but especially
inlining and subsequent optimizations still do not happen often enough.

I attached a small example. Its two different methods to do a mandelbrot set
iteration that should run equally fast with a decent JIT optimizer.

The first method uses the operators of the struct and therefore involves
some method and constructor calls. But since they are nonvirtual and
relatively small, they should all be inlined and the unnessecary temporary
structs eliminated. So for a decent JIT compiler the performance of the two
methods should be identical. Unfortunately it is not.

The method using the struct operators is about four times slower
on .NET whidbey!

Whidbey Beta (Debug)
Relying on the JIT to inline: 00:00:05.5079200
Manually inlined like in the bad old days: 00:00:01.4621024

Whidbey Beta (Release)
Relying on the JIT to inline: 00:00:02.1230528
Manually inlined like in the bad old days: 00:00:00.5708208

Mono 0.97 Linux (different machine)
Relying on the JIT to inline: 00:00:01.0744950
Manually inlined like in the bad old days 00:00:00.4676540

I know that there is a tradeoff between JIT optimizations and startup speed.
THe JIT can not spend as much time optimizing the generated code as e.g. a
C++ compiler will, because that would result in very low startup
performance.

But in many cases it is really, really nessecary that the JIT compiler tries
very hard to optimize the code even if it reduces startup performance. I
read somewhere that there is a hard limit on the size of the IL code. If a
method contains more than 32 bytes of IL code it will never be inlined,
even though inlining and subsequent optimizations might reduce these 32
bytes of IL to almost nothing. For some cases the inlining threshold of 32
bytes of IL is just too small.

So I think there should be some (per assembly?) attributes to control how
much effort the JIT puts into inlining and other complex optimizations.
Maybe some per-assembly InliningThresholdAttribute. This could default to a
low value such as 32, but you could manually set it to a higher value for
numerical libraries.

best regards

Rüdiger Klaehn

---

using System;

namespace OperatorInliningTest
{
public struct Complex
{
internal double re, im;
public Complex(double re, double im)
{
this.re = re;
this.im = im;
}
public static implicit operator Complex(double re) {
return new Complex(re, 0);
}
public static Complex I
{
get { return new Complex(0,1); }
}
public static Complex Zero
{
get { return new Complex(0,0); }
}
public static Complex One
{
get { return new Complex(1,0); }
}
public static Complex operator +(Complex a, Complex b)
{
return new Complex(a.re+b.re,a.im+b.im);
}
public static Complex operator *(Complex a, Complex b)
{
return new Complex(a.re*b.re-a.im*b.im,a.re*b.im+a.im*b.re);
}
public double AbsoluteSquared
{
get { return re*re+im*im; }
}
}
class Program
{
static int MandelbrotIteration1(Complex c)
{
Complex x = Complex.Zero;
for (int i = 0; i < 1000; i++)
{
x = x*x+c;
if (x.AbsoluteSquared > 4)
return i;
}
return -1;
}
static int MandelbrotIteration4(Complex c)
{
double xre, xim, cre, cim, t;
xre = 0; xim = 0;
cre = c.re; cim = c.im;
for (int i = 0; i < 1000; i++)
{
//x=x*x...
t = xre * xre - xim * xim;
xim = xre * xim + xre * xim;
xre = t;
//...=c;
xre += cre;
xim += cim;
//if(x.AbsoluteSquared>4)
if (xre * xre + xim * xim > 4)
return i;
}
return -1;
}
static void Main(string[] args)
{
DateTime time0, time1;
TimeSpan delta0, delta1;
Complex x=Complex.Zero;
time0 = DateTime.Now;
for (int i = 0; i < 10000; i++)
MandelbrotIteration1(x);
delta0 = DateTime.Now - time0;
time1 = DateTime.Now;
for (int i = 0; i < 10000; i++)
MandelbrotIteration4(x);
delta1 = DateTime.Now - time1;
Console.WriteLine("Relying on the JIT to inline: {0}", delta0);
Console.WriteLine("Manually inlined like in the bad old days:
{0}", delta1);
Console.ReadLine();
}
}
}
Reply to this message...
 
    
Frank Hileman
I agree with you, we should have some way to force inlining and temp removal
with simple struct functions. A few extra cycles on launch could make a
difference throughout the whole user session.

Regards,
Frank Hileman

check out VG.net: www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor

"Ruediger Klaehn" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Ruediger Klaehn
Frank Hileman wrote:

[Original message clipped]

<lab.msdn.microsoft.com/ProductIT Certification> :

-Methods using valuetype parameters should be eligible for inlining
-Attribute for changing the JIT inlining threshold
-Arithmetic types like int,double,decimal should implement IArithmetic<T>

[Original message clipped]

and PointF value types all the time, so you do not get any inlining
whatsoever. It is just as bad as with complex numbers... :-(

I don't know what inlining is for if not even a simple method such as

public static Point operator + (Point a,Point b)
{
return new Point(a.X+b.X,a.Y+b.Y);
}

is inlined. Stuff like this was acceptable for the first production version
of the .NET JIT, but with the 2005 version it should really be fixed!

regards,

Rüdiger Klaehn
Reply to this message...
 
    
Frank Hileman
Great idea, you got my vote!
Regards,
Frank
"Ruediger Klaehn" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Olaf Baeyens
> I agree with you, we should have some way to force inlining and temp
removal
[Original message clipped]

that that particular function is called a lot.
Dynamic optimizing during running.

But you are right, I prefer my code to launch slower and execute faster.

Reply to this message...
 
    
Mike Swaim
Olaf Baeyens wrote:

[Original message clipped]

Does it actually do this? I know that the Java JITter does, but
haven't seen any evidence that the MSIL one does.

--
Mike Swaim Click here to reveal e-mail address at home | Quote: "Boingie"^4 Y,W & D
MD Anderson Dept. of Biostatistics & Applied Mathematics
Click here to reveal e-mail address or Click here to reveal e-mail address at work
ICBM: 29.763N -95.363W|Disclaimer: Yeah, like I speak for MD Anderson.
Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Mike Swaim <Click here to reveal e-mail address> wrote:
[Original message clipped]

No - the .NET JIT is a one-time JIT. There are various trade-offs
between a one-off JIT and a JIT which can recompile based on usage,
changing assumptions etc.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
 
System.Console
System.DateTime
System.Drawing.Point
System.Drawing.PointF
System.TimeSpan




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification