473,809 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inlining Constructors

Hi,
I remember from somewhere reading that inlining constructors is a
'BadThing', but now can't seem to find the original source. I can't
however thing of a reason why it would be for simple constructors...
any thoughts
Mike

May 24 '07 #1
21 2055
On May 24, 1:52 pm, Michael Hull <mikehul...@goo glemail.comwrot e:
Hi,
I remember from somewhere reading that inlining constructors is a
'BadThing', but now can't seem to find the original source. I can't
however thing of a reason why it would be for simple constructors...
any thoughts

Mike
Hi,
For simple constructors of classes having implicit data types as their
data members only it is ok to have inline constructors but even it is
not getting derived.

But for heavy classes (classes with many user defined data types as
data members) inlining constructors is a bad thing and it becomes even
more worse if the class is inherited from some other class.

When you will look at the number of lines in the constructor you will
feel it should be allowed, but an object construction results in
calling so many constructor calls (of all composed objects and of all
base classes and their composed objects).

Please correct me if i am wrong.

Vineet

May 24 '07 #2
Vineet Chawla wrote :
On May 24, 1:52 pm, Michael Hull <mikehul...@goo glemail.comwrot e:
>Hi,
I remember from somewhere reading that inlining constructors is a
'BadThing', but now can't seem to find the original source. I can't
however thing of a reason why it would be for simple constructors...
any thoughts

Mike

Hi,
For simple constructors of classes having implicit data types as their
data members only it is ok to have inline constructors but even it is
not getting derived.

But for heavy classes (classes with many user defined data types as
data members) inlining constructors is a bad thing and it becomes even
more worse if the class is inherited from some other class.

When you will look at the number of lines in the constructor you will
feel it should be allowed, but an object construction results in
calling so many constructor calls (of all composed objects and of all
base classes and their composed objects).
You're describing WHEN it is bad, but not WHY it is bad, so this is not
helping. I can think of several reasons: inlining large functions
causes massive code bloat, thus increasing the size of your executable
and probably decreasing performance. Another reason might be that the
larger the constructor is, the more dependencies (on average) it has on
the rest of the code, causing you to rebuild large parts of your
application for even small changes that don't have to do anything with
the users of the constructor in question.

However, these reasons all apply to regular functions as well, and the
threadstarter was explicitely talking about constructors. Besides that,
keep in mind that 'inline' is only a hint to the compiler - a good
compiler will make his own judgements on whether to inline a function
or not, no matter if the constructor/function was actually defined as
being inline, so the first argument is pretty moot.

So to conclude, I have no idea why inlining constructors in particular
is a Bad Thing.

- Sylvester
May 24 '07 #3
Sylvester Hesp wrote:
Besides that,
keep in mind that 'inline' is only a hint to the compiler
It's not *only* a hint. It does more than that (specifically
related to linking).
May 24 '07 #4
Juha Nieminen wrote:
Sylvester Hesp wrote:
>Besides that,
keep in mind that 'inline' is only a hint to the compiler

It's not *only* a hint. It does more than that (specifically
related to linking).
Yes I might've been a bit unclear. I wasn't talking about the inline
keyword in general, I was talking about the actual process of function
inlining by the compiler.

- Sylvester
May 24 '07 #5
On May 25, 4:07 am, Sylvester Hesp <s.hes...@SPAMo isyn.nlwrote:
Juha Nieminen wrote:
Sylvester Hesp wrote:
Besides that,
keep in mind that 'inline' is only a hint to the compiler
It's not *only* a hint. It does more than that (specifically
related to linking).

Yes I might've been a bit unclear. I wasn't talking about the inline
keyword in general, I was talking about the actual process of function
inlining by the compiler.

- Sylvester
If you see the third paragraph in my reply there i have mentioned why
it is bad.
When you will look at the number of lines in the constructor you will
feel it should be allowed, but an object construction results in
calling so many constructor calls (of all composed objects and of all
base classes and their composed objects).
Yes i agree with your point that it is just a hint to the compiler. I
don't know if there is some other reason as well for this.

Regards
Vineet

May 25 '07 #6
Vineet Chawla wrote:
On May 25, 4:07 am, Sylvester Hesp <s.hes...@SPAMo isyn.nlwrote:
>Juha Nieminen wrote:
>>Sylvester Hesp wrote:
Besides that,
keep in mind that 'inline' is only a hint to the compiler
It's not *only* a hint. It does more than that (specifically
related to linking).
Yes I might've been a bit unclear. I wasn't talking about the inline
keyword in general, I was talking about the actual process of function
inlining by the compiler.

- Sylvester

If you see the third paragraph in my reply there i have mentioned why
it is bad.
>> When you will look at the number of lines in the constructor you will
feel it should be allowed, but an object construction results in
calling so many constructor calls (of all composed objects and of all
base classes and their composed objects).

Yes i agree with your point that it is just a hint to the compiler. I
don't know if there is some other reason as well for this.

Regards
Vineet
The fact that you write a method, function or constructor inline does
*NOT* force the compiler to inline the code and cause 'massive code
bloat'. I would be very disappointed in such a stupid compiler.

People get so confused about inlining. As Juha said the meaning of
inline is to tell the compiler not to apply the 'one definition rule',
i.e. it means it's OK to put the function definition in a header file.
Whether it's a good idea to do this or not is primarily a style issue.

Whether a function's code is inlined is an optmization issue, and I
would exect a good compiler not to inline some functions I had declared
inline, and also to inline some functions I had not declared inline.
Both of these things are optmizations which the compiler can work out
for itself. VC++ 2005 does this.

john

May 25 '07 #7
On May 25, 8:12 am, John Harrison <john_androni.. .@hotmail.comwr ote:
Vineet Chawla wrote:
On May 25, 4:07 am, Sylvester Hesp <s.hes...@SPAMo isyn.nlwrote:
Juha Nieminen wrote:
Sylvester Hesp wrote:
Besides that,
keep in mind that 'inline' is only a hint to the compiler
It's not *only* a hint. It does more than that (specifically
related to linking).
Yes I might've been a bit unclear. I wasn't talking about the inline
keyword in general, I was talking about the actual process of function
inliningby the compiler.
- Sylvester
If you see the third paragraph in my reply there i have mentioned why
it is bad.
> When you will look at the number of lines in the constructor you will
feel it should be allowed, but an object construction results in
calling so many constructor calls (of all composed objects and of all
base classes and their composed objects).
Yes i agree with your point that it is just a hint to the compiler. I
don't know if there is some other reason as well for this.
Regards
Vineet

The fact that you write a method, function or constructor inline does
*NOT* force the compiler to inline the code and cause 'massive code
bloat'. I would be very disappointed in such a stupid compiler.

People get so confused aboutinlining. As Juha said the meaning of
inline is to tell the compiler not to apply the 'one definition rule',
i.e. it means it's OK to put the function definition in a header file.
Whether it's a good idea to do this or not is primarily a style issue.

Whether a function's code is inlined is an optmization issue, and I
would exect a good compiler not to inline some functions I had declared
inline, and also to inline some functions I had not declared inline.
Both of these things are optmizations which the compiler can work out
for itself. VC++ 2005 does this.

john
Thanks for all the replies..... most of this is covered in the FAQ, i
was wondering whether the Constructor is a special case in particular,
i.e, If I have a simple, POD class, is there a reason not to inline
the constructor (except the standard reasons of not inlining...) ?

Thanks for the replies
Mike

May 25 '07 #8
On May 25, 9:55 am, Michael Hull <mikehul...@goo glemail.comwrot e:

[...]
Thanks for all the replies..... most of this is covered in the FAQ, i
was wondering whether the Constructor is a special case in particular,
i.e, If I have a simple, POD class, is there a reason not to inline
the constructor (except the standard reasons of not inlining...) ?
The main reason for the special rule concerning constructors
(and destructors) is that even if the function looks trivial, it
might not be, which is not normally the case for other
functions. At least one place I worked (a long time ago) did
have a rule in the coding guidelines against inlining
constructors and destructors.

Several things have changed since then. Projects have become
larger (so dependency management becomes more important),
function calls faster, and the most frequent general rule today
is not to inline anything until the profiler says to do so. On
the other hand, templates in the absense of export means that
the dependencies are there anyway, so some guidelines are more
liberal with regards to inlining template functions (where they
allow templates).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 25 '07 #9
James Kanze wrote:
On May 25, 9:55 am, Michael Hull <mikehul...@goo glemail.comwrot e:

[...]
>Thanks for all the replies..... most of this is covered in the FAQ, i
was wondering whether the Constructor is a special case in particular,
i.e, If I have a simple, POD class, is there a reason not to inline
the constructor (except the standard reasons of not inlining...) ?

The main reason for the special rule concerning constructors
(and destructors) is that even if the function looks trivial, it
might not be, which is not normally the case for other
functions. At least one place I worked (a long time ago) did
have a rule in the coding guidelines against inlining
constructors and destructors.
Just out of interest, what did those guidelines say about simple
aggregates containing lots of members that have no trivial
constructors?

struct Foo
{
Type1 a;
Type2 b;
Type3 c;
// etc.
};

It has no defined default constructor, so one is generated. But the
generated constructor can be very expensive, and you have no control
over whether it gets inlined. Did those guidelines said you should
define a non-inlined constructor to circumvent compiler inlining?

- Sylvester
May 25 '07 #10

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

Similar topics

17
2356
by: Tony Vitonis | last post by:
From what I can tell, VB.NET doesn't allow programmers to say explicitly that they want a function to be inlined. Now, I'm a big fan of factoring out duplicate code, but I don't want to do too much of it in VB if there's going to be a noticeable performance hit. So: 1. Does the VB compiler sometimes inline simple functions? 2. Is there much of a performance hit on a function call? Thanks.
6
1641
by: glen_stark | last post by:
Hi. I'm just curious if there any warnings or caveats or whatever to be aware of when inlining function object calls? If the answer is no, they inline just like everything else, that's good news for me. glen
10
2343
by: gianguz | last post by:
The question is about the possible use of inlining to improve performance in a heavy multithreading environment (200+ threads). If we have to work with applications in which threads aren't I/O bounded or user-time bounded (i.e. windows based applications) but they are concurrently involved in the execution of the same parallelized task (i.e. a matrix-matrix multiplication), we must ensure that the advantage obtained by the parallelization...
15
2056
by: Generic Usenet Account | last post by:
While I have a very good feel for how inlining works, I fail to see how in the world inlining can work if the inlined function is not described "in place" in a header file, but rather defined in a separate source file (using the inline keyword), which gets linked in? Does inling work at all in such cases? If it does, can someone kindly explain how the compilers handle that? If it does not, is that documented somewhere? Thanks, Gus
5
1472
by: Raphael | last post by:
Hello, I have 2 files A.c : Defining a bunch of usefull functions B.c : Defining a main that uses these ones. I need to improve perfs : I know that, if the caller and the functions called were in the same file, the compiler would automatically inline them if possible.
11
2330
by: Elpoca | last post by:
Hi: What rules govern the inlining of templated functions and templated class methods? It has always been my understanding that both templated functions and templated class methods were always expanded inline. Recently, I replaced an explicitly written function with one implemented using templates (and partial-template specialisation), in the belief that the the latter would be entirely inlined-away by the compiler, leaving the
21
1803
by: LuB | last post by:
How judicious ought one be when inlining small methods. I once read that in general, most compiles will only inline 'one' level. IE: if all the following methods were declared/defined as inline in their respective classes, can I expect the compiler to try and inline them all? obj.GetSize()
15
3072
by: Lloyd Dupont | last post by:
I have some code which looks like that: public CornerStyle RectCornerMode { get { return this.GetValue<CornerStyle>(); } set { this.SetValue<CornerStyle>(value); } }
8
1511
by: Yakov | last post by:
I'd like a tool that performed inlining of function bodies of which do not appear in the .h file. Really. gcc on Linux. Yakov
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10120
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.