Interesting Results, StackFrame

M

Mythran

Here is some code the provides some really interesting results! First, read
over the two methods in class 'A' and compare. Just by looking at them,
both results appear to return the EXACT same information the same way. But
the appearances are deceiving! Copy and paste into a console application,
then set the configuration to Release mode :)

using System;
using System.Diagnostics;
using System.Reflection;

namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(A.DoIt1());
Console.WriteLine(A.DoIt2());
}
}

public class A
{
public static string DoIt1()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(0);
MethodBase method = frame.GetMethod();
return method.Name;
}
public static string DoIt2()
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(0);
MethodBase method = frame.GetMethod();
int i = 0; i++;
return method.Name;
}
}
}


Very interesting ... anywho, comments or explanations? :)

Mythran
 
B

Barry Kelly

Mythran said:
Here is some code the provides some really interesting results! [snip]
Very interesting ... anywho, comments or explanations? :)

What you're seeing is inlining. Stack traces in release mode don't
necessarily reflect the true sequence of calls. If any of the stack
frames include code access security attributes, or if they are
moderately complex, then they won't be inlined.

-- Barry
 
M

Mehdi

Here is some code the provides some really interesting results! First, read
over the two methods in class 'A' and compare. Just by looking at them,
both results appear to return the EXACT same information the same way. But
the appearances are deceiving! Copy and paste into a console application,
then set the configuration to Release mode :) [...]
Very interesting ... anywho, comments or explanations? :)

That looks surpring indeed. I suppose that when compiling in Release mode,
the JIT compiler must do some optimization such as inlining the call to
DoIt1, hence the result.
 
M

Mythran

Barry Kelly said:
Mythran said:
Here is some code the provides some really interesting results! [snip]
Very interesting ... anywho, comments or explanations? :)

What you're seeing is inlining. Stack traces in release mode don't
necessarily reflect the true sequence of calls. If any of the stack
frames include code access security attributes, or if they are
moderately complex, then they won't be inlined.

-- Barry

And I don't have code access security attributes applied, and I don't see
how i++ is even considered complex at all...the only difference in the two
methods is one having an "int i = 0; i++;" and the other doesn't...yet the
results of the trace is different...

Mythran
 
M

msdn

Mythran,

What is so interesting

I get the exact result expected,
DoIt1
DoIt2

To really see result use
Console.WriteLine(A.DoIt1());
Console.WriteLine(A.DoIt2());
Console.ReadLine();

Sa
 
B

Barry Kelly

Mythran said:
And I don't have code access security attributes applied, and I don't see
how i++ is even considered complex at all...the only difference in the two
methods is one having an "int i = 0; i++;" and the other doesn't...yet the
results of the trace is different...

Basically, you can't rely on whether or not inlining occurs. That's
about all you can say.

-- Barry
 
B

Barry Kelly

msdn said:
What is so interesting

I get the exact result expected,
DoIt1
DoIt2

Did you compile in both release and debug mode? Or alternatively, on the
command line, try 'csc /optimize+' versus 'csc /optimize-'.

-- Barry
 
B

Barry Kelly

msdn said:
Yes I did in both ways (release and debug) and in both ways I got the same
result.

And you ran both on the command line (i.e. outside the IDE, so the
debugger isn't attached), on .NET 2.0? Interesting.

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top