Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: Having constructors is expensive. Message-ID: <6590137@hplsla.HP.COM> Date: 1 Jun 89 22:24:06 GMT References: <11615@ulysses.homer.nj.att.com> Organization: HP Lake Stevens, WA Lines: 41 > jeffb@grace.cs.washington.edu (Jeff Bowden) writes: > > > > The problem (which I found by examining the output of CC -F) is that > >the empty constructor (which is defined in the class declaration, so it is > >should to be inline) somehow gets incarnated and called as a normal function! > > > > This wouldn't be so bad except that I declare many arrays of vectors > >of this class and I generally use only 10% of the elements. (I have an empty > >constructor because of the vectors; fascist cfront doesn't like it when you > >declare vectors of things which haven't any no-argument constructor). > > The reason C++ demands a no argument constructor is so that it can > maintain what I call the "allocation guarantee". Namely every area > of storage that it will call an X must be initialized by a > constructor. In the case of something like > > X array[1000] > > this means that the X() constructor must be called 1000 times to > "initialize" the areas that are holding an X. This is done by a > function that is passed the constructor as an argument. This is why > The X() constructor is being incarnated, and presumably it is also > account for the profiling showing many calls to X(). One might hope for a compiler that inlines code for array initialization if a class's X() constructor is declared inline. I see nothing that requires a compiler to use a vector new function expecting a pointer to an X() constructor. For example, why couldn't a compiler pass a null function pointer to the vector new function, test in the vector new function whether or not to call that function, and then generate an inline for-loop with the inline X() constuctor to do the actual init? Good optimizing backend C "code generators" would be smart enough to void the loop if the X() constructor is null. Its ugly to have to come up with hack work-arounds for compiler deficiencies. Compilers need to do a much better job of respecting a user's request for inlining -- resolving inline verses non-inline compiler deficiencies presently takes me more time than all other aspects of C++ code writing! One might question how usable present compilers are for arrays of objects. I'm always having to find hack work-arounds. Is this what C++ is about ???