public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: John Fine <johnsfine@verizon.net>
To: VM <mayeski@gmail.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: how to pass params to inline functions by reference or value?
Date: Thu, 10 Apr 2008 15:33:00 -0000	[thread overview]
Message-ID: <47FE27E0.1020205@verizon.net> (raw)
In-Reply-To: <1207801566.12695.8.camel@vm-laptop>

Your second version tells the compiler to use atype_t's copy constructor 
to make a copy of the object and then get a and b from that copy.

After earlier compiler steps generate an intermediate level 
representation of that copy constructor, the optimizer might well figure 
out that all the copying is redundant and eliminate it and generate the 
minimal code which you want.

All that assumes the compiler really does take your suggestion of 
"inline".  As someone else already mentioned, "inline" is just a 
suggestion.  If the code weren't inlined then the optimizer could not 
avoid the copy operation.

I think it is cleanest to pass by const reference, assuming the real 
code, like your reduced example, only reads the structure, and assuming 
we are talking about C++, not C.

inline int
sum(atype_t const& x)
{
  return x.a + x.b;
}

That gives the compiler the least opportunity to generate bad code.  
Maybe all versions discussed will generate equal code, but why push your 
luck.


VM wrote:

>Hello,
>
>I'm trying to decide on the best way to pass parameters to inline
>function. For example, the two functions below:
>
>inline int
>sum(atype_t *x)
>{
>  return x->a + x->b;
>}
>
>and
>
>inline int
>sum(atype_t x)
>{
>  return x.a + x.b;
>}
>
>Since the function is inline, my guess is that the compiler will
>generate identical code for both. So there should be no performance
>difference.
>
>Is this assumption correct?
>
>V
>
>
>  
>

  parent reply	other threads:[~2008-04-10 14:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-10  5:55 VM
2008-04-10 13:32 ` Ted Byers
2008-04-10 13:37   ` Vincent Mayeski
2008-04-10 14:45     ` Ted Byers
2008-04-10 15:11     ` Eljay Love-Jensen
2008-04-10 15:37       ` Vincent Mayeski
2008-04-10 21:39         ` Tony Wetmore
2008-04-10 15:33 ` John Fine [this message]
2008-04-11 11:57   ` Brian Dessent

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=47FE27E0.1020205@verizon.net \
    --to=johnsfine@verizon.net \
    --cc=gcc-help@gcc.gnu.org \
    --cc=mayeski@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).