www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/12/18:07:41

Message-Id: <3.0.6.32.19990212180551.00841100@pop.netaddress.com>
X-Sender: pderbysh AT pop DOT netaddress DOT com
X-Mailer: QUALCOMM Windows Eudora Light Version 3.0.6 (32)
Date: Fri, 12 Feb 1999 18:05:51 -0500
To: djgpp AT delorie DOT com, egcs AT egcs DOT cygnus DOT com
From: Paul Derbyshire <pderbysh AT usa DOT net>
Subject: Code gen question
Mime-Version: 1.0
Reply-To: djgpp AT delorie DOT com

Which will cause cc1plus to generate better code?

inline int myclass::myfunc (int j) { return j*j*j; }


inline int myclass::myfunc (const int &j) { return j*j*j; }


My guess would be the latter, since the latter when inlined won't make a
copy of the argument passed. However, it might be that at high -O settings
cc1plus will spot that the first version doesn't modify j and silently
compile it like the second version.
If so, this leads me to ask: under what circumstances will the compiler be
smart enough to detect that an inline function passed an argument of a
builtin type doesn't modify it and avoid making an unnecessary copy?

This leads me to ask: when writing short inline functions, is it better for
code optimization to pass builtin data types (bool, int, double, etc.) and
pointers by value or by reference? (Yuck, passing pointers by reference,
well I'll do it if it means real speed gains in tiny inline functions that
get invoked a great deal.)

-- 
   .*.  "Clouds are not spheres, mountains are not cones, coastlines are not
-()  <  circles, and bark is not smooth, nor does lightning travel in a
   `*'  straight line."    -------------------------------------------------
        -- B. Mandelbrot  |http://surf.to/pgd.net
_____________________ ____|________     Paul Derbyshire     pderbysh AT usa DOT net
Programmer & Humanist|ICQ: 10423848|

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019