Topic: Efficient template generation


Author: bmcphers@aw.sgi.com (Brent McPherson)
Date: 1996/05/13
Raw View
In article <01bb3886.8095e1a0$c4c2b7c7@www.doubleplus.com> "Bradd W. Szonye" <bradds@ix.netcom.com> writes:

> From: "Bradd W. Szonye" <bradds@ix.netcom.com>
> Newsgroups: comp.std.c++
> Date: 3 May 1996 00:21:49 GMT
> Organization: Netcom
> X-Nntp-Posting-Host: det-mi6-04.ix.netcom.com
> X-Netcom-Date: Thu May 02  7:25:21 PM CDT 1996
> X-Lines: 37
>
>
> On Thursday, May 02, 1996, Fergus Henderson wrote...
> > Valentin Bonnard <bonnardv@pratique.fr> writes:
> >
> > >Willy Wood <ofile@main.aquanet.co.il> wrote:
> > >>If I have ten instantations of an STL container, for
> > >>ten different pointer types, which compilers/linkers, if any, will
> know
> > >>to generate only one set of template code in the final executable ?
> > >
> > >But Stroustrup (in The C++ Programming Language, second edition, I
> > >think) gives the solution: instead of having one class template, you
> > >will have two.
> > [... details of solution with base class using `void *' ...]
> >
> > Yes, but do you know of any STL implementations which actually do this?
>
> (Bradd sighs.)
> Here we are again, at the mercy of our compiler and library vendors.
> Perhaps the STL implemenations don't do this *now*, but they should. Or
> the compiler should optimize it properly. There are currently five good
> solutions to this problem, depending on your level of ambition and
> persistence.
>

In all fairness, I have used both approaches and can say that the technique
proposed by Stroustrup works well for collections of pointers but not so
well for collections of objects where the container must properly call
the constructor/destructor/copy constructor when adding/removing from
a container.

The only way to separate it (that I have found) and still handle arbitrary
objects is to insert pure virtual methods in the base (void *) constainer
that are called to construct/destruct/copy objects and have the template
provide those methods for the specific instantiation type.

Also, the container will have to deal with different sized objects
if you want to put more than pointers into it.

None, of these are of course impossible to do but you end up
with slightly less efficient code because of the virtual function
calls at the expense of less duplicate code introduced by templates.

Finally, STL would not be likely to use this approach since it
seems to avoid virtual functions and prefer code generation
through template instantiation. (ie. the void * conatiner approach
is not in sync with the general philosophy behind STL and would be
difficult to make it work in STL. IMHO)

Of course, compiler writers could note which template methods don't
depend on any template type parameters (or static class data)
and only generate a single version of these methods for all
instantiations. This would complicate the template generation (especially
since type information is encoded into the generated names) but I still
think it would be a very worthwhile optimization.

Cheers!
____________________________________________________________________________
Brent McPherson                    . .          .  Alias | Wavefront Inc.
Senior Software Engineer         .     .       .   110 Richmond St. E.
E-Mail: bmcphers@aw.sgi.com     .       .     .    Toronto, CANADA  M5C 1P1
Phone: (416) 362-8558 ext.212  .          . .      http://www.aw.sgi.com
--
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Willy Wood <ofile@main.aquanet.co.il>
Date: 1996/04/27
Raw View
If I have ten instantations of an STL container, for
ten different pointer types, which compilers/linkers, if any, will know
to generate only one set of template code in the final executable ?


--
Willy Wood,



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/05/02
Raw View
Valentin Bonnard <bonnardv@pratique.fr> writes:

>Willy Wood <ofile@main.aquanet.co.il> wrote:
>>If I have ten instantations of an STL container, for
>>ten different pointer types, which compilers/linkers, if any, will know
>>to generate only one set of template code in the final executable ?
>
>But Stroustrup (in The C++ Programming Language, second edition, I
>think) gives the solution: instead of having one class template, you
>will have two.
[... details of solution with base class using `void *' ...]

Yes, but do you know of any STL implementations which actually do this?

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Bradd W. Szonye" <bradds@ix.netcom.com>
Date: 1996/05/03
Raw View

On Thursday, May 02, 1996, Fergus Henderson wrote...
> Valentin Bonnard <bonnardv@pratique.fr> writes:
>
> >Willy Wood <ofile@main.aquanet.co.il> wrote:
> >>If I have ten instantations of an STL container, for
> >>ten different pointer types, which compilers/linkers, if any, will
know
> >>to generate only one set of template code in the final executable ?
> >
> >But Stroustrup (in The C++ Programming Language, second edition, I
> >think) gives the solution: instead of having one class template, you
> >will have two.
> [... details of solution with base class using `void *' ...]
>
> Yes, but do you know of any STL implementations which actually do this?

(Bradd sighs.)
Here we are again, at the mercy of our compiler and library vendors.
Perhaps the STL implemenations don't do this *now*, but they should. Or
the compiler should optimize it properly. There are currently five good
solutions to this problem, depending on your level of ambition and
persistence.

(1) Live with the current state of affairs.
(2) Find a different vendor.
(3) Complain to your library vendor to implement the classes intelligently
(4) Complain to your compiler vendor to implement the template optimizer
intelligently
(5) Write the compiler and/or library yourself and make a lot of money

--
Bradd W. Szonye (bradds@ix.netcom.com), Doubleplus Corporation

"To chill or to pop a cap in my dome, whoomp, there it is."
   -- Hamlet, Prince of Denmark



[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1996/05/02
Raw View
Willy Wood <ofile@main.aquanet.co.il> wrote:
>If I have ten instantations of an STL container, for
>ten different pointer types, which compilers/linkers, if any, will know
>to generate only one set of template code in the final executable ?

It would be interresting, especialy when templates are used to have type
checking:

Vector<A*>      va;
Vector<B*>      vb;

If you write:

Vector<void*>   va, vb;

you lose the type-checking. I don't know any compiler/linker which
generate only one Vector code, even if the code is _exactly_ the same.

But Stroustrup (in The C++ Programming Language, second edition, I
think) gives the solution: instead of having one class template, you
will have two.

Don't write:

template <typename T>
class Containner
{
    private:
        T* something;

    public:
        T*      get (X x); // inline functions or not, that's a detail
        void    put (T* v, X x);
};

but remplace with

class BaseNonTyped
{
    private:
        void* something;

    public:
        void*   get (X x); // inline functions or not, that's a detail
        void    put (void* v, X x);
};

template <typename T>
class DerivedTyped : private BaseNonTyped
{
    public:     // inline all these functions
        T*      get (X x) { return (T*) BaseNonTyped::get (x); }
        void    put (T* t, X x) { BaseNonTyped::put (t, x); }
};

BaseNonTyped is compiled where is it defined (once); DerivedTyped
functions are compiled when called, but there is nothing to compile, so
the code exec just contains a direct BaseNonTyped::f call.

-----------------------------
My opinions are mines.

Valentin Bonnard
bonnardv@pratique.fr
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]