Connecting Tech Pros Worldwide Forums | Help | Site Map

Performance question regarding inlining of very simple struct methods

Fernando Cacciola
Guest
 
Posts: n/a
#1: Nov 15 '05
Hi, I'm terribly new at C# (come from C++ land).
I'm making some benchmarks to see the effect of different coding styles, and
I run across a situation that strikes me as pretty odd.

For my image processing code I would like to use low-level "Iterators", as I
would in C++, so I have a struct of the form:

public unsafe struct Col
{
public Col ( byte* ptr ) { mPtr = ptr ; }
static public bool Same( Col a, Col b ) { return a.mPtr == b.mPtr ; }
public void MoveRight() { mPtr += 3 ; }
public byte* mPtr ;
}

And a loop that looks like:

while ( !Col.Same(col_beg,col_end) )
//while ( col_beg.mPtr != col_end.mPtr )
{
.... some code here...
col_beg.MoveRight();
//col_beg.mPtr += 3 ;
}

I'm compiling with Optimizations on, (with TRACE but no DEBUG) and using
QueryPerformanceCounter() to measure execution time in ms.

If, for the while expression, I use the method 'Same', it takes about 2.7
seconds to complete a loop, but if I use direct comparison (see the
commented code right below), it takes about .58 seconds.
Likewise, using MoveRight() instead of the direct pointer addition takes 2.2
seconds with the direct pointer comparison for the while (that is, using the
configuration that took .58s and replacing only MoveRight())

Both MoveRight() and Same() are the kind of simple methods that I would have
expected the JIT to inline, so I'm kind of furstated by these results. Is
there any compiler options or switch or keyword I should have used?

TIA

Fernando Cacciola



mikeb
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Performance question regarding inlining of very simple struct methods


Fernando Cacciola wrote:
[color=blue]
> Hi, I'm terribly new at C# (come from C++ land).
> I'm making some benchmarks to see the effect of different coding styles, and
> I run across a situation that strikes me as pretty odd.
>
> For my image processing code I would like to use low-level "Iterators", as I
> would in C++, so I have a struct of the form:
>
> public unsafe struct Col
> {
> public Col ( byte* ptr ) { mPtr = ptr ; }
> static public bool Same( Col a, Col b ) { return a.mPtr == b.mPtr ; }
> public void MoveRight() { mPtr += 3 ; }
> public byte* mPtr ;
> }
>
> And a loop that looks like:
>
> while ( !Col.Same(col_beg,col_end) )
> //while ( col_beg.mPtr != col_end.mPtr )
> {
> .... some code here...
> col_beg.MoveRight();
> //col_beg.mPtr += 3 ;
> }
>
> I'm compiling with Optimizations on, (with TRACE but no DEBUG) and using
> QueryPerformanceCounter() to measure execution time in ms.
>
> If, for the while expression, I use the method 'Same', it takes about 2.7
> seconds to complete a loop, but if I use direct comparison (see the
> commented code right below), it takes about .58 seconds.
> Likewise, using MoveRight() instead of the direct pointer addition takes 2.2
> seconds with the direct pointer comparison for the while (that is, using the
> configuration that took .58s and replacing only MoveRight())
>
> Both MoveRight() and Same() are the kind of simple methods that I would have
> expected the JIT to inline, so I'm kind of furstated by these results. Is
> there any compiler options or switch or keyword I should have used?[/color]

Are you running your tests inside of VS.NET? If so, the JIT is not
performing any optimizations, even when you've compiled with
optimizations and you're not using the debugger.

Rerun your tests outside of VS.NET, see if that makes any difference.
[color=blue]
>
> TIA
>
> Fernando Cacciola
>
>[/color]

--
mikeb

Fernando Cacciola
Guest
 
Posts: n/a
#3: Nov 15 '05

re: Performance question regarding inlining of very simple struct methods



"mikeb" <mailbox.google@mailnull.com> escribió en el mensaje
news:uiVnLSdmDHA.2616@TK2MSFTNGP11.phx.gbl...[color=blue]
> Fernando Cacciola wrote:
>[color=green]
> > Hi, I'm terribly new at C# (come from C++ land).
> > I'm making some benchmarks to see the effect of different coding styles,[/color][/color]
and[color=blue][color=green]
> > I run across a situation that strikes me as pretty odd.
> >
> > For my image processing code I would like to use low-level "Iterators",[/color][/color]
as I[color=blue][color=green]
> > would in C++, so I have a struct of the form:
> >
> > public unsafe struct Col
> > {
> > public Col ( byte* ptr ) { mPtr = ptr ; }
> > static public bool Same( Col a, Col b ) { return a.mPtr == b.mPtr[/color][/color]
; }[color=blue][color=green]
> > public void MoveRight() { mPtr += 3 ; }
> > public byte* mPtr ;
> > }
> >
> > And a loop that looks like:
> >
> > while ( !Col.Same(col_beg,col_end) )
> > //while ( col_beg.mPtr != col_end.mPtr )
> > {
> > .... some code here...
> > col_beg.MoveRight();
> > //col_beg.mPtr += 3 ;
> > }
> >
> > I'm compiling with Optimizations on, (with TRACE but no DEBUG) and using
> > QueryPerformanceCounter() to measure execution time in ms.
> >
> > If, for the while expression, I use the method 'Same', it takes about[/color][/color]
2.7[color=blue][color=green]
> > seconds to complete a loop, but if I use direct comparison (see the
> > commented code right below), it takes about .58 seconds.
> > Likewise, using MoveRight() instead of the direct pointer addition takes[/color][/color]
2.2[color=blue][color=green]
> > seconds with the direct pointer comparison for the while (that is, using[/color][/color]
the[color=blue][color=green]
> > configuration that took .58s and replacing only MoveRight())
> >
> > Both MoveRight() and Same() are the kind of simple methods that I would[/color][/color]
have[color=blue][color=green]
> > expected the JIT to inline, so I'm kind of furstated by these results.[/color][/color]
Is[color=blue][color=green]
> > there any compiler options or switch or keyword I should have used?[/color]
>
> Are you running your tests inside of VS.NET? If so, the JIT is not
> performing any optimizations, even when you've compiled with
> optimizations and you're not using the debugger.
>
> Rerun your tests outside of VS.NET, see if that makes any difference.
>[/color]
Ah! It must be that.. I run the code from within VS.NET.

Thanks!

Fernando Cacciola



Michael Culley
Guest
 
Posts: n/a
#4: Nov 15 '05

re: Performance question regarding inlining of very simple struct methods


"Fernando Cacciola" <fernando_cacciola@hotmail.com> wrote in message news:OlVY5ddmDHA.424@TK2MSFTNGP10.phx.gbl...[color=blue]
> Ah! It must be that.. I run the code from within VS.NET.[/color]

Did that make a difference?

--
Michael Culley


Fernando Cacciola
Guest
 
Posts: n/a
#5: Nov 15 '05

re: Performance question regarding inlining of very simple struct methods



"Michael Culley" <mculley@NOSPAMoptushome.com.au> escribió en el mensaje
news:O1doLAemDHA.2432@TK2MSFTNGP10.phx.gbl...[color=blue]
> "Fernando Cacciola" <fernando_cacciola@hotmail.com> wrote in message[/color]
news:OlVY5ddmDHA.424@TK2MSFTNGP10.phx.gbl...[color=blue][color=green]
> > Ah! It must be that.. I run the code from within VS.NET.[/color]
>
> Did that make a difference?
>[/color]
Not really....

Simply changing this
while ( col_beg.mPtr != col_end.mPtr )

with this
while ( !Col.Same(col_beg,col_end) )

makes the ellapsed time rise from about 2400 to 4400...

with

static public bool Same ( Col a, Col b ) { return a.mPtr != b.mPtr ; }

(I tried with 'ref' parameters too but that made no difference)

Any other idea? Or is it just that the JIT can't inline the simplest of the
functions?

Fernando Cacciola




mikeb
Guest
 
Posts: n/a
#6: Nov 15 '05

re: Performance question regarding inlining of very simple struct methods


Fernando Cacciola wrote:[color=blue]
> "Michael Culley" <mculley@NOSPAMoptushome.com.au> escribió en el mensaje
> news:O1doLAemDHA.2432@TK2MSFTNGP10.phx.gbl...
>[color=green]
>>"Fernando Cacciola" <fernando_cacciola@hotmail.com> wrote in message[/color]
>
> news:OlVY5ddmDHA.424@TK2MSFTNGP10.phx.gbl...
>[color=green][color=darkred]
>>>Ah! It must be that.. I run the code from within VS.NET.[/color]
>>
>>Did that make a difference?
>>[/color]
>
> Not really....
>
> Simply changing this
> while ( col_beg.mPtr != col_end.mPtr )
>
> with this
> while ( !Col.Same(col_beg,col_end) )
>
> makes the ellapsed time rise from about 2400 to 4400...[/color]

It seems to have a made a difference comparing numbers that you posted
in your first post and here. In your first post you were describing
timings that differed approximately 400% (2.7 vs. .58 seconds and 2.2
vs. .58 seconds). In this post, the numbers indicate about a 200%
difference.

I understand that this is still not as close as it should be, but it is
an indication that running outside of the VS.NET environment does make a
difference on the JIT output.
[color=blue]
>
> with
>
> static public bool Same ( Col a, Col b ) { return a.mPtr != b.mPtr ; }
>[/color]

I assume that the code for Same() is actually:

static public bool Same ( Col a, Col b ) { return a.mPtr == b.mPtr ; )

[color=blue]
> (I tried with 'ref' parameters too but that made no difference)
>
> Any other idea? Or is it just that the JIT can't inline the simplest of the
> functions?
>[/color]

Finally, in looking over your original post, I noticed this time that
your Col struct is 'unsafe'. I wonder if the JIT does not inline unsafe
code, even if it could correctly do so.
[color=blue]
> Fernando Cacciola
>
>
>
>[/color]

--
mikeb

Fernando Cacciola
Guest
 
Posts: n/a
#7: Nov 15 '05

re: Performance question regarding inlining of very simple struct methods



"mikeb" <mailbox.google@mailnull.com> escribió en el mensaje
news:OQIe3rmmDHA.1408@TK2MSFTNGP11.phx.gbl...[color=blue]
> Fernando Cacciola wrote:[color=green]
> > "Michael Culley" <mculley@NOSPAMoptushome.com.au> escribió en el mensaje
> > news:O1doLAemDHA.2432@TK2MSFTNGP10.phx.gbl...
> >[color=darkred]
> >>"Fernando Cacciola" <fernando_cacciola@hotmail.com> wrote in message[/color]
> >
> > news:OlVY5ddmDHA.424@TK2MSFTNGP10.phx.gbl...
> >[color=darkred]
> >>>Ah! It must be that.. I run the code from within VS.NET.
> >>
> >>Did that make a difference?
> >>[/color]
> >
> > Not really....
> >
> > Simply changing this
> > while ( col_beg.mPtr != col_end.mPtr )
> >
> > with this
> > while ( !Col.Same(col_beg,col_end) )
> >
> > makes the ellapsed time rise from about 2400 to 4400...[/color]
>
> It seems to have a made a difference comparing numbers that you posted
> in your first post and here. In your first post you were describing
> timings that differed approximately 400% (2.7 vs. .58 seconds and 2.2
> vs. .58 seconds). In this post, the numbers indicate about a 200%
> difference.
>[/color]
Yes, very good point.
[color=blue]
> I understand that this is still not as close as it should be, but it is
> an indication that running outside of the VS.NET environment does make a
> difference on the JIT output.
>[/color]
Right. I expected to see a similar performance, but as you say, there's no
doubt that running outside the IDE makes a big difference.
[color=blue][color=green]
> >
> > with
> >
> > static public bool Same ( Col a, Col b ) { return a.mPtr != b.mPtr ; }
> >[/color]
>
> I assume that the code for Same() is actually:
>
> static public bool Same ( Col a, Col b ) { return a.mPtr == b.mPtr ; )
>[/color]
oh, yes, of course
[color=blue]
>[color=green]
> > (I tried with 'ref' parameters too but that made no difference)
> >
> > Any other idea? Or is it just that the JIT can't inline the simplest of[/color][/color]
the[color=blue][color=green]
> > functions?
> >[/color]
>
> Finally, in looking over your original post, I noticed this time that
> your Col struct is 'unsafe'. I wonder if the JIT does not inline unsafe
> code, even if it could correctly do so.
>[/color]
Good point.
I'll keep looking.
Maybe there are other factors like GC or so being involved.
I'll try to code a better benchmark interleaving both versions to account
for GC interruptions and other subtelties.

I'll get back with the new results.

Thanks

Fernando Cacciola


Fernando Cacciola
Guest
 
Posts: n/a
#8: Nov 15 '05

re: Performance question regarding inlining of very simple struct methods


Well, it turned out that there was something getting in the way of my
previous benchmarks.
I rewrote it, and running from outside VS.NET I could see that there's no
performance penalty for such tiny easy to inline functions after all.
I tried with properties (both get/set), static/non-static methods and
trivial constructors.: they all seems to be properly inlined as I expected.

Thanks

Fernando Cacciola



Closed Thread