Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Question for compiler gurus

Author: Dan Honeycutt

Date: 10:02:10 10/01/04

Go up one level in this thread


On October 01, 2004 at 12:15:11, Reinhard Scharnagl wrote:

>On October 01, 2004 at 11:48:25, Dan Honeycutt wrote:
>
>>On October 01, 2004 at 11:14:43, Reinhard Scharnagl wrote:
>>
>>>On October 01, 2004 at 10:58:43, Dan Honeycutt wrote:
>>>
>>>>If I have an inline function, ie:
>>>>
>>>>_inline int Distance(int square1, int square2)
>>>>
>>>>which is going to return the max rank/file difference but is not going to alter
>>>>the arguements, would I be better off passing the arguements by reference, ie:
>>>>
>>>>_inline int Distance(int & square1, int & square2)
>>>>
>>>>so as to save having to make a copy of the arguements.  Or would the compiler
>>>>figure that out on it's own since the function is inline?  I'm using MS VC 6.
>>>>
>>>
>>>Well I am not a guru, but taking an int not by value does only make sense if
>>>the original data should be changed. Copying an address could never be faster
>>>than copying an int. And a good compiler will work within inline parts with the
>>>original variables not with copies. But may be I have not seen your problem yet.
>>>
>>>Reinhard.
>>
>>Hi Reinhard:
>>Let me clarify a bit.  The body of Distance() looks like:
>>
>>return Max(RankDiff(square1, square2), FileDiff(square1, square2));
>>
>>The calling function looks like:
>>
>>square1 = ...
>>square2 = ...
>>d = Distance(square1, square2);
>>
>>When the function is inlined I want:
>>
>>square1 = ...
>>square2 = ...
>>d = Max(RankDiff(square1, square2), FileDiff(square1, square2));
>>
>>But do I get
>>
>>square1 = ...
>>square2 = ...
>>copy1 = square1;
>>copy2 = square2;
>>d = Max(RankDiff(copy1, copy2), FileDiff(copy1, copy2));
>>
>>And if i do get the latter form, would a pass by reference be better?
>
>If the compiler would be intelligent, it would make no unnecessary copies. But
>when using some variables very often, copies cannot be avoided.
>
>When using references here (what of course will only have influence when the
>compiler does not follow the inline directive, e.g. when targeting a debug
>version) two bad things will happen:
>
>a) the optimizer may not notice that those variables are used that often and
>errornously put other variables into registers instead of the probably often
>used two square1 and square2,
>
>b) the not inline function will have double access efforts to work with your
>values, because it has to fetch first their address and then their value.
>
>Therfore it is hard to see how referencing ints (which should not be changed)
>ever could raise any advantage.
>
>Reinhard.

Mario and Andy say the same - pass by reference is at best break even so I'll
stick with by value.  Thanks to all.

Dan H.




This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.