comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@merv.cs.nyu.edu (Robert Dewar)
Subject: C vs Ada code quality
Date: 1997/04/24
Date: 1997-04-24T00:00:00+00:00	[thread overview]
Message-ID: <dewar.861854980@merv> (raw)


Someone asked me, but I lost the email address, because it bounced, the
following (so I am not sure who asked)

<<Robert Dewar <dewar@merv.cs.nyu.edu> writes:

  > Valentin asks
  >
  > <<Can C beat Ada in theory for a given type of compiler (say pure
  > separate compilation - no inter-module annalysis / no
  > inter-procedure annalysis) ?>>
  >
  > If runtime checks are off, the answer is NO!

Suppose I want to define the constructor/destructor for a type. In
C++ I just define them. In Ada, I use the Finalisation type and
I (as far as I understand) overide them thus the object will have
tag (4 bytes long pointer ?); if the object is very small, this
can be significant.

Larger objects can slow down the program (larger blocks to allocate,
more cache miss, more page faults).

Ok this is a pretty small problem, but this is an example which
can be part of 'distributed fat' (many small overheads which
end-up slowing the program).

This problem explainned has perhaps a solution (other then not
using constructors/destructors) but this is just an idea.

Also, I understand that it may be difficult to compare different
compilers using different optimising methods.

Any reasons why Ada could be faster (in theory, no real numbers
required) then C or C++ when all checks are off ? ;-)
>>



A couple of important points here

C IS NOT THE SAME LANGUAGE AS C++ !

This confusion is an amazingly common one. I made a statement about C,
but the response assumes I was talking about C++, false! C++ has some
high level constructs that do not necessarily map identically into Ada,
so I would not make the same claim for C++ (that any program in C could
be mapped identically into Ada).

However, the example here is bogus. The contstructor/destructor mechanism
in C++ is entirely analogous to the controlled type facility in Ada 95.

There is nothing in Ada 95 that says that the tag is stored as part of
the value -- note that in formal RM terms, the tag is NOT part of the
value, and if you start thinking of it, even informally, as part of
the value, you will find the RM confusing (for example, an assignment
copies the value, but it does not copy the tag).

An implementation of destructors and constructors in C++ may or may not
add overhead to stored objects. An implementation of controlled types
in Ada 95 may or may not add overhead to stored objects. In any case
you are in the business of comparing implementations, not languages.

<<Any reasons why Ada could be faster (in theory, no real numbers
required) then C or C++ when all checks are off ? ;-)>>

Absolutely, consider the aliasing problem, in C, we write

  *q = 1;

and if q is a *int, then without very hard analysis that in any case can
never be entirely complete, we have to assume that any int values that
are temporarily in registers may be destroyed. There are many other cases
in which the "excessive" freedom of C pointers degrade the generated code.
Now this does not mean that an Ada compiler *will* do a better job, but
it means it could. For example, gcc does not do a very good job of aliasing
analysis (partly because it is not so useful for C), so GNAT does not see
this advantage -- but everyone knows that adding better aliasing analysis
and data structures to gcc would be helpful, even for C, and when this
is done, you will certainly be able to construct examples where Ada takes
advantage of this and does better than is possible in C.





             reply	other threads:[~1997-04-24  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-04-24  0:00 Robert Dewar [this message]
1997-04-26  0:00 ` C vs Ada code quality Valentin Bonnard
1997-04-26  0:00   ` Robert Dewar
1997-04-29  0:00     ` Richard Kenner
  -- strict thread matches above, loose matches on Subject: below --
1997-05-01  0:00 Valentin Bonnard
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox