From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,115cdbb394b3e615 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!newscon02.news.prodigy.com!newscon06.news.prodigy.com!prodigy.net!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Why can't you create a out of order subtype? Date: 03 Feb 2005 17:05:30 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <1107301914.648240.237290@g14g2000cwa.googlegroups.com> NNTP-Posting-Host: shell01-e.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1107468331 11624 69.38.147.31 (3 Feb 2005 22:05:31 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Thu, 3 Feb 2005 22:05:31 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: g2news1.google.com comp.lang.ada:8157 Date: 2005-02-03T17:05:30-05:00 List-Id: Marius Amado Alves writes: > On 3 Feb 2005, at 19:40, Robert A Duff wrote: > > > Martin Dowie 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