473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Will this be hard/expensive to inline?

I don't really understand the details of what happens when a compiler
inlines a given function. I know that basically it's supposed to cause the
function to be placed directly on the stack per invocation (or something
like that.) That would be in contrast to loading one instance of the
function and branching to it each time.

I don't believe that means I would have a separate instance of a member
function loaded per object instance. But I would like some verification in
this regard.

I'm wondering what the consequences of having the default case throw the
exception indicated in the following code:

template <typename T>
inline T Vector3<T>::operator[](const unsigned& index) const
throw (out_of_range)
{
switch(index){
case 0: return _v[0];
case 1: return _v[1];
case 2: return _v[2];
default:
std::stringstream ss;
ss << namespace_name + "::"
<< class_name
<< "::operator[" << index <<"]";
throw out_of_range(ss.str());
}
}

Could that impact the ability of the compiler to inline the function, or
cause significant bloat if it were inlined?

I also wonder about the performance hit I take by using the switch
statement. I believe this boils down the fact that I can't both have my
cake (ideal performance) and eat it(checked access.) Is this really a case
of either/or? Could I reasonably expect a compiler to optimize away the
comparison for index values 0,1,2?
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #1
2 1222
Steven T. Hatton wrote:
I don't really understand the details of what happens when a compiler
inlines a given function.

It's implementation dependant.

....
I'm wondering what the consequences of having the default case throw the
exception indicated in the following code:

template <typename T>
inline T Vector3<T>::operator[](const unsigned& index) const
throw (out_of_range)
{
switch(index){
case 0: return _v[0];
case 1: return _v[1];
case 2: return _v[2];
default:
std::stringstream ss;
ss << namespace_name + "::"
<< class_name
<< "::operator[" << index <<"]";
throw out_of_range(ss.str());
}
}
This would probably be better:

template <typename T>
inline T & Vector3<T>::operator[](const unsigned& index)
{
if ( index > 2 ) // index is unsigned so it can't be less than 0
{
IsBadIndex( class_name, index );
assert( false ); // should never get here
}
return _v[ index ];
}

template <typename T>
inline const T & Vector3<T>::operator[](const unsigned& index) const
{
if ( index > 2 ) // index is unsigned so it can't be less than 0
{
IsBadIndex( class_name, index );
assert( false ); // should never get here
}
return _v[ index ];
}

And don't inline IsBadIndex ...

Could that impact the ability of the compiler to inline the function, or
cause significant bloat if it were inlined?

I also wonder about the performance hit I take by using the switch
statement. I believe this boils down the fact that I can't both have my
cake (ideal performance) and eat it(checked access.) Is this really a case
of either/or? Could I reasonably expect a compiler to optimize away the
comparison for index values 0,1,2?


My experience with optimizers is that they do a good job with conditionals.
Jul 22 '05 #2
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:1L********************@speakeasy.net...
I don't really understand the details of what happens when a compiler
inlines a given function. I know that basically it's supposed to cause the function to be placed directly on the stack per invocation (or something
like that.) That would be in contrast to loading one instance of the
function and branching to it each time.

I don't believe that means I would have a separate instance of a member
function loaded per object instance. But I would like some verification in this regard.
The keyword "inline" expresses the programmer's desire to have the
function's body inserted into the caller's body, eliminating the overhead
that can be associated with a function call. It has little to do with the
number of instances of an object. If some functions call a function X total
times, and that called function is inlined in every caller, then you would
have X copies of that function.
I'm wondering what the consequences of having the default case throw the
exception indicated in the following code:

template <typename T>
inline T Vector3<T>::operator[](const unsigned& index) const
throw (out_of_range)
{
switch(index){
case 0: return _v[0];
case 1: return _v[1];
case 2: return _v[2];
default:
std::stringstream ss;
ss << namespace_name + "::"
<< class_name
<< "::operator[" << index <<"]";
throw out_of_range(ss.str());
}
}

Could that impact the ability of the compiler to inline the function, or
cause significant bloat if it were inlined?

<snip>

Well, as it has already been said, this is all depends on the
implementation. But I think it's worth noting that you are using an
exception specification, which is something that most experts do not
recommend you do. Sutter says that you shouldn't bother to write them
(http://www.gotw.ca/gotw/082.htm). The boost "exception specification
rationale" (http://www.boost.org/more/lib_guide....-specification)
mentions that some compilers turn off inlining if there is an exception
specification.

--
David Hilsee
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: DJ WIce | last post by:
Hi all, I did make a script/css thing to replace the contextmenu on the website with a new one: http://www.djwice.com/contextmenu.html It works nice in MSIE, but on Netscape (and probable...
20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
8
by: Gabor | last post by:
Hi, I have an app. that uses an MSDE database. I hardcoded the login and password in the application, but it is very simple to see with an ILDASM.exe tool. Is it any procedure to obscure the...
4
by: NotGiven | last post by:
setting up LAMP server. on old server with single Intel PII 450 MGHz, 64 MG Ram and two 9.1 GB Hard drives. How much RAM do I need to run MySQL efficiently?
16
by: Blue Apricot | last post by:
X-No-Archive How long will Classic ASP be supported by Microsoft? Should I start learning ASP.NET? Is it hard if you already know ASP? Blue Apricot
3
by: ary | last post by:
I try to create a weblog host site! in this case i can't use cache for every page because that cause to be my Server ram full of caching page. but if I can save cache in hard disk my problem...
3
by: trahul87 | last post by:
Hi, I have a Toshiba Satellite A25 - S207. The internal hard disk has just crashed. I do not want to change the hard disk since its very expensive. So, is it possible to boot using an external...
5
by: Summercool | last post by:
wow... i didn't know that, for <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:// www.w3.org/TR/html4/loose.dtd"> <span style="width: 100px; background:...
3
by: Kurt Mueller | last post by:
David, Am 07.10.2008 um 01:25 schrieb Blubaugh, David A.: As others mentioned before, python is not the right tool for "HARD REAL TIME". But: Maybe you can isolate the part of your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.