TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
"W. Wesley Groleau x4923" <[log in to unmask]>
Reply To:
W. Wesley Groleau x4923
Thu, 3 Dec 1998 09:39:16 -0500
text/plain (76 lines)
> > C++ "templates" are much more powerful than Ada generics, unless
> > Ada-95 has beefed them up. ....

First time I _ever_ heard anyone claim that templates were "more powerful"
than even Ada 83 generics.  I don't use C++, so I don't know.

>       To describe template expressions (as in Blitz), let's make up a
>       scenario. Suppose we want a class to do matrix operations, and we
>       want it to use operator overloading. So we want to do:
>                 A = B + C + D;
>         to add some 3x4 matrices.  ...
>       The overhead is horrendous, and it's difficult for an optimizer to
>       do much to help......
>         In Blitz, though, the matrix expression A = B + C + D
>         would construct code to compute A = B + C + D as an
>         expression. In other words, the generated code would
>         look more like [adding all three corresponding terms
>         in the same loop iteration]

Wouldn't a decent optimizer for any language do the same?  (Provided
function inlining was allowed.)

>         - Recursive template instantiation (mostly implicitly)

I don't see the connection between this and the above example.

>       The real work at runtime is done in Blitz methods that are not
>       normally inlined. So there is no great increase in memory.  Here's
>       a C++ example to try:
>         [13 SLOC factorial implementation]

I don't see the connection between a C++ example and praising Blitz.
Are you [he?] trying to show that C++ produces bad code for this?

>       Compile this with full optimization and look at the generated
>       code!  Can this be translated into Ada95?

Yes - but here's Ada 83 (and you don't need three special cases; one is

   function Factorial (Argument : Natural)
                       return     Natural  is
      if Argument = 0 then return 1;
      else return Factorial (Argument - 1);
      end if;
   end Factorial;

Four SLOC -- six if I add the other two special cases.  Easy to make
generic, but all you save is two type conversions, at the expense of
instantiation.  Now some people would claim (I'm not convinced) that the
recursion makes it very slow.  To make them happy:

   function Factorial (Argument : Natural)
                       return     Natural  is
      Result : Natural := 1;
      for I in 1 .. Argument loop
         Result := Result * I;
      end loop;
      return Result;
   end Factorial;

Five SLOC.

I don't have the time to examine the generated code.  Unless someone else
proves otherwise, I am going to assume it's quite efficient.

Of course, the matrix example is much more complicated than the above,
but with inlining of "+" and a "not-too-bad" optimizer, ....