comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Why can't you create a out of order subtype?
Date: 03 Feb 2005 17:05:30 -0500
Date: 2005-02-03T17:05:30-05:00	[thread overview]
Message-ID: <wccpszhuwnp.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: mailman.121.1107462019.527.comp.lang.ada@ada-france.org

Marius Amado Alves <amado.alves@netcabo.pt> writes:

> On 3 Feb 2005, at 19:40, Robert A Duff wrote:
> 
> > Martin Dowie <martin.dowie@btopenworld.com> writes:
> >
> >> Marius Amado Alves wrote:
> >>> For run-time I find the "Value of Image" idiom a better one. It's
> >>> readily available. No Unchecked_Conversion required.
> >>> declare
> >>>    M : Msg := Whatever;
> >>>    V : Vowelmsg;
> >>> begin
> >>>    V := Vowelmsg'Value (Msg'Image (M));
> >>>    -- if control reaches here then M is a vowel
> >>> exception
> >>>    when Constraint_Error => -- M is not a vowel
> >>> end;
> >
> > If there are no further uses of V, then I think 11.6 allows the
> > compiler
> > to eliminate the raising of Constraint_Error.  I don't much like
> > 11.6...
> >
> >> That's very, very, very slow...
> >
> > True (if it works at all).
> 
> Before I though you were complaining about the slowness of the Image
> (Value) operations (because there is probably parsing involved and
> string values on and off the stack) compared with unchecked conversion
> and 'Valid (which is very probably faster).

Well, I wasn't the one who originally complained.  When I wrote "True"
above, I was thinking of the cost of Image and Value operations (string
manipulation), which is probably much more than the precalculated-table
method I mentioned earlier.  I don't much like the U_C method here (too
kludgy), so I don't care much about its speed.

As you (or somebody) pointed out, whether Image/Value are "too slow"
depends on the program.

>... Now I realise that you were
> probably complaining about the slowness of the exception handling
> mechanism.

Well, that's an issue if there are a lot of vowels.  The speed of
Image/Value is an issue in any case.

>... In that case, my experience with GNAT is that it is not that
> slow at all. I've made controlled experiments in the past. I think I
> reported them here.

Well, whether "not that slow at all" is "too slow" depends on the
program.  ;-)  I'm pretty sure raising an exception is a lot slower than
the table lookup I suggested.  Note that my suggestion builds on yours
-- it just precalculates the Value-of-Image results.

Note that GNAT has two different exception handling methods, with wildly
different efficiency properties.  One has some overhead to enter the
handled region, and modest cost to do the "raise" (i.e. to find the
handler).  The other method has zero overhead to enter the handled
region, and *huge* cost to do the "raise".  I don't know which one you
were measuring.  I think the different methods are used on different
machines, and command-line switches can control it on some machines.

The huge-cost-for-raise is generally considered to be better, since most
programs are not like the above example -- they raise exceptions rarely.
But it's harder to implement, and impossible to implement portably.

On the other hand, if an exception is raised and handled in the same
procedure, it's pretty easy for the compiler to optimize the raise into
a single jump instruction.  I don't know if any Ada compilers do that.

On the third hand, Image and Value are probably implemented as
out-of-line calls, so that optimization probably doesn't apply.

> Now, you made me take a look at 11.6 and yes, it seems if V is not used
> it can simply hold an undefined result and the exception not raised. But
> I bet you'll get a compiler warning. And rightly so, as unused variables
> are almost certainly a logic bug. It would in this case.

I'm not comforted by that warning.  The warning probably says "V is not
used".  So the programmer (not knowing about 11.6) thinks "that's right,
and it's OK in this case", and puts in a pragma Warnings(Off).  I doubt
if the warning says "...and by the way, the compiler is planning to
generate broken machine code, as allowed by 11.6".

The example you wrote above *should* work as you expected (before you
reread 11.6).  I think 11.6 is evil.  The rest of the manual says 'Image
and 'Value raise C_E in certain cases, and tells you how to write an
exception handler for that C_E.  Then 11.6 comes along and says, never
mind all that, the compiler can generate code that does otherwise.
Most Ada programmers don't understand 11.6.  Even the people who *wrote*
11.6 (mainly me and Tucker) don't really understand it!

>... Unless Vowelmsg
> were controlled and the logic put on Adjust or something. But then the
> compiler would consider V used, no?

I don't remember the exact rules for that.  There were rules in Ada 95
that said the compiler can eliminate nonlimited controlled variables,
along with their Initialize, Adjust, and Finalize, even if those have
important effects.  But there was an AI on that, and the rules changed.

But those rules never applied to *limited* controlled types.
I often use a limited controlled variable whose only purpose is
to cause Finalize to happen.  And the compiler is not allowed to
eliminate that Finalize.  But I believe GNAT warns that the thing
is unused in this case, so I have to sprinkle pragma Warnings(Off)
around the code.  (I compile with the "treat warnings as errors"
switch.)  I also compile with AdaMagic, and I think it doesn't warn in
this case.  That's a problem with relying on warnings -- there's no
universal agreement on what deserves a warning.

- Bob



  reply	other threads:[~2005-02-03 22:05 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-02-01 23:51 Why can't you create a out of order subtype? brett_gengler
2005-02-02  0:24 ` Jeffrey Carter
2005-02-02 17:55   ` Marius Amado Alves
2005-02-02 18:50     ` Pascal Obry
2005-02-02 20:22       ` Marius Amado Alves
2005-02-03  4:36       ` Wes Groleau
2005-02-03 12:59         ` Marius Amado Alves
2005-02-04  3:42           ` Wes Groleau
2005-02-02 19:35     ` Martin Dowie
2005-02-02 20:35       ` Marius Amado Alves
2005-02-02 21:18         ` Martin Dowie
2005-02-03 19:40       ` Robert A Duff
2005-02-03 20:22         ` Marius Amado Alves
2005-02-03 22:05           ` Robert A Duff [this message]
2005-02-04  6:49           ` Martin Dowie
2005-02-02 22:26 ` Georg Bauhaus
2005-02-03 23:09 ` Robert A Duff
replies disabled

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